@@ -17,12 +17,12 @@ public static void main(String[] args) { |
17 | 17 | stack.push(4); |
18 | 18 | stack.push(5); |
19 | 19 |
|
20 | | - stack.printStack(); |
| 20 | + System.out.println(stack); |
21 | 21 |
|
22 | 22 | System.out.println("Size of stack currently is: " + stack.getSize()); |
23 | 23 |
|
24 | | - stack.pop(); |
25 | | - stack.pop(); |
| 24 | + assert stack.pop() == 5; |
| 25 | + assert stack.pop() == 4; |
26 | 26 |
|
27 | 27 | System.out.println("Top element of stack currently is: " + stack.peek()); |
28 | 28 | } |
@@ -48,65 +48,95 @@ public Node(int data) { |
48 | 48 |
|
49 | 49 | class LinkedListStack { |
50 | 50 |
|
51 | | - Node head = null; |
| 51 | + /** |
| 52 | + * Top of stack |
| 53 | + */ |
| 54 | + Node head; |
| 55 | + |
| 56 | + /** |
| 57 | + * Size of stack |
| 58 | + */ |
| 59 | + private int size; |
| 60 | + |
| 61 | + /** |
| 62 | + * Init properties |
| 63 | + */ |
| 64 | + public LinkedListStack() { |
| 65 | + head = null; |
| 66 | + size = 0; |
| 67 | + } |
52 | 68 |
|
53 | | - public void push(int x) { |
54 | | - Node n = new Node(x); |
55 | | - if (head == null) { |
56 | | - head = n; |
57 | | - } else { |
58 | | - Node temp = head; |
59 | | - n.next = temp; |
60 | | - head = n; |
61 | | - } |
| 69 | + /** |
| 70 | + * Add element at top |
| 71 | + * |
| 72 | + * @param x to be added |
| 73 | + * @return <tt>true</tt> if add successfully |
| 74 | + */ |
| 75 | + public boolean push(int x) { |
| 76 | + Node newNode = new Node(x); |
| 77 | + newNode.next = head; |
| 78 | + head = newNode; |
| 79 | + size++; |
| 80 | + return true; |
62 | 81 | } |
63 | 82 |
|
64 | | - public void pop() { |
65 | | - if (head == null) { |
66 | | - System.out.println("Empty stack. Nothing to pop"); |
| 83 | + /** |
| 84 | + * Pop element at top of stack |
| 85 | + * |
| 86 | + * @return element at top of stack |
| 87 | + * @throws NoSuchElementException if stack is empty |
| 88 | + */ |
| 89 | + public int pop() { |
| 90 | + if (size == 0) { |
| 91 | + throw new NoSuchElementException("Empty stack. Nothing to pop"); |
67 | 92 | } |
68 | | - |
69 | | - Node temp = head; |
| 93 | + Node destroy = head; |
70 | 94 | head = head.next; |
71 | | - System.out.println("Popped element is: " + temp.data); |
| 95 | + int retValue = destroy.data; |
| 96 | + destroy = null; // clear to let GC do it's work |
| 97 | + size--; |
| 98 | + return retValue; |
72 | 99 | } |
73 | 100 |
|
| 101 | + /** |
| 102 | + * Peek element at top of stack |
| 103 | + * |
| 104 | + * @return element at top of stack |
| 105 | + * @throws NoSuchElementException if stack is empty |
| 106 | + */ |
74 | 107 | public int peek() { |
75 | | - if (head == null) { |
76 | | - return -1; |
| 108 | + if (size == 0) { |
| 109 | + throw new NoSuchElementException("Empty stack. Nothing to pop"); |
77 | 110 | } |
78 | 111 | return head.data; |
79 | 112 | } |
80 | 113 |
|
81 | | - public void printStack() { |
82 | | - Node temp = head; |
83 | | - System.out.println("Stack is printed as below: "); |
84 | | - while (temp != null) { |
85 | | - if (temp.next == null) { |
86 | | - System.out.print(temp.data); |
87 | | - } else { |
88 | | - System.out.print(temp.data + " -> "); |
89 | | - } |
90 | | - temp = temp.next; |
| 114 | + @Override |
| 115 | + public String toString() { |
| 116 | + Node cur = head; |
| 117 | + StringBuilder builder = new StringBuilder(); |
| 118 | + while (cur != null) { |
| 119 | + builder.append(cur.data).append("->"); |
| 120 | + cur = cur.next; |
91 | 121 | } |
92 | | - System.out.println(); |
| 122 | + return builder.replace(builder.length() - 2, builder.length(), "").toString(); |
93 | 123 | } |
94 | 124 |
|
| 125 | + /** |
| 126 | + * Check if stack is empty |
| 127 | + * |
| 128 | + * @return <tt>true</tt> if stack is empty, otherwise <tt>false</tt> |
| 129 | + */ |
95 | 130 | public boolean isEmpty() { |
96 | | - return head == null; |
| 131 | + return size == 0; |
97 | 132 | } |
98 | 133 |
|
| 134 | + /** |
| 135 | + * Return size of stack |
| 136 | + * |
| 137 | + * @return size of stack |
| 138 | + */ |
99 | 139 | public int getSize() { |
100 | | - if (head == null) |
101 | | - return 0; |
102 | | - else { |
103 | | - int size = 1; |
104 | | - Node temp = head; |
105 | | - while (temp.next != null) { |
106 | | - temp = temp.next; |
107 | | - size++; |
108 | | - } |
109 | | - return size; |
110 | | - } |
| 140 | + return size; |
111 | 141 | } |
112 | 142 | } |