-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.java
87 lines (66 loc) · 1.23 KB
/
stack.java
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
class NewNode{
Object data;
NewNode next;
NewNode(Object value){
next=null;
data=value;
}
void setData(Object value){
data=value;
}
Object getData(){
return data;
}
void setNext(NewNode nextValue){
next=nextValue;
}
NewNode getNext(){
return next;
}
}
class StackDS{
NewNode top;
int counter;
StackDS(){
top=new NewNode(null);
}
void push(Object data){
NewNode current=new NewNode(data);
current.setNext(top.getNext());
top.setNext(current);
counter++;
System.out.println(current+" "+
data+" "+
current.getNext()+" "+
top.getNext()+" "+
top);
}
Object pop(){
NewNode temp=top;
if (counter > 0 && temp.getNext() != null){
top=top.getNext();
counter--;
return temp.getNext().getData();
}
return "No more data";
}
Object peek(){
if (counter > 0 && top.getNext() != null){
return top.getNext().getData();
}
return "No more data";
}
}
public class stack {
public static void main(String args[]){
StackDS ss=new StackDS();
ss.push(1);
ss.push(2);
ss.push(3);
System.out.println(ss.peek());
System.out.println(ss.pop());
System.out.println(ss.peek());
ss.push('A');
System.out.println(ss.peek());
}
}