Stacks - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks

Description:

Stacks Chapter 3 Create Stack algorithm createStack Allocates memory for a stack head node from dynamic memory and returns its address to the caller. – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 37
Provided by: ValuedGate2341
Category:
Tags: parsing | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks
Chapter 3
2
Linear Lists
  • Operations are
  • Insertion
  • Deletion
  • Retrieval
  • Traversal (exception for restristed lists).

Figure 3-2
3
Stacks
A stack is a Last in First out (LIFO) data
structure in which all insertions and deletions
are restricted to one end called top.
Figure 4-1
4
Stack Basic Operations
Overflow?
Figure 4-2
5
Stack Basic Operations
Underflow?
Figure 4-3
6
Stack Basic Operations
Figure 4-4
7
Figure 4-5
8
Stack Data Structure
Figure 4-6
9
Stack Data Structure
Figure 4-7
10
Stack Operations
Figure 4-8, Part I
11
Stack Operations
Figure 4-8, Part II
12
Create Stack
  • algorithm createStack
  • Allocates memory for a stack head node from
    dynamic memory and returns its address to the
    caller.
  • Pre Nothing
  • Post Head node allocated or error returned
  • Return pointer to head node or null pointer if no
    memory
  • if (memory available)
  • allocate (stackPtr)
  • stackPtr?count 0
  • stackPtr?top null
  • else
  • stackPtr null
  • return stackPtr
  • end createStack

13
Push Stack
Figure 4-9
14
Push Stack
  • algorithm pushStack(val stack lthead pointergt,
  • val data ltdata typegt)
  • Insert (push) one item into the stack.
  • Pre stack is a pointer to the stack head
    structure.
  • data contains data to be pushed into stack.
  • Post data have been pushed in stack.
  • if (stack full)
  • success false
  • else
  • allocate (newPtr)
  • newPtr?data data
  • newPtr?next stack?top
  • stack?topnewPtr
  • stack?count stack?count1
  • Successtrue
  • return success
  • end pushStack

15
Pop Stack
Figure 4-10
16
Pop Stack
  • algorithm popStack(val stack lthead pointergt,
  • val dataOut ltdata typegt)
  • Pops the item on the top of the stack and returns
    it to the user.
  • Pre stack is a pointer to the stack head
    structure.
  • dataOut is a reference variable to receive
    the data.
  • Post data have been returned to the calling
    algorithm.
  • if (stack empty)
  • success false
  • else
  • dltPtr stack?top
  • dataOut stack?top?data
  • stack?top stack?top?next
  • stack?count stack?count 1
  • recycle(dltPtr)
  • successtrue
  • return success
  • end popStack

17
Destroy Stack
  • algorithm destroyStack(val stack lthead pointergt)
  • This algorithm releases all nodes back to the
    dynamic memory.
  • Pre stack is a pointer to the stack head
    structure.
  • Post stack empty and all nodes recycled
  • if (stack not empty)
  • loop
  • temp stack?top
  • stack?top stack?top?link
  • recycled(temp)
  • recycled (stack)
  • return null pointer
  • end destroyStack

18
Stack Applications
  • We can be classified stack applications into four
    categories
  • Reversing data.
  • Parsing data.
  • Postponing data usage.
  • Backtracking steps.

19
Stack ApplicationsReversing Data
1 2 3 4 5
5 4 3 2 1
Push
5
Pop
4
3
2
1
20
Stack ApplicationsParsing Data
Breaks the data into independent pieces for
further processing.
source program
machine language
Compiler
Figure 4-11
21
Stack ApplicationsParsing Data
3.Pop ) 2.Push ( 1.Push (
3.Pop ) 2.Pop ) 1.Push (
22
Stack ApplicationsPosponement
  • Arithmetic expression can be represent three
    different formats
  • Prefix a b
  • Infix a b
  • Postfix a b
  • Arithmetic precedence
  • multiply and divide
  • before
  • add and subtract!

23
Stack ApplicationsPosponement
  • A B C
  • Place the paranthesis
  • ( A ( B C))
  • Replace it in postfix format
  • (A(BC))
  • Remove all paranthesis
  • ABC

Implementation by computer is too hard!
24
Stack ApplicationsPosponement
and / operators have higher priority than the
operator at the top of the stack.
Figure 4-12, Part I
25
Stack ApplicationsPosponement
Figure 4-12, Part II
26
Stack ApplicationsEvaluation of Postfix
Expression
Figure 4-13
27
Excercise
  • Change the following infix expression to postfix
    expression using the algoritmic method (a stack).
  • abc-d

Solution abcd-
28
  • Infix String abc-d
  • The first character scanned is 'a'. 'a' is added
    to the Postfix string. The next character scanned
    is ''. It being an operator, it is pushed to the
    stack.
  • Next character scanned is 'b' which will be
    placed in the Postfix string. Next character is
    '' which is an operator. Now, the top element of
    the stack is '' which has lower precedence than
    '', so '' will be pushed to the stack.
  • The next character is 'c' which is placed in the
    Postfix string. Next character scanned is '-'.
    The topmost character in the stack is '' which
    has a higher precedence than '-'. Thus '' will
    be popped out from the stack and added to the
    Postfix string. Even now the stack is not empty.
    Now the topmost element of the stack is '' which
    has equal priority to '-'. So pop the '' from
    the stack and add it to the Postfix string. The
    '-' will be pushed to the stack.
  • Next character is 'd' which is added to Postfix
    string. Now all characters have been scanned so
    we must pop the remaining elements from the stack
    and add it to the Postfix string. At this stage
    we have only a '-' in the stack. It is popped out
    and added to the Postfix string. So, after all
    characters are scanned, this is how the stack and
    Postfix string will be
  • End result
  • Infix String abc-d
  • Postfix String abcd-

29
Stack ApplicationsBackTracking
Figure 4-14
30
Figure 4-15
31
Stack ApplicationsBackTracking
Figure 4-16
32
Figure 4-17
33
Array Implementation of Stacks
stack stackAry ltpointer to array of
dataTypegt count ltintegergt stackMax
ltintegergt top ltindexgt end stack
Figure 4-20
34
Hw-5
  • Write a program that accepts parentheses and
    brackets characters, one per line on standard
    input. Use a stack to determine whether pairs of
    characters are matching or not. Therefore
    complete the body of below function.
  • bool balanced(const char p , size_t n)
  • // Precondition p0...pn-1 contains n
    characters, each of which
  • // is '(', ')', '' or ''.
  • // Postcondition The function returns true if
    the characters form a
  • // sequence of correctly balanced parentheses
    with each '(' matching
  • // a ')' and each '' matching a ''. Note that
    a sequence such as
  • // ( ) is NOT balanced because when we draw
    lines to match the
  • // parentheses to their partners, the lines
    cross each other. On the
  • // other hand, ( ) amd ( ) are both
    balanced.

Load your HW-5 to FTP site until 13 Apr. 07 at
0900 am.
35
Exercises
Projects 23 page 211, 212
Figure 4-21
36
Exercises
Projects 24 page 212
Figure 4-22
Write a Comment
User Comments (0)
About PowerShow.com