CS505 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
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.
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.
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 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Structures Using C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CSCE 210 Data Structures and Algorithms
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Chapter 7 Queues.
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queues.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Queues.
Chapter 16-2 Linked Structures
Queues.
Chapter 19: Stacks and Queues.
CSC 143 Queues [Chapter 7].
ADT list.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Pointers & Dynamic Data Structures
Containers: Queue and List
CE 221 Data Structures and Algorithms
Queues.
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

CS505 Data Structures and Algorithms Queue 1 1

The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues 2 2

1. introduction to the Queue Data Structure A simple data container consisting of a linear list of elements Access is by position (order of insertion) Insertions at one end (rear) , deletions at another end (front) First In First Out (FIFO) structure Two basic operations: enqueue: add to rear dequeue: remove from front 3 3

An Illustration 4 4

Enqueue and Dequeue Problem of Array representation Solution using Circular Array. When last array element is reached, we move back to start The queue is viewed as a circular array To enqueue: rear = (rear + 1) % size To dequeue: front = (front + 1) % size Both rear and front advance clockwise 5 5

Some Queue Applications Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering 6 6

Queue Class Operations construct: construct an empty queue queueIsEmpty  bool : return True if queue is empty queueIsFull  bool : return True if queue is full enqueue(el) : add element (el) at the rear dequeue(el): retrieve and remove the front element queueFront(el): retrieve front without removing it queueLength  int : return the current queue length 7 7

2. Array Based Queue Class Definition The queue may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor (default is 128) The queue ADT will be implemented as a template class to allow for different element types. 8 8

A Queue Class Definition // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template <class Type> class Queuet { public: Queuet (int nelements = 128); // Constructor Queuet (const Queuet <Type> &); // Copy Constructor ~Queuet (); // Destructor 9 9

A Queue Class Definition // Member Functions void enqueue(Type ); // Add to rear void dequeue(Type &); // Remove from front void queueFront(Type &) const; // Retrieve front bool queueIsEmpty() const; // Test for Empty queue bool queueIsFull() const; // Test for Full queue int queueLength() const; // Queue Length private: Type *queue; // pointer to dynamic array int front, rear, count, MaxSize; }; #endif // QUEUET_H #include "Queuet.cpp" 10 10

3. Linked Queues A Queue can be implemented as a linked structure. Requires more space than array implementations, but more flexible in size. Two pointers are needed: front for dequeue and rear for enqueue 11 11

Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main queue class. class node // Hidden from user { public: Type e; // stack element node *next; // pointer to next node }; // end of class node declaration typedef node * NodePointer; NodePointer front , rear; // pointers 12 12

Enqueue Operation 2 1 pnew rear front New enqueue(v): NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; 13 13

Dequeue Operation 1 rear cursor front 3 2 dequeue(v): v = front->e; front = front->next; delete cursor; 14 14

Linked Queue Class // File: QueueL.h // Linked List Queue class definition #ifndef QUEUEL_H #define QUEUEL_H template <class Type> class QueueL { public: QueueL(); // Constructor ~QueueL(); // Destructor void enqueue(Type ); // Add to rear 15 15

Linked Queue Class void dequeue(Type &); // Remove from front void queueFront(Type &) const; // retrieve front bool queueIsEmpty() const; // Test for Empty queue int queueLength() const; // Queue Length private: // Node Class class node { public: Type e; // queue element node *next; // pointer to next node }; // end of class node declaration 16 16

Linked Queue Class typedef node * NodePointer; NodePointer front , rear; // Pointers int count; // length }; #endif // QUEUEL_H #include "QueueL.cpp" 17 17

Part of Implementation file template <class Type> void QueueL<Type>::enqueue(Type v) { NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; if(queueIsEmpty()) front = pnew; rear = pnew;} else {rear->next = pnew; rear = pnew;} count++; } 18 18

Part of Implementation file template <class Type> void QueueL<Type>::dequeue(Type &v) { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else v = front->e; cursor = front; front = front->next; delete cursor; count--; } 19 19

Part of Implementation file template <class Type> void QueueL<Type>::queueFront(Type &v) const { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else { v = front->e; } } 20 20

Implement Queue using ADT of linked List Exercise Implement Queue using ADT of linked List