Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented.

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

QUEUE Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth. Chanakya.
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.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
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.
CHAPTER 7 Queues.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority 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.”
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
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.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. 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.
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.”
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
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.
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.
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 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
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.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 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.
CS505 Data Structures and Algorithms
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM 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.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Revised based on textbook author’s notes.
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,
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues.
CE 221 Data Structures and Algorithms
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Array based Queues A circular implementation

Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented using an array Some are more efficient that others

Similarly to an array based list and stack, there are many identical data members int[] elements; int currentsize; int maxsize;

Begin by considering a queue, where the front of the queue is considered to ALWAYS be array element zero. The rear of the queue will always the next empty slot, which is the value of currentSize CurrentSize = 0 Front Rear

As each value is enqueued, it is placed in the slot referenced by rear, enqueue(77) yields: 77 CurrentSize = 1 Front Rear

Enqueue(11) yields 7711 CurrentSize = 2 Front Rear

In this implementation, there is no need to have additional data members for front and rear

public void enqueue( int newValue) { if (this.isFull() ) System.out.println("operation aborted queue is full"); else { this.elements[currentSize]= newValue; currentSize++; } }// end enqueue

public int dequeue() { int returnValue =0; if (this.isEmpty() ) System.out.println("Dequeue aborted, queue is empty"); else { returnValue = this.elements[0]; for (int i=0; i < currentSize -1; i++) this.elements[i] = this.elements[i+1]; currentSize--; }// end else return returnValue; }// end dequeue A disadvantage with this implementation is that elements must be shifted with each dequeue

Implementation 2, circular In a circular implementation of a queue there is NO shifting of values. Instead, there are two additional data members, front and rear, each of which reference a particular element in the array and which move as elements are dequeued and enqueued.

public class queue2 { private int[] elements; private int maxSize; private int currentSize; private int front; private int rear; // front and rear are initialized to the first element in the array public queue2(int max) { maxSize = max; currentSize = 0; front= 0; rear = 0; elements = new int[maxSize]; }// end constructor

Enqueue Instead of a “flat” series of cells, the array is viewed as a circle maxSize=8

Front and rear are both initialized to element zero maxSize=8 Front Rear

Each time a value is enqueued, it is inserted into the slot referenced by rear, and rear is incremented maxSize=8 Front Rear 100

This continues as long as the queue is not full, eventually rear will reference the last element maxSize=8 Front Rear

Eventually a value will be enqueued into the last element and rear will be incremented and will equal maxsize, this means rear is past the end of the array, and should be reset to maxSize=8 Front Rear No further enqueues will occur, because the queue is full

public void enqueue( int newValue) { if ( this.isFull()) { throw new QueueException("The queue is full the value can not be added"); } // rear will be past the end of the physical array when // rear == maxsize // if this is true and the queue is not full, set rear to 0 if ( rear == maxSize) rear = 0; elements[rear] = newValue; ++currentSize; ++rear; }

Each time a value is dequeued, it is the one referenced by front, the value will be removed, and front incremented maxSize=8 Front Rear Front will always reference the next value to dequeue

public int dequeue(){ int tempValue=0; if (this.isEmpty()) { throw new QueueException("The queue is empty,there is nothing to remove"); } tempValue = elements[front]; // save the front element ++front; --currentSize; // after incrementing, if front == maxsize, it is past the // end of the physical array, reset to zero if (front == maxSize) front=0; return tempValue; }