Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
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.
Stacks Chapter 11.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
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.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queue Overview Queue ADT Basic operations of queue
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
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
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Lecture 7 Feb 19 Goals: l subset generation program (related to lab # 3) l applications of stack l Postfix expression evaluation l Convert infix to postfix.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)
Stacks, Queues & Deques CSC212.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Evaluation of Expressions
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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 AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures & Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Stacks.
COMPSCI 107 Computer Science Fundamentals
Algorithms and Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Presentation transcript:

Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue

Converting infix to Postfix expression Recall the postfix notation from last lecture. Example: a b c + * It represents a*(b + c) What is the postfix form of the expression a + b*(c+d)? Answer: a b c d + * + Observation 1: The order of operands in infix and postfix are exactly the same. Observation 2: There are no parentheses in the postfix notation.

Example : (a) Infix: – 4 Postfix: – (b) Infix: * 4 Postfix: * + The operators of the same priority appear in the same order, operator with higher priority appears before the one with lower priority. Rule: hold the operators in a stack, and when a new operator comes, push it on the stack if it has higher priority. Else, pop the stack off and move the result to the output until the stack is empty or an operator with a lower priority is reached. Then, push the new operator on the stack.

Applying the correct rules on when to pop Assign a priority: ( * and / have a higher priority than + and – etc.) Recall: Suppose st.top() is + and next token is *, then * is pushed on the stack. However, ( behaves differently. When it enters the stack, it has the highest priority since it is pushed on top no matter what is on stack. However, once it is in the stack, it allows every symbol to be pushed on top. Thus, its in-stack-priority is lowest. We have two functions, ISP (in-stack-priority) and ICP (incoming-priority).

In-stack and in-coming priorities icp isp +, – 1 1 *, / ** ( 4 0

Dealing with parentheses An opening parenthesis is pushed on the stack (always). It is not removed until a matching right parenthesis is encountered. At that point, the stack is popped until the matching ( is reached. Example: (a + b * c + d)* a Stack: ( Output: a Stack: ( + Output: a Stack: ( + Output: a b Stack: ( + * Output: a b Stack: ( + * Output: a b c Stack: ( + + Output: a b c * Stack: ( + + Output: a b c * d Stack Output: a b c * d + + Stack * Output: a b c * d + + Stack * Output: a b c * d + + a Stack Output: a b c * d + + *

Code for conversion (infix to postfix) string postfix() { Stack st(100); string str =""; token tok = get_token(); string cur = tok.get_content(); while (tok.get_op_type() != -1) { if (tok.get_value() == 1) str+= cur + " "; else if (cur == ")") { while (st.Top()!= "(") str += st.Pop() +" "; string temp1 = st.Pop(); }

else if (!st.IsEmpty()) { string temp2 = st.Top(); while (!st.IsEmpty() && icprio(cur) <= isprio(temp2)) { str+= temp2 + " "; string temp = st.Pop(); if (!st.IsEmpty()) temp2 = st.Top(); } if (tok.get_value() != 1 && tok.get_content()!= ")") st.Push(cur); current++; tok = get_token(); cur = tok.get_content(); } while (!st.IsEmpty()) { str += st.Pop() + " "; cout << "string at this point is " << str << endl; } return str; }

Queue Overview Queue ADT –FIFO (first-in first-out data structure) Basic operations of queue –Insert, delete etc. Implementation of queue –Array

Queue ADT Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. –Like customers standing in a check-out line in a store, the first customer in is the first customer served.

Insert and delete Primary queue operations: insert and delete Like check-out lines in a store, a queue has a front and a rear. insert - add an element at the rear of the queue delete – remove an element from the front of the queue Insert Remove rearfront

Implementation of Queue Queues can be implemented as arrays Just as in the case of a stack, queue operations ( insert delete, isEmpty, isFull, etc.) can be implemented in constant time

Implementation using Circular Array When an element moves past the end of a circular array, it wraps around to the beginning, e.g. –OOOOO7963  (insert(4)) 4OOOO7963 How to detect an empty or full queue, using a circular array? –Use a counter for the number of elements in the queue. –size == ARRAY_SIZE means queue is full –Size == 0 means queue is empty.

Queue Class Attributes of Queue –front/rear: front/rear index –size: number of elements in the queue –Q: array which stores elements of the queue Operations of Queue –IsEmpty() : return true if queue is empty, return false otherwise –IsFull() : return true if queue is full, return false otherwise –Insert(k): add an element to the rear of queue –Delete(): delete the element at the front of queue

class queue { private: point* Q[MSIZE]; int front, rear, size; public: queue() { // initialize an empty queue front = 0; rear = 0; size = 0; for (int j=0; j < MSIZE; ++j) Q[j] = 0; } void insert(point* x) { if (size != MSIZE) { front++; size++; if (front == MSIZE) front = 0; Q[front] = x; }

point delete() { if (size != 0) { rear++; if (rear == MSIZE) rear = 0; point temp(Q[rear]->getx(), Q[rear]->gety()); size--; return temp; }; } bool isEmpty() { return (size == 0); } bool isFull() { return (size == MSIZE); } };

Breadth-first search using a queue BFS: application that can be implemented using a queue. Our application involves finding the number of distinct letters that appear in an image and draw bounding boxes around them. Taken from the output of the BFS algorithm