forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackWithOperationOnMiddleElement.java
118 lines (103 loc) · 3.01 KB
/
StackWithOperationOnMiddleElement.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
package com.rampatra.linkedlists;
import com.rampatra.base.DoubleLinkedNode;
import java.util.EmptyStackException;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 7/24/15
* @time: 1:06 PM
* <p/>
* <p/>
* Implement a stack with below operations in O(1) time complexity:
* 1) push() which adds an element to the top of stack.
* 2) pop() which removes an element from top of stack.
* 3) findMiddle() which will return middle element of the stack.
* 4) deleteMiddle() which will delete the middle element.
* Push and pop are standard stack operations.
* <p>
* The idea is to use a double linked list to represent a stack with pointer pointing to the middle node.
*/
public class StackWithOperationOnMiddleElement<E extends Comparable<E>> {
int i = -1;
DoubleLinkedNode<E> top, mid;
public void push(E item) {
top = new DoubleLinkedNode<>(null, item, top);
if (top.next != null) {
top.next.prev = top;
} else {
mid = top;
}
if (i % 2 == 0) {
mid = mid.prev;
}
i++;
}
public E pop() {
if (top == null) {
throw new EmptyStackException();
}
DoubleLinkedNode<E> topNode = top;
if (top.next != null) {
top.next.prev = null;
}
top = top.next;
i++;
if (i % 2 == 0) {
mid = mid.next;
}
return topNode.item;
}
public E getMiddleElement() {
if (mid == null) {
throw new EmptyStackException();
}
return mid.item;
}
/**
* Prints the content of the stack.
*/
public void print() {
DoubleLinkedNode<E> curr = top;
out.print("[");
if (curr == null) {
out.println("]");
return;
}
// prints the list from first node
while (curr.next != null) {
out.print(curr.item.toString() + ",");
curr = curr.next;
}
out.println(curr.item.toString() + "]");
// prints the list from last node
out.print("[");
while (curr.prev != null) {
out.print(curr.item.toString() + ",");
curr = curr.prev;
}
out.println(curr.item.toString() + "]");
}
public static void main(String[] args) {
StackWithOperationOnMiddleElement<Integer> stack = new StackWithOperationOnMiddleElement<>();
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.print();
System.out.println("Mid: " + stack.getMiddleElement());
stack.pop();
stack.print();
System.out.println("Mid: " + stack.getMiddleElement());
stack.push(7);
stack.print();
System.out.println("Mid: " + stack.getMiddleElement());
stack.pop();
stack.pop();
stack.pop();
stack.print();
System.out.println("Mid: " + stack.getMiddleElement());
}
}