CS 235102 Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Stacks Example: Stack of plates in cafeteria.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
 Balancing Symbols 3. Applications
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Chapter 3. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
CS Data Structures Chapter 3 Stacks and Queues.
Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Data Structures & Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack.
Chap 3 Stack and Queue. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
STACKS AND QUEUES.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
STACKS & QUEUES for CLASS XII ( C++).
Stacks Stacks.
CS 201 Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
STACKS.
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Data Structures – Week #3
Algorithms and Data Structures
PART II STACK APPLICATIONS
Stack.
Stacks and Queues The Stack abstract data type
Stacks Chapter 5 Adapted from Pearson Education, Inc.
UNIT-I Topics to be covere d 1.Introduction to data structures.
Stacks and Queues 1.
Stack.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012

1 Stack – Abstract Data Type (ADT)  Object: Stack An order list in which insertions and deletions are made at only one end.  Operations: Insert(a) or Push(a) ::= insert element a into the stack. Delete() or Pop() ::= delete and return top element from the stack. IsFull ::= return FALSE or TRUE. IsEmpty ::= return FALSE or TRUE.

 Stack operates in Last-In-First-Out (LIFO) order 2 Operatons on Stack BABA CBACBA top Insert A A Insert BInsert C empty

Operations on a Stack 3 BABA DBADBA top DeleteInsert D CBACBA top

 Using a 1-D array  Stack [MAX_STACK_SIZE] top: point to the top of the element of the stack top = -1 denotes an empty stack. 4 Representation of a stack

5 Operations: Push Push( int *top, element item) { if (*top >= MAX_STACK_SIZE-1) { stackFull(); return; } *top = *top +1; stack[*top] = item; }

Operations: Pop 6 /* return the top element from the stack */ element Pop (int *top) { if (*top == -1) return StackEmpty(); item = stack[*top]; *top = *top-1; return item; }

 System stack: used in the run time to process nested procedure calls The return addresses of previous outer procedures are stored in the stack. Application of Stack 7

8 By the time Sub3 is called, the stack contains all three return addresses: Return address to Sub2 Return address to Sub1 Return address to main System stack

Application of Stack  Creation and activation: record for the invoked subprogram return address for calling procedure Parameters of current procedure local variables in current procedure  When exit a subprogram, delete the activation record on the top of the stack. 9

10 Queue – Abstract Data Type (ADT)  Object: Queue an ordered list in which all insertions take place at the end and all deletions take place at the other end  Operations: Add(a) ::= insert element a at the rear of the queue. DeleteQ() ::= delete and return front element from the queue. IsFullQ ::= return FALSE or TRUE. IsEmptyQ ::= return FALSE or TRUE.

Operations on a Queue  Eg. First-In-First-Out (FIFO) order: f: front pointer r: rear pointer 11

Representation of a Queue  Use a 1-D array  Queue [MAX_QUEUE_SIZE] front, rear: two variables point to the two ends of the queue.  Initialization: front = -1; rear = -1 front: one less than the position of the first element in the queue. 12 Queue

Representation of a Queue  front == rear  empty Queue 13 Queue frontrear

Operations: AddQ 14 AddQ(int *rear, element item) { if (*rear == MAX_Queue_SIZE - 1){ QueueFull(); return; } *rear = *rear +1; queue[*rear] = item; }

Operations: DeleteQ 15 element DeleteQ(int *front, int rear) { if (*front == rear) return QueueEmpty(); *front = *front + 1; return queue[*front]; }

Operations: Problem  Problem: a Job queue by operating system Q[1] Q[2] Q[3]… J_1Insert J_1 J_1 J_2 J_3 Insert J_2, J_3 J_2 J_3 Delete J_2 J_3 J_4 J_5 Insert J_4, J_5 J_3 J_4 J_5 Delete J_4 J_5 Delete  The queue shifts to the right  When queue is full, we need to move the entire queue left  time consuming 16

Solution  Use a circular array to represent a queue, 17 if rear == Max_Queue_Size -1 then rear = 0; else rear = rear +1; rear = (rear+1) % Max_Queue_Size; [1][1] [0][0] [2][2] [3][3] [4][4] [5][5] front rear

