-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStack
74 lines (65 loc) · 1.44 KB
/
MyStack
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
public class MyStack <T>{
Node top;
int size;
public MyStack() {
top = null;
size = 0;
}
int getSize(){
return size;
}
void push(T value){
Node nodo = new Node(value);
if(top == null)
top = nodo;
else {
nodo.next = top;
top = nodo;
}
size++;
}
// Extra methods
void pushButtom(T value){
MyStack stk = new MyStack();
stk.push(value);
this.Reverse();
while(!this.isEmpty()){
stk.push(this.pop());
}
this.top = stk.top;
}
T pop(){
if(top == null)
throw new ArrayIndexOutOfBoundsException();
T value = (T) top.data;
top = top.next;
size--;
return value;
}
T peek(){
return (T) top.data;
}
boolean isEmpty(){
return top == null;
}
void Reverse(){
MyStack stk = new MyStack();
while(!isEmpty()){
stk.push(pop());
}
this.top = stk.top;
}
@Override
public String toString(){
Node curr = top;
String str = "[";
while(curr != null){
str += curr.data;
if(curr.next!= null)
str += ", ";
curr = curr.next;
}
str += "]";
return str;
}
}