-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30. Stack.java
More file actions
243 lines (181 loc) · 5.02 KB
/
30. Stack.java
File metadata and controls
243 lines (181 loc) · 5.02 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
a Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. it’s a collection of elements, and the order of addition and removal is crucial.
in java, the Stack class is part of the java.util package and extends the Vector class.
NOTE: LIFO process is just like the undo option in word/powerpoint.
Basic Operations:
1. push: adds an element to the top of the stack. [time complexity -> O(1)]
2. pop: removes the top element from the stack. [time complexity -> O(1)]
3. peek: returns the top element without removing it. [time complexity -> O(1)]
Implementation:
1. Array (fixed size - last element as top)
2. ArrayList (variable size)
3. Linked List (variable size)
*/
//stack: linked list implementation
public class StackClass {
static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
next = null;
}
}
static class Stack {
public static Node head;
public static boolean isEmpty() {
return head == null;
}
//push function
public static void push(int data) {
Node newNode = new Node(data);
if(isEmpty()) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
//pop function
public static int pop() { //pop func deletes as well as return the value that's why 'int'
if(isEmpty()) {
return -1;
}
int top = head.data;
head = head.next;
return top;
}
//peek function
public static int peek() {
if(isEmpty()) {
return -1;
}
int top = head.data;
return top;
}
}
//main function
public static void main(String[] args) {
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
while(!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}
}
}
//stack: arraylist implementation
import java.util.ArrayList;
public class StackClass {
static class Stack {
static ArrayList<Integer> list = new ArrayList<>();
public static boolean isEmpty() {
return list.size() == 0;
}
//push function
public static void push(int data) {
list.add(data);
}
//pop function
public static int pop() {
if(isEmpty()) {
return -1;
}
int top = list.get(list.size() - 1);
list.remove(list.size() - 1);
return top;
}
//peek function
public static int peek() {
if(isEmpty()) {
return -1;
}
int top = list.get(list.size() - 1);
return top;
}
}
//main function
public static void main(String[] args) {
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
while(!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}
}
}
//stack: using collection framework
import java.util.*;
public class StackClass {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while(!stack.isEmpty()) {
System.out.println(stack.peek());
stack.pop();
}
}
}
/* Qs. Push at the bottom of Stack */
import java.util.*;
public class StackPrbs {
public static void pushAtBottom(int data, Stack<Integer> s) {
if(s.isEmpty()) {
s.push(data);
return;
}
int top = s.pop();
pushAtBottom(data, s);
s.push(top);
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
pushAtBottom(4, s);
while(!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}
}
}
/* Reverse the Stack */
import java.util.*;
public class StackPrbs {
public static void bottomPush(int data, Stack<Integer> s) {
if(s.isEmpty()) {
s.push(data);
return;
}
int top = s.pop();
bottomPush(data, s);
s.push(top);
}
public static void reverse(Stack<Integer> s) {
if(s.isEmpty()) return;
int top = s.pop();
reverse(s);
bottomPush(top, s);
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
while(!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}
}
}
// BYEEEEEEEEEEEEEEEEEEEEE :D