Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.

Similar presentations


Presentation on theme: "1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first."— Presentation transcript:

1 1 CSCD 326 Data Structures I Stacks

2 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first out data structure all insertions are made at the top of the stack all removals are also made from the top of the stack Common uses of stacks: can used to reverse the order of input used to perform balanced symbol checking ( ) { } used to control program execution with function calls used for expression evaluation

3 3 ADT Stack Operations isEmpty boolean -determines whether stack is empty. push adds one item to the top of the stack. pop removes one item from the top of the stack. peek returns most recently inserted item. popAll removes all items from stack. isFull- boolean often not needed.

4 4 Linked List Stack Implementation Stacks can grow and shrink dynamically No particular linked list variations are required: The head pointer can also serve as the top of the stack. Pushing becomes inserting the first node in a linked list. Popping becomes removing the first node. All basic operations take place in O(1) time - or constant time complexity

5 5 Stack Interface public interface StackInterface { public boolean isEmpty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns true if the stack is empty; otherwise returns false. public boolean isFull(); // Determines whether the stack is full. // Precondition: None. // Postcondition: Returns true if the stack is full; otherwise returns false. public void push(Object newItem) throws StackException; // Adds an item to the top of a stack. // Precondition: newItem is the item to be added. // Postcondition: If insertion is successful, newItem is on the top of the stack. // Exception: Some implementations may throw // StackException when newItem cannot be placed on the stack.

6 6 Stack Interface (2) public Object pop() throws StackException; // Removes the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, the item // that was added most recently is removed from the stack and returned. // Exception: Throws StackException if the stack is empty. public void popAll(); // Removes all the items from the stack. // Precondition: None. // PostCondition: Stack is empty. public Object peek() throws StackException; // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, the item that was added most //recently is returned. The stack is unchanged. // Exception: Throws StackException if the stack is empty. } // end StackInterface

7 7 List Stack Implementation // Assumes that the classes ListInterface and Node are available public class ListStack implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public ListStack() { top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty public boolean isFull() { return false; } // end isFull

8 8 List Stack Implementation (2) public void push(Object newItem) { if (!isFull()) { Node temp = new Node(newItem); temp.setNext(top); top = temp; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop

9 9 List Stack Implementation (3) public void popAll() { while(!isEmpty()) pop(); } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end ListStack

10 10 Stack Exception public class StackException extends java.lang.RuntimeException { public StackException(String s) { super(s); } // end constructor } // end StackException

11 11 List Stack Test public class ListStackTest { public static void main(String[ ] args) { Integer items[] = new Integer[15]; System.out.println("Pushing:"); for (int i=0; i<15; i++) { items[i] = new Integer(i); if (!stack.isFull()) { System.out.print(" "+i); stack.push(items[i]); } // end if } // end for System.out.println("\nPopping:"); while (!stack.isEmpty()) { // cast result of pop to Integer System.out.print(" "+(Integer)(stack.pop())); } // end while System.out.println(); } // end main }

12 12 List Stack Test Output Pushing: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Popping: 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

13 13 Array Stack Implementation public class ArrayStack implements StackInterface { final int MAX_STACK = 50; // maximum size of stack private Object items[ ]; private int top; public ArrayStack() { items = new Object[MAX_STACK]; top = -1; } // end default constructor public boolean isEmpty() { return top < 0; } // end isEmpty public boolean isFull() { return top == MAX_STACK-1; } // end isFull

14 14 Array Stack Implementation (2) public void push(Object newItem) throws StackException { if (!isFull()) { items[++top] = newItem; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public void popAll() { items = new Object[MAX_STACK]; top = -1; } // end popAll

15 15 Array Stack Implementation (3) public Object pop() throws StackException { if (!isEmpty()) { return items[top--]; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop public Object peek() throws StackException { if (!isEmpty()) { return items[top]; } else { throw new StackException("Stack exception on " + "peek - stack empty"); } // end if } // end peek } // end ArrayStack

16 16 Array Stack Test public class ArrayStackTest { public static final int MAX_ITEMS = 15; public static void main(String[ ] args) { ArrayStack stack = new ArrayStack(); Integer items[ ] = new Integer[MAX_ITEMS]; System.out.println("Pushing:"); for (int i=0; i<MAX_ITEMS; i++) { items[i] = new Integer(i); if (!stack.isFull()) { System.out.print(" "+i); stack.push(items[i]); } // end if } // end for System.out.println("\nPopping:"); while (!stack.isEmpty()) { // cast result of pop to Integer System.out.print(" "+(Integer)(stack.pop())); } // end while System.out.println(); } // end main } // end ArrayStackTest

17 17 Array Stack Test Output Pushing: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Popping: 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0


Download ppt "1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first."

Similar presentations


Ads by Google