-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
220 lines (192 loc) · 5.72 KB
/
Copy pathArrayStack.java
File metadata and controls
220 lines (192 loc) · 5.72 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
public class ArrayStack implements Stack {
private int size;
private Object[] a;
public ArrayStack(int capacity) {
a = new Object[capacity];
}
public boolean isEmpty() {
return (size == 0);
}
public int size() {
return size;
}
public Object peek() {
if (size == 0) throw new IllegalStateException("Stack is empty");
return a[size - 1];
}
public void push(Object obj) {
if (size == a.length) {
resize();
}
a[size++] = obj;
}
public Object pop() {
if (size == 0) throw new IllegalStateException("Stack is empty");
Object temp = a[--size];
a[size] = null; // Memory free karne ke liye
return temp;
}
private void resize() {
Object[] aa = a;
a = new Object[2 * aa.length];
System.arraycopy(aa, 0, a, 0, size);
}
public Object getBottom() {
if (size == 0) throw new IllegalStateException("Stack is empty");
return a[0];
}
public Object getMiddle(){
if (size == 0) throw new IllegalStateException("Stack is empty");
int midIndex = size / 2;
return a[midIndex];
}
public void reverse(){
// if (size<=1){
// return;
// }
// int start = 0;
// int end = size - 1;
// while (start < end){
// Object temp = a[start];
// a[start] = a[end];
// a[end] = temp;
// start++;
// end--;
// }
// -----------------------------------------------
if (size <= 1) {
return;
}
int originalSize = this.size;
ArrayStack temp1 = new ArrayStack(this.a.length);
while (this.size > 0) {
temp1.push(this.pop());
}
System.arraycopy(temp1.a, 0, this.a, 0, originalSize);
this.size = originalSize;
// -----------------------------------------------
// if (size <= 1) {
// return;
// }
// ArrayStack temp1 = new ArrayStack(this.a.length);
// ArrayStack temp2 = new ArrayStack(this.a.length);
// while (this.size() > 0) {
// temp1.push(this.pop());
// }
// while (temp1.size() > 0) {
// temp2.push(temp1.pop());
// }
// while (temp2.size() > 0) {
// this.push(temp2.pop());
// }
}
public int getmax(){
if (size == 0) throw new IllegalStateException("Stack is empty");
// ArrayStack tempStack = new ArrayStack(this.a.length);
// int max = (int) this.pop();
// tempStack.push(max);
// while (this.size > 0){
// int current = (int) this.pop();
// if (current > max){
// max = current;
// }
// tempStack.push(current);
// }
// while (tempStack.size > 0){
// this.push(tempStack.pop());
// }
// return max;
int max = (int) a[0];
for (int i = 1; i < size; i++){
if ((int) a[i] > max){
max = (int) a[i];
}
}
return max;
}
public int getmin(){
if (size == 0) throw new IllegalStateException("Stack is empty");
int min = (int) a[0];
for (int i = 1; i < size ; i++){
if ((int) a[i] < min){
min = (int) a[i];
}
}
return min;
}
@Override
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append ("[ ");
for (int i = 0; i < size; i++){
sb.append(a[i]);
if (i < size - 1){
sb.append(", ");
}
}
sb.append(" ]");
return sb.toString();
}
// public LinkedStack toLinkedStack(){
// LinkedStack temp = new LinkedStack();
// for (int i = 0; i < this.size; i++){
// temp.push(a[i]);
// }
// return temp;
// }
public boolean equals(ArrayStack arr){
if (this.size != arr.size)
return false;
for (int i = 0; i < this.size; i++){
if (!this.a[i].equals(arr.a[i])){
return false;
}
}
return true;
}
public ArrayStack[] split(){
ArrayStack[] arr = new ArrayStack[2];
int mid = size / 2;
int s = size;
arr[0] = new ArrayStack(mid);
arr[1] = new ArrayStack(s - mid);
for (int i = 0; i < mid; i++){
arr[0].push(a[i]);
}
for (int i = mid; i < s ; i++){
arr[1].push(a[i]);
}
return arr;
}
// Ye neeche waale process m stack khali ho jaayega, isliye use nahi kiya.
// public ArrayStack[] split(ArrayStack arr){
// int s = arr.size();
// int mid = s / 2;
// ArrayStack a1 = new ArrayStack(size);
// ArrayStack a2 = new ArrayStack(size);
// for (int i = 0; i < mid; i++){
// a1.push(arr.pop());
// }
// for (int i = mid; i < s; i++){
// a2.push(arr.pop());
// }
// return new ArrayStack[]{a1, a2};
// }
public ArrayStack merge(ArrayStack arr){
ArrayStack mergedStack = new ArrayStack(this.size + arr.size);
for (int i = 0; i < this.size; i++){
mergedStack.push(this.a[i]);
}
for (int i = 0; i < arr.size; i++){
mergedStack.push(arr.a[i]);
}
return mergedStack;
}
public ArrayStack clone(ArrayStack arr){
ArrayStack cloned = new ArrayStack(arr.size);
for (int i = 0; i < arr.size; i++){
cloned.push(arr.a[i]);
}
return cloned;
}
}