EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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.
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.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.”
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
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.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
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.”
Queues and Priority Queues
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.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Data Structures & Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
CHP-3 STACKS.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
CHAPTER 5 QUEUE v2 by Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
 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 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
CC 215 Data Structures Queue ADT
CENG 213 Data Structure Queue 7/2/2018.
Queues.
Stack and Queues Stack implementation using Array
CSC 143 Queues [Chapter 7].
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Data Structures & Programming
Presentation transcript:

EC-211 DATA STRUCTURES LECTURE 9

Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at one end (Rear). – Elements are removed from the other end (Front). The element added first is also removed first (FIFO: First In, First Out).

Example ABC Front Rear BC Front Rear ABC Front Rear DE (a) Initial Queue (b) After removing 1 element (c) Inserting 2 more elements

Applications of the ADT Queue Print Queue Job Scheduling Event Simulation

Basic Operations Insert – inserts item at the rear of queue. Remove – deletes the front element of the queue – Attempt to remove an element from an empty queue is called an underflow. Empty – returns false or true depending on whether or not the queue contains any elements

Queue Implementation: Dynamic Insert: same logic as insertAtEnd of linked list Remove: same logic as removeFromstart of linked list

Queue Implementation: Static //Declaring a Queue Data-type: # define MAX 100 struct queue { int items[MAX]; int Front; int Rear; int Count; };

Queue Implementation (static) void initialize () { Front=0; Rear=MAX-1; Count=0; } bool empty () { return (Count == 0); }

Queue Implementation (static) void insert(int NewItem) { if (Count == MAX) { cout<<“queue overflow”; exit(1) ; } Rear = (Rear+1) % MAX; Items[Rear] = NewItem; ++(Count); }

Queue Implementation (static) int remove () { if (empty()) { cout <<“queue underflow”; exit(1); } int x= Items[Front]; Front = (Front+1) % MAX; --(Count); return x; }

Example: Reading a String of Characters When You Enter Characters at a Keyboard, the System Must Retain Them in the Order in Which you Typed Them. It Could Use a Queue for This Purpose.

Example: Reading a String of Characters //read a string of characters from a single line of input into a queue queue Q; while ( not end of line) { Read a new character Ch insert( &q, Ch) } Once the characters are in a queue, the system can process them as necessary

Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. able was I ere I saw elba We will read the line of text into both a stack and a queue. Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

Example: recognizing palindromes

IsPal(Str) //Determines whether the string Str is a palindrome. //initialize a queue and a stack queue Q; stack S; //insert each character of the string into both the queue //and the stack for( I=0 through strlen(Str)-1) {NextChar = Str[ I ] insert (&Q, NextChar) push(&S, NextChar) }//end for //compare the queue characters with the stack characters CharactersAreEqual = TRUE cout << "Enter string: " << endl; while(cin.peek() != '\\n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); }

Example: recognizing palindromes while( Q is not empty and CharactersAreEqual) { if ( remove(&Q) equals pop(&S)) continue; else CharacterAreEqual = FALSE }//end while return CharactersAreEqual

Priority Queue Is a Value-Oriented ADT and is More Sophisticated Than Position-Oriented ADTs Like Stacks and Queues Priority Value Indicates, For Example a Task’s Priority for Completion A Priority Queue can be Viewed as a Sequence of Values Which are Sorted According to Priority An Element of Higher Priority is Processed (removed from queue) Before any Element of Lower Priority Two Elements With the Same Priority are Processed (removed from queue) According to the Order in Which They Were Added to the Queue

Priority Queue Implementations –Sorted Array Difficult to Insert Value Very Easy to Find next Value to remove –Unsorted Array Easy to Insert Value Difficult to Find next Value to remove