Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks Chapter 3 Objectives Upon completion you will be able to

Similar presentations


Presentation on theme: "Stacks Chapter 3 Objectives Upon completion you will be able to"— Presentation transcript:

1 Stacks Chapter 3 Objectives Upon completion you will be able to
Explain the design, use, and operation of a stack Implement a stack using a linked list structure Understand the operation of the stack ADT Write application programs using the stack ADT Discuss reversing data, parsing, postponing and backtracking Data Structures: A Pseudocode Approach with C

2 Introduction A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a top When data are inserted into a stack and then they are removed, their order would be reversed Data Structures: A Pseudocode Approach with C

3 Data Structures: A Pseudocode Approach with C

4 3-1 Basic Stack Operations
The stack concept is introduced and three basic stack operations are discussed. Push Pop Stack Top Data Structures: A Pseudocode Approach with C

5 3-1 Basic Stack Operations
There are 3 basic stack operations 1. push: adds an item at the top of the stack after the push, the new item becomes the top the stack is in an overflow state if there is no room for the new item Data Structures: A Pseudocode Approach with C

6 Data Structures: A Pseudocode Approach with C

7 Basic Stack Operations (2)
2. Pop when a stack is popped, the item at the top of the stack is removed and return it to the user as the top item has been removed, the next older item in the stack becomes the top when the last item in the stack is deleted, it must be set to its empty state if pop is called when the stack is empty, then it is in an underflow state Data Structures: A Pseudocode Approach with C

8 Data Structures: A Pseudocode Approach with C

9 Basic Stack Operations (3)
3. Stack Top copies the item at the top of the stack it returns the data in the top element to the user but does not delete it stack top can also result in underflow if the stack is empty Data Structures: A Pseudocode Approach with C

10 Data Structures: A Pseudocode Approach with C

11 An Example Figure 3-5 with an empty stack push green into stack
push blue into stack pop push red into stack stack top =? Data Structures: A Pseudocode Approach with C

12 Data Structures: A Pseudocode Approach with C

13 3-2 Stack Linked List Implementation
In this section we present a linked-list design for a stack. After developing the data structures, we write pseudocode algorithms for the stack ADT. Data Structure Algorithms Data Structures: A Pseudocode Approach with C

14 Stack-linked-list Implementation (2)
There are several data structures that could be used to implement a stack, e.g. array or linked list In this section, we implement it as a linked list To implement the linked-list stack, we need two different structures a head node a data node Data Structures: A Pseudocode Approach with C

15 Data Structures: A Pseudocode Approach with C

16 Stack Head and Stack Data Node
stack head requires at least 2 attributes a count of the number of elements in the stack a top pointer other stack attributes can be placed in a stack head such as the time the stack was created stack data node also requires at least 2 attributes data pointer to the next node Data Structures: A Pseudocode Approach with C

17 Data Structures: A Pseudocode Approach with C

18 Stack Algorithms 8 operations are defined to solve any basic stack problem create stack - empty stack push stack - full stack pop stack - stack count stack top - destroy stack there may be additional stack operations Data Structures: A Pseudocode Approach with C

19 Data Structures: A Pseudocode Approach with C

20 Data Structures: A Pseudocode Approach with C

21 Data Structures: A Pseudocode Approach with C

22 Data Structures: A Pseudocode Approach with C

23 Data Structures: A Pseudocode Approach with C

24 Data Structures: A Pseudocode Approach with C

25 Data Structures: A Pseudocode Approach with C

26 Data Structures: A Pseudocode Approach with C

27 Data Structures: A Pseudocode Approach with C

28 Data Structures: A Pseudocode Approach with C

29 Data Structures: A Pseudocode Approach with C

30 Data Structures: A Pseudocode Approach with C

31 3-3 C Language Implementations
This section presents a simple non-ADT implementation of a stack. We develop a simple program that inserts random characters into the stack and then prints them. Data Structures: A Pseudocode Approach with C

32 Data Structures: A Pseudocode Approach with C

33 Data Structures: A Pseudocode Approach with C

34 Data Structures: A Pseudocode Approach with C

35 Data Structures: A Pseudocode Approach with C

36 (Continued) Data Structures: A Pseudocode Approach with C

37 Data Structures: A Pseudocode Approach with C

38 Data Structures: A Pseudocode Approach with C

39 Data Structures: A Pseudocode Approach with C

40 3-4 Stack ADT We begin the discussion of the stack ADT with a discussion of the stack structure and its application interface. We then develop the required functions. Data Structure ADT Implemenation Data Structures: A Pseudocode Approach with C

