-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTACK_with_LL.java
More file actions
76 lines (68 loc) · 1.49 KB
/
Copy pathSTACK_with_LL.java
File metadata and controls
76 lines (68 loc) · 1.49 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
package DATA_STRUCTURE;
import static java.lang.System.exit;
public class STACK_with_LL {
class Node {
int data;
Node next;
}
Node top;
STACK_with_LL(){
this.top = null;
}
public void push(int d){//adding elements
Node n = new Node();
n.data = d;
n.next = top;
top = n;
}
public boolean isEmpty(){//checking is empty or not
return top == null;
}
public int peek(){
if(top == null){
return top.data;
}
else{
System.out.println("Empty");
return -1;
}
}
public void pop(){//Removing
if(top == null){
System.out.println("Under flow");
return;
}
else{
top = top.next;
}
}
public void display(){//displaying
if(top ==null){
System.out.println("Underflow");
exit(1);
}
else{
Node temp = top;
while(temp!=null){
System.out.print(temp.data+"-->");
temp = temp.next;
}
}
}
public static void main(String[] args) {
STACK_with_LL s = new STACK_with_LL();
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.push(60);
s.push(70);
s.display();
s.pop();
s.pop();
s.pop();
System.out.println("\n\n");
s.display();
}
}