How to test “Empty” & “Full” ?  Empty front == rear 18 [2] [1][1] [0][0] [3][3] front rear

How to test “Empty” & “Full” ?  Full rear + 1 == front  use only n-1 element  use n elements  will not be able to distinguish full or empty 19 [2] [1][1] [0][0] [3][3] front rear [4][4]

AddQ using Circular Array 20 AddQ( int front, int *rear, element item) { *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear){ QueueFull(); return; } Queue[*rear] = item; }

DeleteQ using Circular Array 21 element DeleteQ(int *front, int rear) { if (*front == rear) return QueueEmpty(); *front = (*front +1) % MAX_QUEUE_SIZE; return queue[*front]; }

Evaluation of An Expression  x = A/B-C How to evaluate an expression ?  let A=6, B=3, C=2 (A/B)-C =(6/3)-2 =2-2 =0 (A/(B-C)) =(6/(3-2) ) =6/1 =6 22

Rules to evaluate an expression  Assign precedence to operator, and Evaluate first the operator with higher precedence  Left to right for the operator with the same precedence (in general),  Right to left for prefix unary operators 23

Relues to evaluate an expression 24

Example 1  ALU two operands & one result  Compiler generates codes for the ALU Eg: D=A+B*C mul B C T1 add A T1 D source destination 25 +, -, *, /

Example 2  Eg2: D=A+B-C add A B T1 sub T1 C D  Generates correct code so that when the code is evaluated, the expression is computed correctly. 26

Infix and Postfix Notations  Infix notation = operator comes in–between the operands Eg: A+B*C A+B-C It is not easier for compiler to generate code to evaluate an expression in infix notation  Postfix notation: each operator appears after its operands Eg: A+B-Cinfix AB+C-postfix 27

Translate infix to postfix  How to translate from infix to postfix? A/B-C  First Algorithm: (1) Fully parenthesize the expression (2) Move all operators so that they replace their corresponding right parenthesis (3) Delete all parenthesis 28

Examples  Eg1: ( ( A / B ) – C )  A B / C - Eg2: ( ( A + B ) - ( C * D ) )  A B + C D * - Eg3: ((((A / B ) – C ) + ( D * E ) ) - ( A * C ) )  A B / C - D E * + A C * - 29

 What is good about postfix? Evaluation of postfix expression done in one pass Eg1: A + B - C  A B + C - add A B T1 sub T1 C T2  Scan the expression from left to right  Use stack to hold operands Example T1 T2 -

Example 1: A B + C - ‘+’ pop operands A, B T1 = A + B push T1 to stack 31 A BABA T1=A+B A B C C T1=A+B ‘-’ pop operands C, T1 T2 = T1 – C push T2 to stack T2=T1-C

Example 2  Ex2: A+B-C*D  A B + C D * - 32 A BABA A B T1=A+B ‘+’ pop two operands A, B T1 = A+B push the result “*” pop two operands C, D T2 = C*D push the result C T1=A+B C D C T1=A+B D T2=C*D T1=A+B

Example 2 33 T2=C*D T1=A+B ‘-’ pop T1, T2 T3 = T1 – T2 push the result T3=T1-T2  Ex2: A+B-C*D  A B + C D * -

Example 3 ‘/’ pop two operands T1 = A/B push result 34 A A BABA B T1=A/B  Ex3. A B / C – D E * + A C * -

Example 3 ‘-’ pop two operands T2=T1-C push the result 35 C T1=A/B T2=T1-C C  Ex3. A B / C – D E * + A C * -

Example 3 “*” pop two operands T3= D*E push the result 36 D T2=T1-C D E D T2=T1-C E T3=D*E T2=T1-C  Ex3. A B / C – D E * + A C * -

Example 3 “*” Pop two operands T4=T3+T2 Push the result 37 T4=T3+T2 A T4=T3+T2 A C A T4 C  Ex3. A B / C – D E * + A C * -

Example 3 “*” Pop two operands T5=A*C Push the result 38 T5=A*C T4 T6=T4-T5  Ex3. A B / C – D E * + A C * - “-” Pop two operands T6=T4 – T5 Push the result