41 Data Structures: A Pseudocode Approach with C

42 Data Structures: A Pseudocode Approach with C

43 Data Structures: A Pseudocode Approach with C

44 Data Structures: A Pseudocode Approach with C

45 Data Structures: A Pseudocode Approach with C

46 Data Structures: A Pseudocode Approach with C

47 Data Structures: A Pseudocode Approach with C

48 Data Structures: A Pseudocode Approach with C

49 Data Structures: A Pseudocode Approach with C

50 Data Structures: A Pseudocode Approach with C

51 Data Structures: A Pseudocode Approach with C

52 Data Structures: A Pseudocode Approach with C

53 Data Structures: A Pseudocode Approach with C

54 3-5 Stack Applications Three basic application problems-parsing, postponement, and backtracking-are discussed and sample programs developed. In addition, several other stack applications are presented, including the classic Eight Queens problem. Reversing Data Converting Decimal to Binary Parsing Postponement Backtracking Data Structures: A Pseudocode Approach with C

55 Reversing Data given a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged also for example: {1, 2, 3, 4} becomes {4, 3, 2, 1} we examine 2 different reversing applications: reveres a list convert decimal to binary Data Structures: A Pseudocode Approach with C

56 Algorithm Reverse a number series
Algorithm reverseNumber This program reverses a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one 1  stack = createStack 2  print (Enter a number) 3  read (number) Data Structures: A Pseudocode Approach with C

