 Balancing Symbols 3. Applications

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
§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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Stacks, Queues & Deques CSC212.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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.
Objectives of these slides:
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
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.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Infix to postfix conversion
CE 221 Data Structures and Algorithms
CS 201 Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Stacks Stack: restricted variant of list
Data Structures – Week #3
Stacks and Queues 1.
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Presentation transcript:

 Balancing Symbols 3. Applications §3 The Stack ADT 3. Applications  Balancing Symbols Check if parenthesis ( ), brackets [ ], and braces { } are balanced. Algorithm { Make an empty stack S; while (read in a character c) { if (c is an opening symbol) Push(c, S); else if (c is a closing symbol) { if (S is empty) { ERROR; exit; } else { /* stack is okay */ if (Top(S) doesn’t match c) { ERROR, exit; } else Pop(S); } /* end else-stack is okay */ } /* end else-if-closing symbol */ } /* end while-loop */ if (S is not empty) ERROR; } T( N ) = O ( N ) where N is the length of the expression. This is an on-line algorithm.

§3 The Stack ADT  Postfix Evaluation 〖Example〗An infix expression: a  b  c  d  e A prefix expression:   a  b c  d e A postfix expression: a b c   d e   operator with the highest precedence Reverse Polish notation operator operand 〖Example〗 6 2  3  4 2   = ? 8 Get token: 6 ( operand ) Get token: 2 ( operand ) Get token:  ( operator ) Get token: 3 ( operand ) 2 top Get token:  ( operator ) Get token: 4 ( operand ) 8 3 4 2 top top top top top Get token: 2 ( operand ) Get token:  ( operator ) 6  2 = 3 6 8 3 top top top top top top top top Get token:  ( operator ) 3 3 Pop: 8  = 0 top top top top top 4  2 = 8  = 8 8 T( N ) = O ( N ). No need to know precedence rules. 1/9

    Infix to Postfix Conversion 〖Example〗 a  b  c  d = ? §3 The Stack ADT  Infix to Postfix Conversion 〖Example〗 a  b  c  d = ? a b c   d  Note:  The order of operands is the same in infix and postfix.  Operators with higher precedence appear before those with lower precedence. Isn’t that simple? Wait till you see the next example... Output: a b c   d  Get token: a (operand) Get token:  (plus) Get token: b (operand) Get token:  (times) Get token: c (operand) Get token:  (minus)    ?  top Get token: d (operand)    ?    ?   top top top top top top 2/9

+ (   〖Example〗 a  ( b  c )  d = ? a b c   d  a b c   d  §3 The Stack ADT 〖Example〗 a  ( b  c )  d = ? a b c   d  Output: a b c   d  Get token: a (operand) Get token:  (times) Get token: ( (lparen) Get token: b (operand) + top Get token:  (plus) Get token: c (operand)   ( ? ( top top Get token: ) (rparen) Get token:  (divide) (   ?   top top top Get token: d (operand)    ? top top top NO?! T( N ) = O ( N ) 3/9

 Never pop a ( from the stack except when processing a ) . §3 The Stack ADT Solutions:  Never pop a ( from the stack except when processing a ) .  Observe that when ( is not in the stack, its precedence is the highest; but when it is in the stack, its precedence is the lowest. Define in-stack precedence and incoming precedence for symbols, and each time use the corresponding precedence for comparison. Note: a – b – c will be converted to a b – c –. However, 2^2^3 ( ) must be converted to 2 2 3 ^ ^, not 2 2 ^ 3 ^ since exponentiation associates right to left. 4/9

Due: Thursday, October 12th, 2006 at 10:00pm Laboratory Project 2 Desk Calculator Due: Thursday, October 12th, 2006 at 10:00pm Detailed requirements can be downloaded from http://10.71.45.99/list.asp?boardid=47 Courseware Download Don’t forget to sign you names and duties at the end of your report. 5/9

Home work: p.82 3.21 Implement Two Stacks §3 The Stack ADT  Function Calls -- System Stack Recursion can always be completely removed. Non recursive programs are generally faster than equivalent recursive programs. However, recursive programs are in general much simpler and easier to understand. f p Well, if 1 million elements are not enough to crash your program, try a larger one. Old Frame Pointer s p What will happen if L contains 1 million elements? Return Address s p Home work: p.82 3.21 Implement Two Stacks What’s wrong with it? Local Variables s p tail recursion f p f p Stack Frame s p s p Return Address void PrintList ( List L ) { if ( L != NULL ) { PrintElement ( L->Element ); PrintList( L->next ); } } /* a bad use of recursion */ void PrintList ( List L ) { top: if ( L != NULL ) { PrintElement ( L->Element ); L = L->next; goto top; /* do NOT do this */ } } /* compiler removes recursion */ 6/9

4 3 2 1 2 1 2 1 §4 The Queue ADT  int IsEmpty( Queue Q ); A queue is a First-In-First-Out (FIFO) list, that is, an ordered list in which insertions take place at one end and deletions take place at the opposite end. 3 2 Objects: A finite ordered list with zero or more elements. 1 Operations:  int IsEmpty( Queue Q );  Queue CreateQueue( );  DisposeQueue( Queue Q );  MakeEmpty( Queue Q );  Enqueue( ElementType X, Queue Q );  ElementType Front( Queue Q );  Dequeue( Queue Q ); 2 1 2 1 7/9

Job 1 Job 2 Job 3 Job 4 Job 5 Job 6 Job 7 §4 The Queue ADT 2. Array Implementation of Queues (Linked list implementation is trivial) struct QueueRecord { int Capacity ; /* max size of queue */ int Front; /* the front pointer */ int Rear; /* the rear pointer */ int Size; /* Optional - the current size of queue */ ElementType *Array; /* array for queue elements */ } ; 〖Example〗 Job Scheduling in an Operating System 1 2 3 4 5 6 Job 1 Job 2 Job 3 Job 4 Job 5 Job 6 Job 7 Front Rear Rear Rear Rear Rear Rear Front Rear Front Rear Enqueue Job 1 Enqueue Job 2 Enqueue Job 3 Dequeue Job 1 Enqueue Job 4 Enqueue Job 5 Enqueue Job 6 Dequeue Job 2 Enqueue Job 7 Enqueue Job 8 8/9

Home work: p.82 3.26 Implement deque §4 The Queue ADT Circular Queue: [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] Rear Question: Why is the queue announced full while there is still a free space left? Rear Do you have a better idea? Enqueue Job 1 Rear Front Rear Front Job 5 Job 6 Are you kidding? Of course I do! Enqueue Job 2 Home work: p.82 3.26 Implement deque Enqueue Job 3 Job 4 Job 1 Dequeue Job 1 Enqueue Job 4 Rear Job 3 Job 2 Enqueue Job 5 Rear The queue is full Enqueue Job 6 Front Enqueue Job 7 Rear Note: Adding a Size field can avoid wasting one empty space to distinguish “full” from “empty”. Do you have any other ideas? 9/9