-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackUsingArray.java
160 lines (148 loc) · 4.74 KB
/
StackUsingArray.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
public class StackUsingArray {
// The Array used to implement stack
int[] arr;
// topOfStack is used to maintain the index of the topmost element present in stack. If not present then it will store -1
int topOfStack;
// constructor
StackUsingArray(int size){
this.arr = new int[size];
this.topOfStack=-1;
}
// used to initialize the array for stack
// Time Complexity - O(n), Space Complexity - O(n)
public void initializeArray(){
if(topOfStack==-1){
for(int i=0;i<this.arr.length;i++){
this.arr[i]=Integer.MIN_VALUE;
}
}
}
// function to push element to top of stack
// Time Complexity - O(1), Space Complexity - O(1)
public void push(int value){
// check if there is stack present or not
if(this.arr==null){
System.out.println("No stack present, first create a stack");
return;
}
// check if stack is full or not
if(this.topOfStack==(this.arr.length-1)){
System.out.println("Stack is full, Can't push into stack");
return;
}
// if stack is empty
this.topOfStack++;
this.arr[this.topOfStack]=value;
}
// function to pop an element from the top of stack
// Time Complexity - O(1), Space Complexity - O(1)
public void pop(){
// check if there is any stack present or not
if(this.arr==null){
System.out.println("No stack present, first create a stack");
return;
}
// check if stack is empty
if(this.topOfStack==-1){
System.out.println("Stack is empty, can't pop from stack");
return;
}
// if stack is not empty
this.arr[this.topOfStack]=Integer.MIN_VALUE;
this.topOfStack--;
}
// function to see the topmost element of stack
// Time Complexity - O(1), Space Complexity - O(1)
public void peek(){
// check if there is any stack present or not
if(this.arr==null){
System.out.println("No stack present, first create a stack");
return;
}
// check if stack is empty
if(topOfStack==-1){
System.out.println("Stack is empty, can't peek");
return;
}
// if stack is not empty
System.out.println("Top of the stack: "+this.arr[this.topOfStack]);
}
// function to traverse the stack array
// Time Complexity - O(n), Space Complexity - O(1)
public void traverseStack(){
// check if there is any stack present or not
if(this.arr==null){
System.out.println("No stack present, first create a stack");
return;
}
// check if stack is empty
if(this.topOfStack==-1){
System.out.println("Stack is empty, can't traverse");
return;
}
// if stack is not empty
System.out.println("Printing stack...");
for(int i=0;i<=this.topOfStack;i++){
System.out.print(this.arr[i]+" ");
}
System.out.println();
}
// function to delete whole stack
// Time Complexity - O(1), Space Complexity - O(1)
public void deleteStack(){
// check if there is any stack present or not
if(this.arr==null){
System.out.println("No stack present, first create a stack");
return;
}
// if stack is present
topOfStack=-1;
arr=null;
System.out.println("Stack deleted successfully");
}
public static void main(String[] args) throws Exception {
StackUsingArray stack = new StackUsingArray(5);
stack.initializeArray();
stack.peek();
stack.pop();
stack.traverseStack();
stack.push(1);
stack.traverseStack();
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(2);
stack.push(6);
stack.traverseStack();
stack.pop();
stack.traverseStack();
stack.peek();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.traverseStack();
stack.deleteStack();
stack.push(1);
stack.traverseStack();
}
}
/* ================== OUTPUT ====================
Stack is empty, can't peek
Stack is empty, can't pop from stack
Stack is empty, can't traverse
Printing stack...
1
Stack is full, Can't push into stack
Printing stack...
1 2 3 4 2
Printing stack...
1 2 3 4
Top of the stack: 4
Stack is empty, can't pop from stack
Stack is empty, can't traverse
Stack deleted successfully
No stack present, first create a stack
No stack present, first create a stack
==================================================*/