57 Algorithm Reverse a number series (2)
Fill stack 4  loop ( not end of data AND stack not full) 1 pushStack (number ) 2 prompt (Enter next number: <EOF> to stop) 3 read( number ) Print numbers in reverse 5  loop (not emptyStack (stack) 1 popStack (stack, dataOut) 2 print (dataOut) 6  stack = destroyStack (stack) end reverseNumber Data Structures: A Pseudocode Approach with C

58 Data Structures: A Pseudocode Approach with C

59 (continued) Data Structures: A Pseudocode Approach with C

60 Convert Decimal to Binary
1 read (number) 2 loop (number > 0) 1 digit = number modulo 2 2 print (digit) 3 number = number / 2 Data Structures: A Pseudocode Approach with C

61 Algorithm Convert decimal to binary
algorithm decimalToBinary This algorithm reads an integer from the keyboard and print its binary equivalent. It uses a stack to reverse the order of 0’s and 1’s produces. 1 stack = createStack 2  prompt (Enter a decimal to convert to binary) 3  read (number) 4  loop (number > 0) 1 digit = number modulo 2 2 pushOK = push (stack,digit) 3 if (pushOK false) 1 print (Stack overflow creating digit) 2 quit algorithm 4 number = number/2 Data Structures: A Pseudocode Approach with C

62 Algorithm Convert decimal to binary (2)
Binary number create in stack. Now print it. 5  loop (not emptyStack(stack)) 1 popStack (stack,digit) 2 print(digit) Binary number create. Destroy stack and return 6 destroy(stack) end decimalToBinary Data Structures: A Pseudocode Approach with C

63 Parsing any logic that breaks data into independent pieces for further processing for example, to translate a source program to machine language, a compiler must parse the program into individual parts such as keywords, names, and tokens one common programming problem, unmatched parentheses in an algebraic expression Data Structures: A Pseudocode Approach with C

64 Data Structures: A Pseudocode Approach with C

65 Data Structures: A Pseudocode Approach with C

66 Data Structures: A Pseudocode Approach with C

67 Data Structures: A Pseudocode Approach with C

68 Data Structures: A Pseudocode Approach with C

69 Data Structures: A Pseudocode Approach with C

70 Data Structures: A Pseudocode Approach with C

71 Postponement When we used a stack to reverse a list, the entire list was read before we began outputting the results Often the logic of an application requires that the usage of data be deferred until some later point A stack can be useful when the application requires that data’s use be postponed for a while two stack postponement applications: infix to postfix translation postfix expression evaluation Data Structures: A Pseudocode Approach with C

72 Infix To Postfix Transformation
An arithmetic expression can be represented in 3 different formats infix: the operator comes between the 2 operands postfix: the operator comes after the 2 operands prefix: the operator comes before the 2 operands Infix: a + b Postfix: a b + Prefix: + a b Data Structures: A Pseudocode Approach with C

73 Transformation Using Stack
Can we use stack to handle infix to postfix transformation? What happen if we push operators into a stack and after the whole infix expression has been read, we pop the stack? A * B = A B * Data Structures: A Pseudocode Approach with C

74 Transformation Using Stack (2)
Priority 2: * / Priority 1: Priority 0: ( Data Structures: A Pseudocode Approach with C

75 Example 3 Given the expression: A + B * C Using the following rules
copy operand A to output expression push operator + into stack copy operand B t output expression push operator * into stack (*’s priority is higher than +’s) copy operand C to output expression pop operator * and copy to output expression pop operator + and copy to output expression A result is: A B C * + Data Structures: A Pseudocode Approach with C

76 Example 4 Given the expression: A + B * C – D / E The result is
Data Structures: A Pseudocode Approach with C

77 Figure 3-12, Part I: Infix transformations
Data Structures: A Pseudocode Approach with C

78 รูปที่ 3-12, ส่วนที่ 2 : การแปลงนิพจน์ infix ให้เป็น postfix
Data Structures: A Pseudocode Approach with C

79 Data Structures: A Pseudocode Approach with C

80 Data Structures: A Pseudocode Approach with C

81 Example 5 Given the expression: A * B – (C + D) + E
Data Structures: A Pseudocode Approach with C

82 Table 3-1 Example: convert infix to postfix
Data Structures: A Pseudocode Approach with C

83 Data Structures: A Pseudocode Approach with C

84 Data Structures: A Pseudocode Approach with C

85 Data Structures: A Pseudocode Approach with C

86 Data Structures: A Pseudocode Approach with C

87 Evaluating Postfix Expressions
Given the expression A B C + * assuming that A = 2 B= 4 C = 6 Data Structures: A Pseudocode Approach with C

88 Figure 3-13: Evaluation of postfix expression
Data Structures: A Pseudocode Approach with C

89 Data Structures: A Pseudocode Approach with C

90 Data Structures: A Pseudocode Approach with C

91 Data Structures: A Pseudocode Approach with C

92 Data Structures: A Pseudocode Approach with C

93 Data Structures: A Pseudocode Approach with C

94 Data Structures: A Pseudocode Approach with C

95 Data Structures: A Pseudocode Approach with C

96 Data Structures: A Pseudocode Approach with C

97 Backtracking found in applications such as computer gaming, decision analysis, and expert systems in this section, we examine a backtracking application called goal seeking Data Structures: A Pseudocode Approach with C

98 Goal Seeking one way to portray the problem is to layout the steps in the form of a graph that contains several alternate paths in it only one of the pats in the figure leads us to a desired goal while we can immediately see the correct path when we look at the figure, the computer needs an algorithm to determine the correct path Data Structures: A Pseudocode Approach with C

99 Data Structures: A Pseudocode Approach with C

100 Goal Seeking (2) what do we put into the stack?
if all we wanted to do was to located the node that contains the goal, we would just put the branch point nodes into the stack as we want to print out the path that leads us to our goal, we must put the nodes in the valid path into the stack Data Structures: A Pseudocode Approach with C

101 Figure 3-15: Backtracking stack operation
Data Structures: A Pseudocode Approach with C

102 Data Structures: A Pseudocode Approach with C

103 Data Structures: A Pseudocode Approach with C

104 Data Structures: A Pseudocode Approach with C

105 (continued) Data Structures: A Pseudocode Approach with C

106 Data Structures: A Pseudocode Approach with C

107 Data Structures: A Pseudocode Approach with C

108 Data Structures: A Pseudocode Approach with C

109 Data Structures: A Pseudocode Approach with C

110 Data Structures: A Pseudocode Approach with C

111 Data Structures: A Pseudocode Approach with C

112 Data Structures: A Pseudocode Approach with C

113 Data Structures: A Pseudocode Approach with C

114 Data Structures: A Pseudocode Approach with C

115 Data Structures: A Pseudocode Approach with C

116 Data Structures: A Pseudocode Approach with C

117 Data Structures: A Pseudocode Approach with C

118 3-6 How Recursion Works This section discusses the concept of the stack frame and the use of stacks in writing recursive software Enqueue Dequeue Queue Front Queue Rear Queue Example Data Structures: A Pseudocode Approach with C

119 Data Structures: A Pseudocode Approach with C

120 Data Structures: A Pseudocode Approach with C

121 3-7 Array Implementation of Stacks
if we know a stack’s maximum size, an array implementation of a stack is more efficient than linked list implementation with array implementation, the base is index 0 (in C) the top moves up and down as data are inserted or deleted to push an element, we add one to top and use it as the array index to pop, we copy at index location top and then subtract one from top Data Structures: A Pseudocode Approach with C

122 Figure 3-20: Stack array implementation
Data Structures: A Pseudocode Approach with C

123 Algorithm Stack array definition
Stack Definitions for Array Implementation stack stackAry <pointer to array of dataType> count <integer> stackMax <integer> top <index> end stack Data Structures: A Pseudocode Approach with C

124 Algorithm Create stack array
algorithm createStack (stackElem <integer>) Allocates memory for a stack head node from dynamic memory and returns its address to the caller. Pre stackElem contains size of stack Post Head node and array allocated or error returned Return pointer to head node or null pointer if no memory 1 if (memory not available) 1 stackPtr = null 2 else 1 allocate (stackPtr) Data Structures: A Pseudocode Approach with C

125 Algorithm Create stack array (2)
Head allocated. Now initialize and allocate stack array. 2 stackPtr -> count = 0 3 stackPtr -> top = -1 4 stackPtr -> stackMax = stackElem 5 if (memory not available) 1 recycle (stackPtr) 2 stackPtr = null 6 else Allocate memory for stack array 1 allocate (stackPtr -> stackAry) 3 return stackPtr end createStack Data Structures: A Pseudocode Approach with C

126 Algorithm Push stack array
algorithm pushStack (val stack <head pointer>, val data <dataType>) 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 Return true if successful; false if memory overflow Data Structures: A Pseudocode Approach with C

127 Algorithm Push stack array (2)
1 if (stack -> count is at maximum) 1 success = false 2 else 1 stack -> count = stack -> count + 1 2 stack -> top = stack -> top + 1 3 stack -> stackAry[stack -> top] = data 4 success = true 3 return success end pushStack Data Structures: A Pseudocode Approach with C

128 Algorithm Pop stack algorithm popStack (val stack <head point>,
ref dataOut <dataType>) This algorithm 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 calling algorithm Return true if successful; false if underflow Data Structures: A Pseudocode Approach with C

129 Algorithm 3-19 Pop stack (2)
1 if (stack empty) 1 success = false 2 else 1 dataOut = stack -> stackAry [stack -> top] 2 stack -> top = stack -> top – 1 3 stack -> count = stack -> count – 1 4 success = true 3 return success end popStack Data Structures: A Pseudocode Approach with C

130 Algorithm Stack top algorithm stackTop (val stack <head pointer>, ref dataOut <dataType>) This algorithm retrieves the data from the top of the stack without changing the stack. Pre stack is a pointer to the stack head structure Post data have been returned to calling algorithm Return true if data returned, false if underflow Data Structures: A Pseudocode Approach with C

131 Algorithm Stack top (2) 1 if (stack -> count zero)
1 success = false 2 else 1 dataOut = stack -> stackAry [stack -> top].data 2 success = true 3 return success end stackTop Data Structures: A Pseudocode Approach with C

132 Algorithm Empty stack algorithm emptyStack (val stack <head pointer>) Determines if stack is empty and return a Boolean. Pre stack is a pointer to the stack head structure Post returns stack status Return Boolean, true: stack empty, false: stack contains data 1 if (stack -> count > 0) 1 result = false 2 else 1 result = ture 3 return result end emptyStack Data Structures: A Pseudocode Approach with C

133 Algorithm Full stack algorithm fullStack (val stack <head pointer>) Determines if stack is full and return a Boolean. Pre stack is a pointer to the stack head structure Post returns stack status Return Boolean, true: stack full, false: memory available 1 if (stack ->count < MAX_STACK) 1 result = false 2 else 1 result = true 3 return result end fullStack Data Structures: A Pseudocode Approach with C

134 Algorithm Stack count algorithm stackCount (val stack <head pointer>) Return the number of elements currently in stack. Pre stack is a pointer to the stack head structure Post returns stack count Return integer count of number of elements in stack 1 return (stack->count) end stackCount Data Structures: A Pseudocode Approach with C

135 Algorithm 3-24 Destroy stack
algorithm destroyStack (val stack <typeStack>) This algorithm releases all nodes back to the dynamic memory. Pre stack is a pointer to the stack head structure Post head structure and array recycled Return null pointer 1 if (stack not empty) 1 recycle (stack->stackAry) 2 recycle (stack) 2 return null pointer end destroyStack Data Structures: A Pseudocode Approach with C


Download ppt "Stacks Chapter 3 Objectives Upon completion you will be able to"

Similar presentations


Ads by Google