forked from Shradha27/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
152 lines (150 loc) · 3.16 KB
/
Copy pathStack.java
File metadata and controls
152 lines (150 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* This class Implements Stack
* @author hamza39460
*/
public class Stack<T> {
/**
* node for stack
*/
private static class Node<K>{
// data to store in node
K data;
// next node reference
Node next;
/**
* Constructor for Class Node
* @param data to be saved in node
*/
public Node(K data){this.data=data;
next=null;
}
}
// reference to head node of stack
private Node headNode;
// stack size
private int stackSize;
/**
* constructor for class stack
* initialize head node reference to null
* initialize stack size to zero
*/
public Stack(){
headNode=null;
stackSize=0;
}
/**
* to create new node
* @param data to be saved in node
* @return new created node
*/
private Node addNode(T data)
{
Node<T> newNode=new Node<T>(data);
return newNode;
}
/**
* @return size of stack
*/
public int stackSize()
{
return stackSize;
}
/**
* to push in stack
* @param data to be pushed in stack
*/
public void push(T data)
{
Node newNode=addNode(data);
newNode.next=headNode;
headNode=newNode;
stackSize++;
}
/**
* removes top element
* @return top element if not empty
* else return false
*/
public T pop()
{
if (headNode!=null)
{
T data=(T)headNode.data;
headNode=headNode.next;
stackSize--;
return data;
}
else
return null;
}
/**
* to get top element of stack
* @return top of stack if stack is not empty
* else return null
*/
public T top()
{
if (headNode!=null)
return (T)headNode.data;
else
return null;
}
/**
* to check if stack is empty
* @return
* true if stack is empty
* false if stack is not empty
*/
boolean is_empty()
{
if (headNode==null)
return true;
return false;
}
/**
* check if an element exist in stack
* @param key element to check
* @return
* true if element exists
* false if element does not exist
*/
boolean exist(T key)
{
if(headNode==null)
return false;
else if (headNode.data==key)
return true;
else {
Node temp=headNode;
while (temp!=null&&temp.data!=key)
{
temp=temp.next;
}
if (temp!=null)
return true;
}
return false;
}
/*
pop all elements from stack
*/
public void popAll(){
while (headNode!=null)
headNode=headNode.next;
}
// driver program
public static void main(String args[])
{
Stack<Double> obj;
obj = new Stack<Double>();
obj.push(2.2);
obj.push(3.3);
obj.push(4.4);
obj.push(5.5);
obj.push(6.6);
while(!obj.is_empty())
{
System.out.println(obj.pop());
}
}
}