-
Notifications
You must be signed in to change notification settings - Fork 168
/
StackList.java
132 lines (107 loc) · 1.92 KB
/
StackList.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
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
/*
Solved By: Sandeep Ranjan (1641012352)
Solution for Programming Project
5.4
*/
class Node {
int data;
Node link;
Node(int data) {
this.data = data;
this.link = null;
}
}
class CircularLinklist {
private Node current;
public CircularLinklist() {
current = null;
}
public void insert(int data) {
Node newNode = new Node(data);
if(current == null) {
newNode.link = newNode;
current = newNode;
} else {
newNode.link = current.link;
current.link = newNode;
current = newNode;
}
}
public void delete() {
Node p = current;
if(p.link == current) {
current = null;
} else {
do {
p = p.link;
} while(p.link != current);
p.link = current.link;
current = p;
}
}
public void display() {
Node p = current;
if(current != null) {
do {
p = p.link;
System.out.print(p.data + " ");
} while(p != current);
System.out.println();
} else {
System.out.println("List Empty");
}
}
}
class Stack {
private CircularLinklist cll;
private int size;
private int ele;
public Stack(int size) {
cll = new CircularLinklist();
this.size = size;
this.ele = -1;
}
public void insert(int data) {
if(isFull()) {
System.out.println("Stack Full");
} else {
cll.insert(data);
ele++;
}
}
public void delete() {
if(isEmpty()) {
System.out.println("Stack Empty");
} else {
cll.delete();
ele--;
}
}
private boolean isFull() {
return ele == (size-1);
}
private boolean isEmpty() {
return ele == -1;
}
public void display() {
if(isEmpty()) {
System.out.println("Stack Empty");
} else {
cll.display();
}
}
}
class StackList {
public static void main(String[] args) {
Stack theStack = new Stack(5);
theStack.insert(10);
theStack.insert(20);
theStack.insert(30);
theStack.insert(40);
theStack.insert(50);
theStack.display();
theStack.delete();
theStack.insert(60);
theStack.display();
}
}