-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.java
More file actions
230 lines (199 loc) · 5.93 KB
/
Copy pathLinkedStack.java
File metadata and controls
230 lines (199 loc) · 5.93 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
public class LinkedStack implements Stack {
private int size;
private Node top;
private static class Node {
Object data;
Node next;
Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
public boolean isEmpty() {
return (size == 0);
}
public int size() {
return size;
}
public Object peek() {
if (size == 0) throw new IllegalStateException("Stack is empty");
return top.data;
}
public void push(Object obj) {
top = new Node(obj, top);
++size;
}
public Object pop() {
if (size == 0) throw new IllegalStateException("Stack is empty");
Object temp = top.data;
top = top.next;
--size;
return temp;
}
public Object getBottom() {
if (size == 0) throw new IllegalStateException("Stack is empty");
Node temp = top;
while (temp.next != null) {
temp = temp.next;
}
return temp.data;
}
public Object getMiddle(){
if (size == 0) throw new IllegalStateException("Stack is empty");
Node temp = top;
int midIndex = size / 2;
for (int i = 0; i < midIndex; i++) {
temp = temp.next;
}
return temp.data;
}
public void reverse(){
// if (size <= 1) {
// return;
// }
// Node prev = null;
// Node current = top;
// Node next = null;
// while (current != null) {
// next = current.next;
// current.next = prev;
// prev = current;
// current = next;
// }
// top = prev;
// -----------------------------------------------
if (size <= 1) {
return;
}
Object[] tempArray = new Object[size];
Node p = top;
for (int i = 0; i < size; i++){
tempArray[i] = p.data;
p = p.next;
}
p = top;
for (int i = size - 1; i >= 0; i--){
p.data = tempArray[i];
p = p.next;
}
// -----------------------------------------------
//if (size <= 1) return;
//LinkedStack temp1 = new LinkedStack();
//LinkedStack temp2 = new LinkedStack();
//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");
Node p = top;
int max = (int) p.data;
while (p != null){
if ((int) p.data > max){
max = (int) p.data;
}
p = p.next;
}
return max;
}
public int getmin(){
if (size == 0) throw new IllegalStateException("Stack is Empty");
Node p = top;
int min = (int) p.data;
while (p != null){
if((int) p.data < min){
min = (int) p.data;
}
p = p.next;
}
return min;
}
@Override
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("[");
Node p = top;
while (p != null){
sb.append(p.data);
if (p.next != null){
sb.append(", ");
}
p = p.next;
}
sb.append("]");
return sb.toString();
}
// ---- Ye Code Perfectly Work Karta Hai, Lekin Ye LinkedStack ko ArrayStack me Convert Karta Hai, Isliye Use Nahi Kiya. ----
//
// public ArrayStack toArrayStack(){
// ArrayStack temp = new ArrayStack(this.size);
// for (Node i = this.top; i != null; i = i.next){
// temp.push(i.data);
// }
// ArrayStack as = new ArrayStack(temp.size());
// for (int j=0 ; j<temp.size(); j++){
// as.push(temp.pop());
// }
// return as;
// }
public boolean equals(LinkedStack ls){
if (this.size != ls.size){
return false;
}
Node q = ls.top;
for (Node i= this.top; i != null; i = i.next,q = q.next){
if (!i.data.equals(q.data)){
return false;
}
}
return true;
}
public LinkedStack[] split(){
LinkedStack[] ls = new LinkedStack[2];
ls[0] = new LinkedStack();
ls[1] = new LinkedStack();
LinkedStack temp = new LinkedStack();
for (Node i = this.top; i != null; i = i.next){
temp.push(i.data);
}
int s = this.size;
int mid = s / 2;
for (int i = 0; i < mid; i++){
ls[0].push(temp.pop());
}
for (int i = mid; i < s; i++){
ls[1].push(temp.pop());
}
return ls;
}
public LinkedStack merge(LinkedStack ls){
LinkedStack merged = new LinkedStack();
LinkedStack temp1 = new LinkedStack();
LinkedStack temp2 = new LinkedStack();
for (Node p = this.top; p != null; p = p.next){
temp1.push(p.data);
}
for (Node q = ls.top; q != null; q = q.next){
temp2.push(q.data);
}
for (int i = 0; i < this.size; i++){
merged.push(temp1.pop());
}
for (int j = 0; j < ls.size; j++){
merged.push(temp2.pop());
}
return merged;
}
public LinkedStack Clone(LinkedStack ls){
LinkedStack temp = new LinkedStack();
LinkedStack cloned = new LinkedStack();
int s = ls.size;
for (Node i = ls.top; i != null; i = i.next){
temp.push(i.data);
}
for (int i = 0; i < s; i++){ // --> for (int i = 0; i < ls.size; i++) is also correct agr upar "int s = ls.size;" na krte!!
cloned.push(temp.pop());
}
return cloned;
}
}