Upload
annona
View
22
Download
1
Tags:
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
CHAPTER 6
Stacks
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
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 Pop Pop
Item 1
Item 2Item 1
Item 3Item 2Item 1
Item 2Item 1
Item 4Item 2Item 1
6
More operations on a stack
7
ADT definition of STACK
Notation: S stack e item of same type as the elements of S b boolean 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 classLinked implementationArray implementation
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();}
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
19
Linked implementation
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
21
Array implementation