Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.

Similar presentations


Presentation on theme: "Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack."— Presentation transcript:

1 Data Structures: A Pseudocode Approach with C1 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 Stacks

2 Data Structures: A Pseudocode Approach with C2 Stacks A stack is a linear list in which all additions and deletions are restricted to one end, called the top. Last in – first out (LIFO)

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

4 Data Structures: A Pseudocode Approach with C4 Push Push adds an item at the top of the stack.

5 Data Structures: A Pseudocode Approach with C5 Pop Pop removes the item at the top of the stack.

6 Data Structures: A Pseudocode Approach with C6 Stack top Stack top copies the item at the top of the stack.

7 Data Structures: A Pseudocode Approach with C7 Example

8 Data Structures: A Pseudocode Approach with C8 3-2 Stack Linked List Implementation (physical) 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

9 Data Structures: A Pseudocode Approach with C9 Data Structure

10 Data Structures: A Pseudocode Approach with C10 head data node

11 Data Structures: A Pseudocode Approach with C11

12 Data Structures: A Pseudocode Approach with C12 Stack Algorithms Create stack Push stack Pop stack Stack top Empty stack Full stack Stack count Destroy stack

13 Data Structures: A Pseudocode Approach with C13 Create stack Create stack allocates memory for the stack structure and initializes its metadata.

14 Data Structures: A Pseudocode Approach with C14 Push stack inserts an element into the stack. Push stack example 1 2 3 4

15 Data Structures: A Pseudocode Approach with C15 Push stack algorithm

16 Data Structures: A Pseudocode Approach with C16 Pop stack sends the data in the node at the top of the stack back to the calling algorithm. Pop stack

17 Data Structures: A Pseudocode Approach with C17 Call by reference

18 Data Structures: A Pseudocode Approach with C18 Stack top

19 Data Structures: A Pseudocode Approach with C19 Empty stack

20 Data Structures: A Pseudocode Approach with C20 Empty stack

21 Data Structures: A Pseudocode Approach with C21 Stack count

22 Data Structures: A Pseudocode Approach with C22 Destroy stack

23 Data Structures: A Pseudocode Approach with C23 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.

24 Data Structures: A Pseudocode Approach with C24

25 Data Structures: A Pseudocode Approach with C25 head

26 Data Structures: A Pseudocode Approach with C26

27 Data Structures: A Pseudocode Approach with C27 Q: 傳進來的是什麼 ? A: The address of the head Q: How to keep this address A: Using a pointer to keep it. Q: 傳進來的是什麼 ? A: The address of the head Q: How to keep this address A: Using a pointer to keep it.

28 Data Structures: A Pseudocode Approach with C28

29 Data Structures: A Pseudocode Approach with C29 Pop 出來的 char 放在這裡

30 Data Structures: A Pseudocode Approach with C30

31 Data Structures: A Pseudocode Approach with C31 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

32 Data Structures: A Pseudocode Approach with C32 ADT Prototype Declarations STACK* createStack(void); bool pushStack(STACK* stack, void* dataInPtr); void* popStack(STACK* stack); void* stackTop(STACK* stack); bool emptyStack(STACK* stack); bool fullStack(STACK* stack); int stackCount(STACK* stack); STACK* destroyStack(STACK* stack); STACK* createStack(void); bool pushStack(STACK* stack, void* dataInPtr); void* popStack(STACK* stack); void* stackTop(STACK* stack); bool emptyStack(STACK* stack); bool fullStack(STACK* stack); int stackCount(STACK* stack); STACK* destroyStack(STACK* stack);

33 Data Structures: A Pseudocode Approach with C33 Stack ADT structural concepts STACK* stack; … stack = createStack(); STACK* stack; … stack = createStack();

34 Data Structures: A Pseudocode Approach with C34 head data node

35 Data Structures: A Pseudocode Approach with C35 如果沒要到空間,就會傳回 NULL

36 Data Structures: A Pseudocode Approach with C36

37 Data Structures: A Pseudocode Approach with C37

38 Data Structures: A Pseudocode Approach with C38 ADT Pop Stack /*=================== popStack ================== This function pops item on the top of the stack. Pre stack is pointer to a stack Post Returns pointer to user data if successful NULL if underflow */ void* popStack (STACK* stack) { //Local Definitions void* dataOutPtr; STACK_NODE* temp; //Statements if (stack->count == 0) dataOutPtr = NULL; else { temp = stack->top; dataOutPtr = stack->top->dataPtr; stack->top = stack->top->link; free (temp); (stack->count)--; } // else return dataOutPtr; }// popStack

39 Data Structures: A Pseudocode Approach with C39 retrieve stackTop /*================== stackTop ================= Retrieves data from the top of stack without changing the stack. Pre stack is a pointer to the stack Post Returns data pointer if successful null pointer if stack empty */ void* stackTop (STACK* stack) { //Statements if (stack->count == 0) return NULL; else return stack->top->dataPtr; }// stackTop

40 Data Structures: A Pseudocode Approach with C40

41 Data Structures: A Pseudocode Approach with C41 ADT Pop Stack /*================== fullStack ================= This function determines if a stack is full. Full is defined as heap full. Pre stack is pointer to a stack head node Return true if heap full false if heap has room */ bool fullStack (STACK* stack) { //Local Definitions STACK_NODE* temp; //Statements if ((temp = (STACK_NODE*)malloc (sizeof(*(stack->top))))) { free (temp); return false; } // if // malloc failed return true; }// fullStack

42 Data Structures: A Pseudocode Approach with C42

43 Data Structures: A Pseudocode Approach with C43

44 Data Structures: A Pseudocode Approach with C44 3-5 Stack Applications Four basic application problems – 1)reversing data, 2)parsing data, 3)postponing data usage, and 4)backtracking steps - 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

