Stack Data Structure
Linked Stack Class - Push (Code)
// Adds the item to this stack. // @param item the item to add .....public void [method](Item item) { ..........Node<Item> oldfirst = first; // save old first ..........first = new Node<Item>(); // make new first ..........first.item = item; ..........first.next = oldfirst; // point new to old ..........n++; .....}
Linked Stack Class - Pop (Code)
// Removes and returns the item most recently added to this stack. // @return the item most recently added // @throws NoSuchElementException if this stack is empty .....public Item [method]() { ..........if (isEmpty()) throw new NoSuchElementException("Stack underflow"); ..........Item item = first.item; // save item to return ..........first = first.next; // delete first node ..........n--; ..........return item; // return the saved item .....}
Linked Stack Class - Peek (Code)
// Returns (but does not remove) the item most recently added to this stack. // @return the item most recently added to this stack // @throws NoSuchElementException if this stack is empty .....public Item [method]() { ..........if (isEmpty()) throw new NoSuchElementException("Stack underflow"); ..........return first.item; .....}
Linked Stack Class - ToString (Code)
// Returns a string representation of this stack. // @return the sequence of items in this stack in LIFO order, separated by spaces .....public String [method]() { ..........StringBuilder s = new StringBuilder(); ..........for (Item item : this) { ...............s.append(item); ...............s.append(' '); ..........} ..........return s.toString(); .....}
Linked Stack Class - Iterator (Code)
// Returns an iterator to this stack that iterates through the items in LIFO order. // @return an iterator to this stack that iterates through the items in LIFO order .....public Iterator<Item> iterator() { ..........return new ListIterator<Item>(first); .....} ..... // an iterator, doesn't implement remove() since it's optional .....private class ListIterator<Item> implements Iterator<Item> { ..........private Node<Item> current; .......... ..........public ListIterator(Node<Item> first) { ...............current = first; ..........} .......... ..........public boolean hasNext() { ...............return current != null; ..........} .......... ..........public void remove() { ...............throw new UnsupportedOperationException(); ..........} .......... ..........public Item next() { ...............if (!hasNext()) throw new NoSuchElementException(); ...............Item item = current.item; ...............current = current.next; ...............return item; ..........} .....}
Linked Stack Class - Implementation (Code)
public class Stack<Item> implements Iterable<Item>{ .....private Node<Item> first; // top of stack .....private int n; // size of the stack ..... .....// helper linked list class .....private static class Node<Item> { ..........private Item item; ..........private Node<Item> next; .....} ..... // Initializes an empty stack. .....public Stack() { ..........first = null; ..........n = 0; .....}
Peek/Top (Operation)
Returns top element of stack. Time is O(1).
Linked Stack Class - Basic Methods (Code)
// Returns true if this stack is empty. // @return true if this stack is empty; false otherwise .....public boolean isEmpty() { ..........return first == null; .....} ..... // Returns the number of items in this stack. // @return the number of items in this stack .....public int size() { ..........return n; .....}
Stack
A linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Push (Operation)
Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Time is O(1).
Pros and Cons to Array Implementation
Pros: Easy to implement. Memory is saved as pointers are not involved. Cons: It is not dynamic. It doesn't grow and shrink depending on needs at runtime.
Pros and Cons to Linked List Implementation
Pros: The linked list implementation of stack can grow and shrink according to the needs at runtime. Cons: Requires extra memory due to involvement of pointers.
Pop (Operation)
Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Time is O(1).
IsEmpty (Operation)
Returns true if stack is empty, else false. Time is O(1).