update StackOfLinkedList

This commit is contained in:
shellhub
2019-10-18 17:39:32 +08:00
parent 23d9eaee7f
commit bb9e082aa9

View File

@ -17,12 +17,12 @@ class StackOfLinkedList {
stack.push(4); stack.push(4);
stack.push(5); stack.push(5);
stack.printStack(); System.out.println(stack);
System.out.println("Size of stack currently is: " + stack.getSize()); System.out.println("Size of stack currently is: " + stack.getSize());
stack.pop(); assert stack.pop() == 5;
stack.pop(); assert stack.pop() == 4;
System.out.println("Top element of stack currently is: " + stack.peek()); System.out.println("Top element of stack currently is: " + stack.peek());
} }
@ -48,65 +48,95 @@ class Node {
class LinkedListStack { class LinkedListStack {
Node head = null; /**
* Top of stack
*/
Node head;
public void push(int x) { /**
Node n = new Node(x); * Size of stack
if (head == null) { */
head = n; private int size;
} else {
Node temp = head; /**
n.next = temp; * Init properties
head = n; */
} public LinkedListStack() {
head = null;
size = 0;
} }
public void pop() { /**
if (head == null) { * Add element at top
System.out.println("Empty stack. Nothing to pop"); *
* @param x to be added
* @return <tt>true</tt> if add successfully
*/
public boolean push(int x) {
Node newNode = new Node(x);
newNode.next = head;
head = newNode;
size++;
return true;
} }
Node temp = head; /**
* Pop element at top of stack
*
* @return element at top of stack
* @throws NoSuchElementException if stack is empty
*/
public int pop() {
if (size == 0) {
throw new NoSuchElementException("Empty stack. Nothing to pop");
}
Node destroy = head;
head = head.next; head = head.next;
System.out.println("Popped element is: " + temp.data); int retValue = destroy.data;
destroy = null; // clear to let GC do it's work
size--;
return retValue;
} }
/**
* Peek element at top of stack
*
* @return element at top of stack
* @throws NoSuchElementException if stack is empty
*/
public int peek() { public int peek() {
if (head == null) { if (size == 0) {
return -1; throw new NoSuchElementException("Empty stack. Nothing to pop");
} }
return head.data; return head.data;
} }
public void printStack() { @Override
Node temp = head; public String toString() {
System.out.println("Stack is printed as below: "); Node cur = head;
while (temp != null) { StringBuilder builder = new StringBuilder();
if (temp.next == null) { while (cur != null) {
System.out.print(temp.data); builder.append(cur.data).append("->");
} else { cur = cur.next;
System.out.print(temp.data + " -> ");
} }
temp = temp.next; return builder.replace(builder.length() - 2, builder.length(), "").toString();
}
System.out.println();
} }
/**
* Check if stack is empty
*
* @return <tt>true</tt> if stack is empty, otherwise <tt>false</tt>
*/
public boolean isEmpty() { public boolean isEmpty() {
return head == null; return size == 0;
} }
/**
* Return size of stack
*
* @return size of stack
*/
public int getSize() { public int getSize() {
if (head == null)
return 0;
else {
int size = 1;
Node temp = head;
while (temp.next != null) {
temp = temp.next;
size++;
}
return size; return size;
} }
} }
}