-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.java
262 lines (228 loc) · 6.06 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface IStack {
/*** Removes the element at the top of stack and returnsthat element.
* @return top of stack element, or through exception if empty
*/
public Object pop();
/*** Get the element at the top of stack without removing it from stack.
* @return top of stack element, or through exception if empty
*/
public Object peek();
/*** Pushes an item onto the top of this stack.
* @param object to insert*
*/
public void push(Object element);
/*** Tests if this stack is empty
* @return true if stack empty
*/
public boolean isEmpty();
/**
* @return the size of the stack
*/
public int size();
/**
* print the stack in the form [1, 2, 3, .....]
*/
public void printStack();
}
public class MyStack implements IStack {
SingleLinkedList stack = new SingleLinkedList();
public Object pop(){
if(stack.isEmpty())
return null;
Object temp = stack.get(0);
stack.remove(0);
return temp;
}
public Object peek(){
if(stack.isEmpty())
return null;
return stack.get(0);
}
public void push(Object element){
stack.insertFirst(element);
}
public boolean isEmpty(){
return stack.isEmpty();
}
public int size(){
return stack.size();
}
public void printStack(){
stack.printList();
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
MyStack stack = new MyStack();
Scanner sc = new Scanner(System.in);
String st = sc.nextLine().replaceAll("\\[|\\]", "");
String[] s = st.split(", ");
if (!(s.length == 1 && s[0].isEmpty())){
for(int i = s.length - 1; i >= 0; i--){
Object o = Integer.parseInt(s[i]);
stack.push(o);
}
}
String keyword = sc.nextLine();
if(Objects.equals(keyword, "push")){
int element = sc.nextInt();
stack.push(element);
stack.printStack();
}
if(Objects.equals(keyword, "pop")){
if(stack.isEmpty()){
System.out.println("Error");
}
else{
stack.pop();
stack.printStack();
}
}
if(Objects.equals(keyword, "peek")){
if(stack.isEmpty()){
System.out.println("Error");
}
else{
System.out.println(stack.peek());
}
}
if(Objects.equals(keyword, "isEmpty")){
if(stack.isEmpty())
System.out.println("True");
else
System.out.println("False");
}
if(Objects.equals(keyword, "size")){
System.out.println(stack.size());
}
}
}
interface ILinkedList {
/**
* @param index
* @return the element at the specified position in this list.
*/
public Object get(int index);
/**
* @return true if this list contains no elements.
*/
public boolean isEmpty();
/**
* Removes the element at the specified position in this list.
* @param index
*/
public void remove(int index);
/**
* @return the number of elements in this list.
*/
public int size();
/**
* print the list in the form [1, 2, 3, .....]
*/
public void printList();
}
/**
* class which contains single list implementation
* @author Mohamed H.Sadek
*/
class SingleLinkedList implements ILinkedList {
public int length = 0;
public static boolean flag;
public class Node {
Object item;
Node next;
}
public Node head = null;
public Node tail = null;
public void insertFirst(Object element) {
Node newNode = new Node();
newNode.item = element;
if (length == 0) {
newNode.next = null;
head = tail = newNode;
}
else {
newNode.next = head;
head = newNode;
}
length++;
}
public Object get(int index) {
if(index < 0 || index >= length) {
return 0;
}
else if(index == 0) {
return head.item;
}
else if(index == length - 1) {
return tail.item;
}
else {
Node current = head;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.item;
}
}
public void remove(int index) {
flag = true;
Node current = head;
if(index < 0 || index >= length) {
flag = false;
}
else if(length == 1) {
head = tail = null;
length--;
}
else if (index == 0) {
head = head.next;
length--;
}
else if (index == length - 1) {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = null;
tail = current;
length--;
}
else {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
length--;
}
}
public boolean isEmpty() {
if (length == 0) {
return true;
}
else {
return false;
}
}
public int size() {
return length;
}
public void printList() {
Node current = head;
if(current == null) {
System.out.println("[]");
}
else {
System.out.print("[" + current.item);
current = current.next;
while (current != null) {
System.out.print(", " + current.item);
current = current.next;
}
System.out.println("]");
}
}
}