|
| 1 | +package me.ramswaroop.linkedlists; |
| 2 | + |
| 3 | +import me.ramswaroop.common.DoubleLinkedNode; |
| 4 | + |
| 5 | +import java.util.EmptyStackException; |
| 6 | + |
| 7 | +import static java.lang.System.out; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by IntelliJ IDEA. |
| 11 | + * |
| 12 | + * @author: ramswaroop |
| 13 | + * @date: 7/24/15 |
| 14 | + * @time: 1:06 PM |
| 15 | + * <p/> |
| 16 | + * <p/> |
| 17 | + * Implement a stack with below operations in O(1) time complexity: |
| 18 | + * 1) push() which adds an element to the top of stack. |
| 19 | + * 2) pop() which removes an element from top of stack. |
| 20 | + * 3) findMiddle() which will return middle element of the stack. |
| 21 | + * 4) deleteMiddle() which will delete the middle element. |
| 22 | + * Push and pop are standard stack operations. |
| 23 | + * |
| 24 | + * The idea is to use a double linked list to represent a stack with pointer pointing to the middle node. |
| 25 | + */ |
| 26 | +public class StackWithOperationOnMiddleElement<E extends Comparable<E>> { |
| 27 | + |
| 28 | + int i = -1; |
| 29 | + DoubleLinkedNode<E> top, mid; |
| 30 | + |
| 31 | + public void push(E item) { |
| 32 | + |
| 33 | + top = new DoubleLinkedNode<>(null, item, top); |
| 34 | + if (top.next != null) { |
| 35 | + top.next.prev = top; |
| 36 | + } else { |
| 37 | + mid = top; |
| 38 | + } |
| 39 | + |
| 40 | + if (i % 2 == 0) { |
| 41 | + mid = mid.prev; |
| 42 | + } |
| 43 | + i++; |
| 44 | + } |
| 45 | + |
| 46 | + public E pop() { |
| 47 | + if (top == null) { |
| 48 | + throw new EmptyStackException(); |
| 49 | + } |
| 50 | + |
| 51 | + DoubleLinkedNode<E> topNode = top; |
| 52 | + if (top.next != null) { |
| 53 | + top.next.prev = null; |
| 54 | + } |
| 55 | + top = top.next; |
| 56 | + |
| 57 | + i++; |
| 58 | + if (i % 2 == 0) { |
| 59 | + mid = mid.next; |
| 60 | + } |
| 61 | + |
| 62 | + return topNode.item; |
| 63 | + } |
| 64 | + |
| 65 | + public E getMiddleElement() { |
| 66 | + if (mid == null) { |
| 67 | + throw new EmptyStackException(); |
| 68 | + } |
| 69 | + return mid.item; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Prints the content of the stack. |
| 74 | + */ |
| 75 | + public void print() { |
| 76 | + DoubleLinkedNode<E> curr = top; |
| 77 | + out.print("["); |
| 78 | + if (curr == null) { |
| 79 | + out.println("]"); |
| 80 | + return; |
| 81 | + } |
| 82 | + // prints the list from first node |
| 83 | + while (curr.next != null) { |
| 84 | + out.print(curr.item.toString() + ","); |
| 85 | + curr = curr.next; |
| 86 | + } |
| 87 | + out.println(curr.item.toString() + "]"); |
| 88 | + // prints the list from last node |
| 89 | + out.print("["); |
| 90 | + while (curr.prev != null) { |
| 91 | + out.print(curr.item.toString() + ","); |
| 92 | + curr = curr.prev; |
| 93 | + } |
| 94 | + out.println(curr.item.toString() + "]"); |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String a[]) { |
| 98 | + StackWithOperationOnMiddleElement<Integer> stack = new StackWithOperationOnMiddleElement<>(); |
| 99 | + stack.push(2); |
| 100 | + stack.push(3); |
| 101 | + stack.push(4); |
| 102 | + stack.push(5); |
| 103 | + stack.push(6); |
| 104 | + stack.print(); |
| 105 | + System.out.println("Mid: " + stack.getMiddleElement()); |
| 106 | + stack.pop(); |
| 107 | + stack.print(); |
| 108 | + System.out.println("Mid: " + stack.getMiddleElement()); |
| 109 | + stack.push(7); |
| 110 | + stack.print(); |
| 111 | + System.out.println("Mid: " + stack.getMiddleElement()); |
| 112 | + stack.pop(); |
| 113 | + stack.pop(); |
| 114 | + stack.pop(); |
| 115 | + stack.print(); |
| 116 | + System.out.println("Mid: " + stack.getMiddleElement()); |
| 117 | + } |
| 118 | +} |
0 commit comments