21
CHAPTER 6 Stacks

CHAPTER 6

  • Upload
    annona

  • View
    22

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CHAPTER 6

CHAPTER 6

Stacks

Page 2: CHAPTER 6

2

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

Page 3: CHAPTER 6

3

A conceptual view of a stack

Page 4: CHAPTER 6

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

Page 5: CHAPTER 6

5

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

Item 1

Item 2Item 1

Item 3Item 2Item 1

Item 2Item 1

Item 4Item 2Item 1

Page 6: CHAPTER 6

6

More operations on a stack

Page 7: CHAPTER 6

7

ADT definition of STACK

Notation: S stack e item of same type as the elements of S b boolean value

Page 8: CHAPTER 6

8

Operations

InitStack(S)

Procedure to initialize S to an empty stack

Preconditions: none Postconditions: S empty

Page 9: CHAPTER 6

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

Page 10: CHAPTER 6

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

Page 11: CHAPTER 6

11

Operations

IsEmpty(S) b

Boolean function that returns TRUE if S is empty

Preconditions: none Postconditions: S not changed

Page 12: CHAPTER 6

12

Stack AXIOMS

s.InitStack().IsEmpty() = true

s.MakeEmpty().IsEmpty() = true

s.Push(g).IsEmpty() = false

s.Push(g).Pop() = s

s.Peek() = s

Page 13: CHAPTER 6

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.

Page 14: CHAPTER 6

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)

Page 15: CHAPTER 6

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

Page 16: CHAPTER 6

16

Stack implementation

The interface classLinked implementationArray implementation

Page 17: CHAPTER 6

17

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();}

Page 18: CHAPTER 6

18

Linked implementation

Internally, a stack is represented as alinked 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

Page 19: CHAPTER 6

19

Linked implementation

Page 20: CHAPTER 6

20

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

Page 21: CHAPTER 6

21

Array implementation