7- 1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
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 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
Data Structures for Media Queues. Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
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.
Stacks, Queues & Deques CSC212.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
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.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
7.2 Priority Queue ADTs Priority queue concepts
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 7 Queues Introduction Queue applications Implementations.
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.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Linked Lists, Queues, Stacks
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Data Structure By Amee Trivedi.
Double-Ended Queues Chapter 5.
Chapter 4 The easy stuff.
Doubly Linked List Review - We are writing this code
Stack and Queue APURBO DATTA.
Algorithms and Data Structures
Priority Queue.
Introduction to Data Structure
Chapter 16-2 Linked Structures
Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Chapter 16 Linked Structures
8 List ADTs List concepts. List applications.
Presentation transcript:

Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.

7- 2 Linked Data Structures Linked data structures (or simply data structures), are structures represent objects linked in one of the following types: Linear data structures: –Linked lists: single, double, circuliar. –Stack (LIFO). –Queue (FIFO). Non-Linear data structures : –Tree: binary, multi-branch. –Graph.

7- 3 Queues Definition of a Queue: A queue is a linear list in which data is inserted at one end (the rear or tail), and from which data is removed from the other end (the front or head). FIFO. The length of a queue is the number of elements it contains. An empty queue has length zero. Queues are used to help divide subtasks.

7- 4 Priority Queues What if items in the queue have an order –Usually termed a priority queue –We must sort the items so that the highest (lowest ) priority item is removed first Items have some ordering relation –It doesn’t matter much what it is –As long as there’s some way to define order

7- 5 Queue concepts (1) A queue is a first-in-first-out sequence of elements. Elements can added only at one end (the rear of the queue) and removed only at the other end (the front of the queue).

7- 6 Queue concepts (2) Illustration (queue of cars):

7- 7 Queue applications Print server –maintains a queue of print jobs. Disk driver –maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system) –maintains a queue of processes awaiting a slice of machine time. Services Queues, i.e. waiting in line for a bank teller

7- 8 Queue ADT: requirements Requirements: 1)It must be possible to make a queue empty. 2)It must be possible to test whether a queue is empty. 3)It must be possible to obtain the length of a queue. 4)It must be possible to add an element at the rear of a queue. 5)It must be possible to remove the front element from a queue. 6)It must be possible to access the front element in a queue without removing it.

7- 9 Queue ADT: contract (1) Possible contract, in C++: class Queue { // Each Queue object is a queue whose elements are / / objects. ///////////// Accessors /////////////// bool isEmpty (); // Return true if and only if this queue is empty. int size (); // Return this queue’s length. char getFirst (); // Return the element at the front of this queue.

7- 10 Queue ADT: contract (2) Possible contract (continued): /////////// Transformers //////////// void clear (); // Make this queue empty. void addLast (char elem); // Add elem as the rear element of this queue. char removeFirst (); // Remove and return the front element of this queue. }

7- 11 Invariant: 0frontrear–1maxlen–1 element Implementation of queues using arrays (1) Consider representing a bounded queue (length  maxlen) by: –a variable length, containing the current length –variables front and rear –an array elems of length maxlen, containing the queued elements in elems[front…rear–1]: Empty queue: 0maxlen–1front=rear unoccupied rear elementfront element

7- 12 Implementation using arrays (2) Animation (with maxlen = 6): Initially: front 0 rear elems 0 length 0 Hamed 1 Murad 2 Baher 3 Layla 45 0 front 4 rear elems 4 length After adding Hamed, Murad, Baher, Layla: 0 Hamed 1 Murad 2 Baher 3 Layla 4 Maged 5 0 front 5 rear elems 5 length After adding Maged: 01 Murad 2 Baher 3 Layla 4 Maged 5 1 front 5 rear elems 4 length After removing the front element: 012 Baher 3 Layla 4 Maged 5 2 front 5 rear elems 3 length After removing the front element: 012 Baher 3 Layla 4 Maged 5 Radi 2 front 0 rear elems 4 length After adding Radi:

7- 13 Implementation using arrays (3) If we must shift elements along the array, operations addLast and removeFirst will have time complexity O(n). We can avoid this if we use a “cyclic array” instead of an ordinary array.

7- 14 Implementation of queues using SLLs (1) Represent an (unbounded) queue by: –a SLL, whose first node contains the front element, and whose header contains links to the first node (front) and last node (rear). –a variable length (optional). Invariant: element front rear Empty queue: front rear Illustration: front rear Hamed Murad Baher Layla

7- 15 Implementation using SLLs (2) C++ implementation: / / Queue Node declaration struct QueNode{ char Data; // Node Information struct QueNode* Next; // Node Communication };

7- 16 Implementation using SLLs (2) C++ implementation (continued): // Queue ADT declaration class QueSLL { private: QueNode *Front, *Rear; int Length; public: //////////// Constructor //////////// QueSLL () { Front = Rear = NULL; Length = 0;} ;

7- 17 Implementation using SLLs (2) C++ implementation (continued): //////////// Accessors //////////// int Size () { return Length;}; void PrintQue(); int QueIsEmpty() { // return (Front==NULL); if (Front==NULL) { cout << "Queue is Empty" << endl; return TRUE; } else return FALSE; }

7- 18 Implementation using SLLs (2) C++ implementation (continued): //////////// Transformers //////////// // Add node to the the Que rear void Add(char c) { QueNode* p; // Create a node in memory p=new QueNode; // Fill node p->Data = c; p->Next = NULL; // Add node to Queue if (Front == NULL) Front = Rear = p; // If Que is empty else{ Rear->Next=p; Rear=Rear->Next; } Length++; }

7- 19 Implementation using SLLs (2) C++ implementation (continued): //////////// Transformers //////////// // Delete a node from the the Que front void Delete() { QueNode* p = Front; if(p){ Front=Front->Next; delete p; Length--; } else { cout << "Empty Queue..." << endl; } } }; // End of class QueSLL

7- 20 Implementation using SLLs (3) Possible implementation for main(): void main() { QueSLL Qlist; Qlist.Add('a'); Qlist.Add('b'); Qlist.Add('c'); Qlist.PrintQue(); cout << "Number of Nodes in Queue = " << Qlist.Size() << endl; Qlist.Add('a'); Qlist.Add('b'); Qlist.Add('c'); Qlist.Add('d'); Qlist.Add('e'); Qlist.Add('f'); cout << "Number of Nodes in Queue = " << Qlist.Size() << endl; while (!Qlist.QueIsEmpty()) cout << Qlist.Delete() << endl; Qlist.Delete(); } Que_SLL2.CPP

7- 21 Output a b c Number of Nodes in Queue = 3 a b c a b c d e f Number of Nodes in Queue = 9 Queue is Empty Number of Nodes in Queue = 0 Implementation using SLLs (8)

7- 22 Comparison of implementations We can similarly implement a priority queue using a sorted or an unsorted array. Time complexities of main operations: OperationUnsorted SLLUnsorted Array Add() O(1) Delete() O(n)O(n)O(n)O(n) PrintQue() O(n)O(n)O(n)O(n)