Data Structure Chapter 3 Stacks and Queues Instructor: Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring
Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2
Figure 3.: Inserting and Deleting Elements in Stack top top top E top D D top C C C B B B B A A A A A top D C B A Stack: an ordered list in which insertions and deletions are made at one end called the top The last element inserted into a stack is the first element removed Last-In-First-Out (LIFO) list C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3
Implementation of Stack by Array a n- a 2 a a a a a 2 a n- Array index 2 3 n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4
Functions for Stacks Create #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct{ int key; /* other fields */ }element; element stack[max_stack_size]; Int top = -; IsFull top >=MAX_STACK_SIZE-; IsEmpty top < Add Delete C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5
Program 3.: Add to Stack void add (int *top, element item) { /* add an item to the global stack */ if (*top >=MAX_STACK_SIZE-) { stack_full(); return; } stack[++*top]=item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6
Program 3.2: Delete from a Stack Element delete (int *top) { /* return the top element from the stack */ if (*top == -) return stack_empty(); /* returns an error key */ return stack[(*top)--]; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7
How Does the System Stack Work after a Function Call? Old frame pointer Return address 2 Old frame pointer Return address Local variables Old frame pointer Return address fp main fp: a pointer to the current stack frame a: function name fp a C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8
Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9
Figure 3.4: Inserting and Deleting Elements in A Queue rear rear rear D rear C C B B B A A A A rear front front front front front Queue: an ordered list in which insertions take place at one end and all deletions take place at the opposite end The first element inserted into a queue is the first element removed First-In-First-Out (FIFO) list D C B a a a 2 a n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 front rear
Functions of Queue CreateQ #define MAX_QUEUE_SIZE /* Maximum queue size */ typedef struct{ int key; /* other fields */ } element; element queue[max_queue_size]; int rear = -; int front = -; IsFullQ rear == MAX_QUEUE_SIZE- IsEmptyQ front == rear AddQ DeleteQ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29
Program 3.3: Add to A Queue void addq (int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE-) { queue_full(); return; } queue[++*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2
Program 3.4: Delete from A Queue element deleteq (int *front, int rear) { /* remove element at the front of the queue */ if (*front == rear) return queue_empty(); /*return an error key */ return queue[++*front]; } Is there any problem with current queue representation? C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3
Initialization: front=rear= Circular Queues front will always point one position counterclockwise from the first element in the queue circular queue assigns next element to q[] when rear == MaxSize If front == rear, is queue empty or full? front = ; rear = 2 3 2 J3 J2 J n- n-2 front = ; rear = 3 n-4 n-3 J4 J3 n- J2 J n-2 front = n-4; rear = C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4 n-3 n-4
Examples of Full Circular Queues J8 J7 Jn- J6 J5 J3 n- Jn J2 n-2 front = ; rear = n- n-3 n-4 J8 J7 J6 J5 J2 J4 J3 n- Jn- n-4 n-3 n-2 front = n-3; rear = n-4 Solution: A circular queue of size MAX_QUEUE_SIZE will be Permitted to hold at most MAX_QUEUE_SIZE- elements! C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5
Program 3.5: Add to A Circular Queue void addq (int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear+)% MAX_QUEUE_SIZE; if (front == *rear) { queue_full(rear); /* reset rear and print error */ return; } queue[*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6
Program 3.6: Delete from A Circular Queue element deleteq (int *front, int rear) { } element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty(); /* queue_empty returns an error key */ *front = (*front+)% MAX_QUEUE_SIZE; return queue[*front]; C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7
Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8
A Mazing Problem An Example Entrance C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9 Exit : open path : barrier
Allowable Moves NW [i-][j-] N [i-][j] NE [i-][j+] W [i][j-] X [i][j] [i][j+] E [i+][j-] SW [i+][j] S [i+][j+] SE C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2
A Mazing Problem Data Structure Stack : store the coordinates and direction #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct { short int row; short int col; short int dir; } element; element stack[max_stack_size]; Save current position and arbitrarily pick a possible move we can return to it and try another path if fails! Use of anothr 2-D array to mark any position that has been visited C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2
Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 22
Order in Which A Second-Degree Polynomial Is Evaluated C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 23
Figure 3.3: Infix and Postfix Notation Expressions are converted from Infix notation into Postfix notation before compiler can process them! Infix? Postfix 2+3*4 234*+ a*b+5 ab*5+ (+2)*7 2+7* a*b/c ((a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c ab*c/ abc-d+/ea-*c* ab/c-de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 24
Infix to Postfix Step : Fully parenthesize the expression Step 2: Move all binary operators so that they replace their corresponding right parentheses Step 3: Delete all parentheses An example: a/b-c+d*e-a*c After step ((((a/b)-c)+(d*e))-a*c)) After steps 2 and 3 ab/c=de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 25
Figure 3.5: Translation of a+b*c (Infix) to PostFix Token Stack [] [] [2] Top a - a Output + + a b + ab * + * ab c + * abc eos - abc*+ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 26
Figure 3.6: Translation of a*(b+c)*d to Postfix Token Stack [] [] [2] Top a - a * * a ( * ( a b * ( ab + * ( + 2 ab c * ( + 2 abc ) * abc+ * * abc+* d * abc+*d eos * abc+*d* Output C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 27
Postfix Evaluation : The Representations of Postfix Expression & Stack Expression character array Binary operators: +-*/% Operands: 2 3 4 5 6 7 8 9 Stack int array Operands only Declarations: #define MAX_STACK_SIZE /* maximum stack size */ #define MAX_EXPR_SIZE /* max size of expression */ typedef enum{lparen, rparen, plus, minus, times, divide, mod, eos, operand} precedence; int stack[max_stack_size]; /* global stack */ char expr[max_expr_size]; /* input string */ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 28
Figure 3.4: Postfix Evaluation Input postfix expression: 6 2 / 3 4 2 * + Token Stack Top [](bottom) [] [2] 6 6 2 6 2 / 6/2 3 6/2 3-6/2-3 4 6/2-3 4 2 6/2-3 4 2 2 * 6/2-3 4*2 + 6/2-3+4*2 C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 29
Postfix Expression Evaluation Step : Make a single left-to-right scan of the postfix Step 2: Place the operands on a stack until we find an operator 2.: Remove the correct number of operands for the operator from the stack 2.2: Perform the operation 2.3: Place the result back on the stack Step 3: Go to step until we reach the end of the expression Step 4: Remove the answer from the top of the stack C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3