Postfix vs Infix  Postfix- when you see an operator, operands have been seen in front of it (on the top of the stick)  Infix- when you see an operator, you do not know if the operand is ready Thus when we see the operator, we cannot do the evaluation for this operator yet. 39

Evaluate Postfix Expression EvalPostfixExpression(expression e) { x = NextToken(e); while (x is not end of expression) do { if (x is an operand) then push(&top, x) else{ /* x is an operator */ 1. pop the correct number of operands for x from stack; 2. perform the operation; (or generate the code) 3. push the result onto the stack; } x = NextToken(e); }

How to Transform Infix to Postfix?  How to translate from infix to postfix? First algorithm: “Fully parenthesize” the expression Convert full parenthesized expr. to postfix Disadvantage: Need two passes of scanning the expression 41

Algorithm Two  Alg2: 1. scan the expression just once 2. utilize stack  Observations: 1: The positions of operands are not changed  passing any operands to the output immediately 2: Store the operator in stack until it is time to pass it to the output 42

Algorithm Two  “it is time”: means that the precedence of operator in the stack is larger or equal to that of the incoming operator (left-to-right associativity) 43

Example 1  2.1push operator to the stack  2.2 operators are taken out of stack as long as their precedence is higher or equal to the precedence of new incoming operator Eg1: A / B – C  A B / C - 44 / A / A B - AB/ - AB/C AB/C- - eoe

Example 2  Eg2: A + B – C * D  A B + C D * A + A B - AB+ *-*- AB+C *-*- AB+CD AB+CD* - AB+CD*- - * eoe

Example 3  Eg3: A / B – C + D * E – A * C 46 / A / A B / AB/ - AB/C - AB/C- - + A B + AB/C-D + *

Example 3  Eg3: A / B – C + D * E – A * C 47 A B/C-DE *+*+ AB/C-DE* AB/C-DE*+A - * AB/C-DE*+AC *-*- AB/C-DE*+AC*-

Transform Infix Expr. with ( )  Expression with ( ) 1. ‘( )’  evaluate first 2. ‘(‘  push ‘(’ to stack always 3. when ’(’ is on top of the stack, then push other operators (always) to stack 4. ‘)’  pop the operators until you see the matched ‘(‘  Eg. A * ( B + C ) / D 48 * ( (*(* AB (*(* + A +(*+(*

Transform Infix Expr. with ( ) 49 +(*+(* ) * ABC+ / ABC+*D ABC / eoe ABC+*D/ Eg. A * ( B + C ) / D

Transform Infix to Postfix 50 PosfixExpression(InfixExpression: e) { while (not end of expression){ x = NextToken(e); if x is an operand then write (x) else switch(x){ case ’(’: push (&top, ‘(’); break; case ‘)’: do { y = pop(&top); if (y!= ‘(’) write(y); } while y!= ‘(’; break; default: while (precedence of operator on top of stack >= precedence of x) { y=pop(&top); write(y); } push(&top, x); } } do{ y = pop (&top); write(y);} while !EmptyStack(); }

Multiple Stacks  Multiple stacks Store multiple stacks in 1-D array  # of stacks = 2 : - Using top[0] & top[1]  # of stacks = k > 2 : - Using b[i] & top[i] for each stack b[0] b[1] b[2] b[k] 51 top[0]top[1]

B[i] and Top[i] for each Stack  B[i] : points to the position immediately to the left of the bottom element of stack[i]  Top[i]: points to the top element of stack[i] 52

 To test empty & full of stack[i] empty: top[i] == b[i] full: top[i] == b[i+1]  Add an item to stack[i] Operations 53 void push(int i, element item) { if (top[i] == b[i+1]) then StackFull(i); else { top[i] = top[i]+1; M[top[i]] = item; } }

Operations  Delete top element from stack[i]  StackFull(i) for stack[i]: Have to check all stacks (all memory elements) If there exists empty space in array M[] == > shift other stacks to give space to stack[i] 54 element pop(int i) { if (top[i] == b[i]) then StackEmpty(i); returnM[top[i]--]; }