Data Structures & Programming

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

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
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.
CHAPTER 7 Queues.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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.
Queues.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks And Queues Chapter 18.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
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.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Lecture No.09 Data Structures Dr. Sohail Aslam
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queues.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Memory Organization Process 1 (browser) Code Process 3 (word)
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: 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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
ADT Queue (Array Implementation)
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Stacks, Queues, and Deques
Stacks and Linked Lists
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
Presentation transcript:

Data Structures & Programming Queue Golnar Sheikhshab

Queue Abstract Data Type First in First out (FIFO) container of objects Elements enter the queue at the rear and are removed from front Some applications: Simulating real-world queues Multi-tasking in operating systems

Queue ADT Supported functions: enqueue(e): Insert element e at the rear of the queue. dequeue(): Remove element at the front of the queue; an error occurs if the queue is empty. front(): Return, but do not remove, a reference to the front element in the queue; an error occurs if the queue is empty. size(): Return the number of elements in the queue. empty(): Return true if the queue is empty and false otherwise.

STL Queue #include <queue> using std::queue; // make queue accessible queue myQueue; // a queue of floats Similar to other STL containers, queue doesn't throw exceptions. It may very well cause the program to crash though. STL queue has a back() method too and calls enqueue and dequeue, push and pop respectively.

An informal interface for queue template class Queue { // an interface for a queue public: int size() const; // number of items in queue bool empty() const; // is the queue empty? const E& front() const throw(QueueEmpty); // the front element void enqueue (const E& e); // enqueue element at rear void dequeue() throw(QueueEmpty); // dequeue element at front };

Make it a formal interface (an abstract class) template class Queue { // an interface for a queue public: virtual int size() const = 0; // number of items in queue bool empty() const; // is the queue empty? const E& front() const throw(QueueEmpty); // the front element void enqueue (const E& e); // enqueue element at rear void dequeue() throw(QueueEmpty); // dequeue element at front };

An array based implementation What is the time complexity of functions if the front of the queue is index 0 of the array? the rear of the queue is index 0 of the array? Can we have an array based implementation where all operations are of O(1)?

Using an Array in a Circular Way We keep three variables f, r, and n for front, rear and size We use modulo operation to wrap around the array

Implementation of queue using circular array

Implementing a Queue with a Circularly Linked List

Circular Linked List ADT (reminder) front() and back() return the data in front and back nodes add(e) adds e right after the cursor remove() removes the element right after the cursor advance() moves the cursor forward

Reading material Section 5.2 of the textbook