1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
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.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
Chapter 6 Queue. Learning Objectives ● Describe the behavior of a queue. ● Enumerate the primary operations supported by a queue. ● Learn about the UNIX.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Linear Data Structures
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Computer Engineering Rabie A. Ramadan Lecture 6.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
 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.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Review Array Array Elements Accessing array elements
Queues.
Chapter 6 Queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Data Structures and Algorithms for Information Processing
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Queues.
Getting queues right … finally (?)
Lecture 9: Stack and Queue
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues

2 Rules for Calculator use User enters a command: ?, =, +, -, *, or / ? means the next entry is a number. = means print the result (top of stack). All others mean perform the given arithmetic operation.

3 Processing commands void do_command(char command,Stack &numbers) { double p,q ; switch (command){ case '?': break; case '=': break; cout <<"Enter a real number:" ; cin >>p ; if (numbers.push(p)==overflow) { cout <<"Warning:Stack full,lost number "<<endl; } if (numbers.top(p) == underflow) { cout << "Stack empty." << endl; } else { cout << p <<endl; }

4 More number processing case '+': break; //Add options for further user commands. } //end switch } //end do_command if (numbers.top(p)==underflow){ cout <<"Stack empty "<<endl ; } else { numbers.pop(); if (numbers.top(q)==underflow){ cout <<"Stack has just one entry "<<endl ; numbers.push(p); } else { numbers.pop(); if (numbers.push(q + p)==overflow) { cout <<"Warning:Stack full,lost result "<<endl ; }

5 Queues A queue is a list in which items are added at one end and deleted from the other end. It is like line of people waiting to purchase tickets: New people can join the line at the back of the line. People at the front of the line are served first (and then they leave the line). This is an example of “First in, First out”, FIFO. Adding an item is called “appending” to the queue. Deleting an item is called “serving” from the queue.

6 Queue Terminology Append: Add an item to the back of the queue. Also known as: Insert, Push or enqueue. Serve: Delete an item from the front of the queue. Also known as: Delete, Pop or dequeue. Retrieve: Return the value of the item at the front of the queue. Front: The first item in the queue. Rear: The last item in the queue.

7 Using Queues A Queue class should have the following methods: append(item)//Add an item to the rear of the queue serve( )//Delete an item from front of queue retrieve( )//Return value of front item empty()//Return true if queue is empty.

8 Queue Applications Any application where a group of items is waiting to use a shared resource will use a queue. For example: A queue of jobs waiting to use a printer (the print queue). A queue of jobs waiting to use the processor (in a multitasking system).

9 Implementing Queues with Arrays Three ways to implement queues: The physical model. The Linear model. The Circular model.

10 The Physical Model The front of the queue is always at index 0. The rear of the queue varies depending on the length of the queue. append: add 1 to rear; assign new item to entry[rear] retrieve: return entry[0] serve: move each entry 1 to left. Decrement rear by 1. The problem: serve is very inefficient.

11 The Linear Model The front of the queue varies as items are served The rear of the queue varies depending on the length of the queue. append: add 1 to rear; assign new item to entry[rear] retrieve: return entry[front] serve: increment front by 1 The problem: use of memory is inefficient (the queue moves).

12 The Circular Model The front and rear are the same as the linear model, except: The queue wraps around when the end of the array is reached. append: add 1 to rear, if off the end of array, rear = 0; assign new item to entry[rear] retrieve: return entry[front] serve: increment front by 1, if off the end of array, front = 0; How do we do this in C ++ ?

13 Detecting an Empty Queue The idea: in an empty queue, the front is behind the rear: front == rear + 1. The problem: this is also true for a full queue! The possible solutions: 1) Always leave an empty position. Then the queue is full when front == rear + 2 2) Use a boolean flag that is set to true when the queue becomes full. 3) Use a counter to keep track of number of items in the queue.