210 likes | 345 Views
CHAPTER 6. Stacks. Stacks. A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first element to be removed A stack is LIFO – last in, first out. A conceptual view of a stack. Basic operations.
E N D
CHAPTER 6 Stacks
Stacks • A stackis a linear collection whose elements are added and removed from one end • The last element to be put on the stack is the first element to be removed • A stack is LIFO – last in, first out
Basic operations • Push: store a data item at the top of the stack • Pop: retrieve a data item from the top of the stack
Item 3 Item 2 Item 1 Item 4 Item 2 Item 1 Item 2 Item 1 Item 2 Item 1 Item 1 Example • Push Item 1 • Push Item 2 • Push Item 3 • Pop • Push Item 4 • Pop • Pop • Pop
ADT definition of STACK • Notation: • S stack • e item of same type as the elements of S • b boolean value
Operations • InitStack(S) Procedure to initialize S to an empty stack • Preconditions: none • Postconditions: S empty
Operations • Push(S,e) • Procedure to place an item e into S (if there is room, i.e. S is not full) • Preconditions: S not full • Postconditions: size of S increased by 1 • Pop(S) e • Procedure to remove and return the last item stored in S (the top element) if S is not empty • Preconditions: S not empty • Postconditions: size of S decreased by 1
Operations • Peek(S) e Procedure to return (without removing) the last item stored in S (the top element) if S is not empty • Preconditions: S not empty • Postconditions: S not changed
Operations • IsEmpty(S) b Boolean function that returns TRUE if S is empty • Preconditions: none • Postconditions: S not changed
Stack AXIOMS • s.InitStack().IsEmpty() = true • s.MakeEmpty().IsEmpty() = true • s.Push(g).IsEmpty() = false • s.Push(g).Pop() = s • s.Peek() = s
Example • A procedure to replace the elements of a nonempty stack, assuming they are of type integers, with their sum • Pre: A nonempty stack with elements of type integers. • Post: s contains only one element - the sum of previously stored elements.
Algorithm 1. element1 pop(S) 2. while stack is not empty repeat 2.1. element2 pop(S) 2.2. push(S,element1+element2) 2.3. element1 pop(S) 3. push(S,element1)
Stack applications • Undo operations in editors • Evaluating an arithmetic expression using postfix notation • Converting infix expression into postfix expression • Depth-first search in a tree/graph
Stack implementation • The interface class • Linked implementation • Array implementation
The interface class public interface StackADT<T> { // Adds one element to the top of this stack public void push (T element); // Removes and returns the top element from this stack public T pop(); // Returns without removing the top element of this stack public T peek(); // Returns true if this stack contains no elements public boolean isEmpty(); // Returns the number of elements in this stack public int size(); // Returns a string representation of this stack public String toString(); }
Linked implementation Internally, a stack is represented as a linked list of nodes, with a reference to the top of the stack and an integer count of the number of nodes in the stack LinearNode class is reused to define the nodes
Array implementation • Design decisions: • maintain an array of Object references • the bottom of the stack is at index 0 • the elements of the stack are in order and contiguous • an integer variable top stores the index of the next available slot in the array • This approach allows the stack to grow and shrink at the higher indexes