forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackWithFindMiddle.java
More file actions
176 lines (145 loc) · 4.46 KB
/
StackWithFindMiddle.java
File metadata and controls
176 lines (145 loc) · 4.46 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
package com.geeksforgeeks.stack;
import static com.util.LogUtil.logIt;
@SuppressWarnings("Duplicates")
public class StackWithFindMiddle {
DLLNode top;
DLLNode middle;
int size;
public StackWithFindMiddle() {
size = 0;
}
// Helper class for Stack with Find Middle and Delete Middle operation
class DLLNode {
DLLNode prev;
DLLNode next;
int data;
public DLLNode(int data) {
this.data = data;
}
}
/**
* 1) Simple Rule for Pushing , the first element entered will be out last so for the first element there is no nextElement
* but only previous element, think of this as a Stack of Trays placed, the last plate has no next and first plate has no previous
* <p>
* | |
* |77| <--- = Top Element
* |66|
* |55|
* |44|
* |33|
* |22|
* |11| <---- = First Element Inserted
* ----
* <p>
* So how we will push after first element.
*
* @param data
*/
public void push(int data) {
logIt("Pushing Element " + data);
DLLNode newNode = new DLLNode(data);
if (top == null) {
top = newNode; // Since this is the first element so the TOP point to it.
size++;
middle = top; // If only 1 element hence middle element point to it.
} else {
DLLNode temp = top;
temp.prev = newNode;
temp.prev.next = temp;
top = temp.prev;
size++;
}
if (size > 1 && size % 2 != 0) {
middle = middle.prev;
}
}
public int pop() {
logIt("Popping Element " + top.data);
// If No Item in stack
if (top == null) {
System.out.println("Stack Underflow");
return 0;
}
// If only 1 item in stack
if (top.next == null) {
DLLNode temp = top;
top = middle = null;
--size;
return temp.data;
}
DLLNode temp = top;
temp.next.prev = null;
size--;
top = temp.next;
if (size % 2 == 0) {
middle = middle.next;
}
return temp.data;
}
public void printStack() {
logIt("Middle Element in Stack of size [" + size + "] is ");
DLLNode temp = top;
while (temp != null) {
System.out.print(temp.data);
if (temp.data == middle.data) {
System.out.println(" <========== middle element");
} else {
System.out.println();
}
temp = temp.next;
}
}
public DLLNode findMiddle() {
printStack();
return middle;
}
public void deleteMiddle() {
logIt("Deleting the current middle Element ==> " + middle.data, true);
if (size == 1) {
logIt("This is the only node present in the Stack which is " + top.data);
--size;
top = middle = null;
} else {
DLLNode prev = middle.prev;
DLLNode next = middle.next;
prev.next = next;
next.prev = prev;
--size;
if (size % 2 == 0) { // After deletion total items inside Stack is Even.
middle = next;
} else {
middle = prev;
}
}
}
public static void main(String[] args) {
StackWithFindMiddle stack = new StackWithFindMiddle();
// Middle Element [Assuming 0 based index]
// int mid = firstElementIndex + (lastElementIndex - firstElementIndex) / 2;
stack.push(11);
stack.push(22);
stack.push(33);
stack.push(44);
stack.push(55);
stack.push(66);
stack.push(77);
logIt("Middle Element is " + stack.findMiddle().data, true);
stack.deleteMiddle();
logIt("Middle Element is " + stack.findMiddle().data, true);
stack.deleteMiddle();
stack.findMiddle();
stack.push(88);
stack.findMiddle();
stack.pop();
stack.findMiddle();
stack.pop();
stack.findMiddle();
// logIt("Middle Element is " + stack.findMiddle().data, true);
// System.out.println("Item popped is " + stack.pop());
//
// logIt("Middle Element is " + stack.findMiddle().data, true);
// System.out.println("Item popped is " + stack.pop());
//
// logIt("Middle Element is " + stack.findMiddle().data, true);
}
}