45 Data Structures: A Pseudocode Approach with C45 Reversing data {1 2 3 4} becomes {4 3 2 1} Examples for reversing data Reverse a list Convert decimal to binary

46 Data Structures: A Pseudocode Approach with C46 Reverse a number series 1 … 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /* */ #include #include "stacksADT.h" int main(void) { //local Definitions bool done = false; int* dataPtr; STACK* stack; // Statements // Create a stack and allocate menory fir data Stack = createStack(); //Fill stack While (!done) { 在使用 ADT stack 時,取得儲存資料 的合法空間是你的責任 。

47 Data Structures: A Pseudocode Approach with C47

48 Data Structures: A Pseudocode Approach with C48

49 Data Structures: A Pseudocode Approach with C49

50 Data Structures: A Pseudocode Approach with C50 (continued)

51 Data Structures: A Pseudocode Approach with C51 Convert decimal binary (omitted) 1 … 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* */ #include #include “stacksADT.h” int main(void) { //local Definitions unsigned int num; int* digit; STACK* stack; // Statements // Create a stack and allocate menory fir data Stack = createStack(); //prompt and read a number printf(“Enter an interger: “); scanf(“%d”, &num); // create 0s and 1s and push them into the stack while (num > 0)

52 Data Structures: A Pseudocode Approach with C52

53 Data Structures: A Pseudocode Approach with C53 Parsing Parsing is any logic that breaks data into independent pieces for further processing. Example for parsing Unmatched parentheses

54 Data Structures: A Pseudocode Approach with C54 Unmatched parentheses example

55 Data Structures: A Pseudocode Approach with C55

56 Data Structures: A Pseudocode Approach with C56

57 Data Structures: A Pseudocode Approach with C57

58 Data Structures: A Pseudocode Approach with C58

59 Data Structures: A Pseudocode Approach with C59

60 Data Structures: A Pseudocode Approach with C60

61 Data Structures: A Pseudocode Approach with C61 Postponement -展延 Examples for postponement Infix to postfix transformation Postfix expression evaluation

62 Data Structures: A Pseudocode Approach with C62 The formats of an arithmetic expression Infix The operator comes before the operands. Postfix The operator comes between the operands. Prefix The operator comes after the operands.

63 Data Structures: A Pseudocode Approach with C63 An example A + B Infix A + B Prefix +AB Postfix AB-

64 Data Structures: A Pseudocode Approach with C64 Concept Prefix to postfix conversion recursion Infix to postfix transformation stacks Evaluate a postfix expression stacks Evaluate a prefix expression queues

65 Data Structures: A Pseudocode Approach with C65 Infix to postfix transformation In the infix notation, we need to use parentheses to control the evaluation of the operators. We thus have an evaluation method that includes parentheses and two operator priority classes. Priority 0 :( Priority 1 :+ - Priority 2 :* / ( 最高 )

66 Data Structures: A Pseudocode Approach with C66 Manual transformation 1.Fully parenthesize the expression using any explicit parentheses and the arithmetic precedence. 2.Change all infix notations in each parenthesis to postfix notation, starting from the innermost expressions. Conversion to postfix notation is done by moving the operator to the location of the expression’s closing parenthesis. 3.Remove all parentheses.

67 Data Structures: A Pseudocode Approach with C67 Example A+B*C Step 1 (A+(B*C)) Step 2 (A+(BC*)) (A(BC*)+) Step 3 ABC*+

68 Data Structures: A Pseudocode Approach with C68 More complex example (A+B)*C+D+E*F-G Step 1 (((((A+B)*C)+D)+(E*F))-G) Step 2 (((((AB+)C*)D+)(EF*)+)G-) Step 3 AB+C*D+EF*+G-

69 Data Structures: A Pseudocode Approach with C69 Algorithmic transformation When we need to push an operator into the stack, if its priority is higher than the operator at the top of the stack, we go ahead and push it into the stack. If the current operator’s priority is lower than or equal to that of the operator at the top of the stack than, it is popped and placed in the output expression.

70 Data Structures: A Pseudocode Approach with C70 Algorithmic transformation After doing this, we must check the new top operator. If its priority is greater than the current operator, it is popped to the output expression.

71 Data Structures: A Pseudocode Approach with C71 Infix Transformations A+B*C-D/E converts to ABC*+DE/-

72 Data Structures: A Pseudocode Approach with C72 ( ) operatoroperand

73 Data Structures: A Pseudocode Approach with C73

74 Data Structures: A Pseudocode Approach with C74 Code

75 Data Structures: A Pseudocode Approach with C75

76 Data Structures: A Pseudocode Approach with C76

77 Data Structures: A Pseudocode Approach with C77

78 Data Structures: A Pseudocode Approach with C78 Convert infix to postfix } // while // Print the postfix printf ("The postfix formula is: "); puts (postfix); // Now destroy the stack destroyStack (stack); return 0; }// main

