CHAPTER 6 Stacks. 2 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.

Presentation on theme: "CHAPTER 6 Stacks. 2 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."— Presentation transcript:

CHAPTER 6 Stacks

2 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

3 A conceptual view of a stack

4 Basic operations Push: store a data item at the top of the stack Pop: retrieve a data item from the top of the stack

5 Example Push Item 1 Push Item 2 Push Item 3 Pop Push Item 4 Pop Item 1 Item 2 Item 1 Item 3 Item 2 Item 1 Item 2 Item 1 Item 4 Item 2 Item 1

6 More operations on a stack

7 ADT definition of STACK Notation: Sstack eitem of same type as the elements of S bboolean value

8 Operations InitStack(S) Procedure to initialize S to an empty stack Preconditions: none Postconditions: S empty

9 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

10 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

11 Operations IsEmpty(S)  b Boolean function that returns TRUE if S is empty Preconditions: none Postconditions: S not changed

12 Stack AXIOMS s.InitStack().IsEmpty() = true s.MakeEmpty().IsEmpty() = true s.Push(g).IsEmpty() = false s.Push(g).Pop() = s s.Peek() = s

13 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.

14 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)

15 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

16 Stack implementation The interface class Linked implementation Array implementation

17 The interface class public interface StackADT { // 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(); }

18 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