-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
58 lines (56 loc) · 1.14 KB
/
Stack.java
File metadata and controls
58 lines (56 loc) · 1.14 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
class Entity {
int data;
Entity down;
public Entity(int data) {
this.data = data;
this.down= null;
}
}
class Stackimp {
Entity top;
public Stackimp() {
this.top = null;
}
/*The data is strored in e; if top is null place e in top; else place the
present top value as top.down and and the data as the lates top*/
public void push(int data) {
Entity e = new Entity(data);
if(top == null) {
top = e;
}
else {
e.down = top;
top = e;
}
}
public int pop(){
int data = top.data;
top = top.down;
return data;
}
public void print() {
Entity temp;
if(top == null) {
System.out.println("Stack is empty");
}
for(temp = this.top; temp != null; temp = temp.down) {
System.out.println(temp.data);
}
}
}
class Stack {
public static void main(String[] args) {
Stackimp s = new Stackimp();
System.out.println("Stack after push");
s.push(2);
s.push(4);
s.push(6);
s.push(12);
s.push(18);
s.push(32);
s.print();
System.out.println("Stack after pop");
s.pop();
s.print();
}
}