79 Data Structures: A Pseudocode Approach with C79 Convert infix to postfix /*=================== priority ==================== Determine priority of operator. Pre token is a valid operator Post token priority returned */ int priority (char token) { //Statements if (token == '*' || token == '/') return 2; if (token == '+' || token == '-') return 1; return 0; }// priority

80 Data Structures: A Pseudocode Approach with C80 Convert infix to postfix /*=================== isOperator ==================== Determine if token is an operator. Pre token is a valid operator Post return true if operator; false if not */ bool isOperator (char token) { //Statements if (token == '*' || token == '/' || token == '+' || token == '-') return true; return false; }// isOperator

81 Data Structures: A Pseudocode Approach with C81 Convert infix to postfix /*Results Run 1 Enter an infix formula: 2+4 The postfix formula is: 24+ Run 2 Enter an infix formula: (a+b)*(c-d)/e The postfix formula is: ab+cd-*e/ */

82 Data Structures: A Pseudocode Approach with C82 Evaluating postfix expressions The operands come before the operators. Postpone the use of the operands. When we find an operator, we pop the two operands at the top of the stack and perform the operator. We then push the value back into the stack to be used later.

83 Data Structures: A Pseudocode Approach with C83

84 Data Structures: A Pseudocode Approach with C84

85 Data Structures: A Pseudocode Approach with C85 Code

86 Data Structures: A Pseudocode Approach with C86

87 Data Structures: A Pseudocode Approach with C87

88 Data Structures: A Pseudocode Approach with C88

89 Data Structures: A Pseudocode Approach with C89

90 Data Structures: A Pseudocode Approach with C90

91 Data Structures: A Pseudocode Approach with C91 backtracking Examples Goal seeking Eight queens problem

92 Data Structures: A Pseudocode Approach with C92 Goal seeking

93 Data Structures: A Pseudocode Approach with C93 Goal seeking

94 Data Structures: A Pseudocode Approach with C94

95 Data Structures: A Pseudocode Approach with C95 Eight queens problem

96 Data Structures: A Pseudocode Approach with C96

97 Data Structures: A Pseudocode Approach with C97 (continued)

98 Data Structures: A Pseudocode Approach with C98

99 Data Structures: A Pseudocode Approach with C99

100 Data Structures: A Pseudocode Approach with C100

101 Data Structures: A Pseudocode Approach with C101

102 Data Structures: A Pseudocode Approach with C102

103 Data Structures: A Pseudocode Approach with C103

104 Data Structures: A Pseudocode Approach with C104

105 Data Structures: A Pseudocode Approach with C105

106 Data Structures: A Pseudocode Approach with C106

107 Data Structures: A Pseudocode Approach with C107

108 Data Structures: A Pseudocode Approach with C108

109 Data Structures: A Pseudocode Approach with C109

110 Data Structures: A Pseudocode Approach with C110 3-6 How Recursion Works This section discusses the concept of the stack frame and the use of stacks in writing recursive software

111 Data Structures: A Pseudocode Approach with C111 Local variable: base, exp, result When power begins to execute, it must know the values of the parameter. It must also know where it needs to return when its processing is done. Finally, if it returns a value, it must know where the value is to be placed when it terminates.

112 Data Structures: A Pseudocode Approach with C112 Stackframe When power begins to execute, it must know the values of the parameter. When its processing is done, it must also know where it needs to return. Finally, if it returns a value, it must know where the value is to be placed when it terminates. These data are conceptually placed in a stackframe.

113 Data Structures: A Pseudocode Approach with C113 Stackframe A stackframe contains four different elements: 1.The parameters to be processed by the called algorithm. 2.The local variables in the calling algorithm 3.The return statement in the calling algorithm 4.The expression that is to receive the return value (if any)

114 Data Structures: A Pseudocode Approach with C114 Recursive Power Algorithm Program testPower 1 read (base, exp) 2 result = power (base, exp) 3 print (“base**exp is”, result) End testPower Program testPower 1 read (base, exp) 2 result = power (base, exp) 3 print (“base**exp is”, result) End testPower Algorithm power(base, exp) 1 If (exp is 0) 1Return (1) 2 Else 1Return (base * power(base, exp – 1)) 3 End if End power Algorithm power(base, exp) 1 If (exp is 0) 1Return (1) 2 Else 1Return (base * power(base, exp – 1)) 3 End if End power

115 Data Structures: A Pseudocode Approach with C115

116 Data Structures: A Pseudocode Approach with C116

117 Data Structures: A Pseudocode Approach with C117


Download ppt "Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack."

Similar presentations


Ads by Google