JavaAlgorithms/DataStructures/Stacks/StackOfLinkedList.java

145 lines
3.0 KiB
Java
Raw Normal View History

package DataStructures.Stacks;
2020-01-29 00:18:15 +08:00
import java.util.NoSuchElementException;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
// An implementation of a Stack using a Linked List
class StackOfLinkedList {
public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
2018-07-25 22:37:10 +08:00
stack.push(5);
2019-10-18 17:39:32 +08:00
System.out.println(stack);
System.out.println("Size of stack currently is: " + stack.getSize());
2019-10-18 17:39:32 +08:00
assert stack.pop() == 5;
assert stack.pop() == 4;
2018-07-25 22:37:10 +08:00
System.out.println("Top element of stack currently is: " + stack.peek());
}
}
// A node class
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
/**
* A class which implements a stack using a linked list
2019-03-05 10:18:20 +08:00
* <p>
* Contains all the stack methods : push, pop, printStack, isEmpty
**/
class LinkedListStack {
2019-10-18 17:39:32 +08:00
/**
* Top of stack
*/
Node head;
/**
* Size of stack
*/
private int size;
/**
* Init properties
*/
public LinkedListStack() {
head = null;
size = 0;
}
2019-10-18 17:39:32 +08:00
/**
* Add element at top
*
* @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;
}
2019-10-18 17:39:32 +08:00
/**
* 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");
}
2019-10-18 17:39:32 +08:00
Node destroy = head;
head = head.next;
2019-10-18 17:39:32 +08:00
int retValue = destroy.data;
destroy = null; // clear to let GC do it's work
size--;
return retValue;
}
2019-10-18 17:39:32 +08:00
/**
* Peek element at top of stack
*
* @return element at top of stack
* @throws NoSuchElementException if stack is empty
*/
2018-07-25 22:37:10 +08:00
public int peek() {
2019-10-18 17:39:32 +08:00
if (size == 0) {
throw new NoSuchElementException("Empty stack. Nothing to pop");
2019-03-05 10:18:20 +08:00
}
return head.data;
2018-07-25 22:37:10 +08:00
}
2019-10-18 17:39:32 +08:00
@Override
public String toString() {
Node cur = head;
StringBuilder builder = new StringBuilder();
while (cur != null) {
builder.append(cur.data).append("->");
cur = cur.next;
}
2019-10-18 17:39:32 +08:00
return builder.replace(builder.length() - 2, builder.length(), "").toString();
}
2019-10-18 17:39:32 +08:00
/**
* Check if stack is empty
*
* @return <tt>true</tt> if stack is empty, otherwise <tt>false</tt>
*/
public boolean isEmpty() {
2019-10-18 17:39:32 +08:00
return size == 0;
}
2019-10-18 17:39:32 +08:00
/**
* Return size of stack
*
* @return size of stack
*/
public int getSize() {
2019-10-18 17:39:32 +08:00
return size;
}
}