Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Stacks, Queues, and Linked Lists
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Chapter 24 Lists, Stacks, and Queues
Chapter 6 Queues and Deques.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
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.
Stacks, Queues, and Deques. 2 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 Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Stacks, Queues & Deques CSC212.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
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.
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.
Stacks, Queues, and Deques
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Chapter 7 Stacks II CS Data Structures I COSC 2006
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
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,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Stacks and Queues Phil Tayco Slide version 1.0 Feb. 16, 2015.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
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.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
 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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
Week 4 - Monday CS221.
Chapter 4 The easy stuff.
Chapter 15 Lists Objectives
Objectives In this lesson, you will learn to: Define stacks
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Programming Abstractions
Priority Queue.
Data Structures and Database Applications Queues in C#
Stacks and Queues.
DATA STRUCTURE QUEUE.
Linked Lists: Implementation of Queue & Deque
Programming Abstractions
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Chapter 9 Linked Lists.
Presentation transcript:

Queues

Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops) occur at one end of the list. Queues are Lists in which the insertion and deletion occur at different ends of the list Head of QueueTail of Queue insertdelete

Insertions takes place at the tail of the Queue whereas deletions occur at the head of the Queue. Insertion into a Queue is called enqueing while deletion is called dequeing. When deleting an element from the head of the Queue, the element is retrieved and output from the deque() operation before it is deleted.

Since the item inserted first ( the first enque ) is the first item deleted then a Queue is known as a First-In-First-Out (FIFO) List. Queuing Applications There are many applications of Queues in Computer Science just as in real life: An electronic air traffic system will place approaching airplanes in a landing queue. A network printer will queue print jobs in the order they are received from the clients.

Priority Queues A refinement of the basic Queue class is the Priority Queue. In many situations, items being queued have some priority associated with them. For example, in an A&E ward patients with more severe conditions are treated before patients with less severe conditions, even though those patients may have been in the queue before them.

In computing terms, priority queues are essential to the well-being of a computer system. An operating system will handle requests from system administrators or troubled processes before normal tasks enqued before them because they have higher priorities. Enqueing in a Priority Queue involves traversing the Queue starting at the tail until an element is found with the same or higher priority. The new item is then queued after this item in the Queue. x p(6) y p(5) z p(3) insert f with priority 4 here tail head

Using this method it should be trivial to see that items with the highest priority collect at the head of the Queue. For items with the same priority, the FIFO rule applies. Queue Implementations Since a Queue is a special case of the List ADT, it seems logical to use the properties of a List to construct a Queue.

Unfortunately, Queues relying on a linked list implementation suffer from some minor problems, while queues represented by array-based lists fair even worse. Linked-List Implementations The main problem associated with simple linked-list implementations of Queues is that only one pointer is maintained (to the first node in the list).

By modifying the List class with an extra end pointer, the items at the end of the queue can be accessed in one operation, independent of the size of the list.

A further complication with this approach concerns re-assigning the end pointer to the last node in the list after a delete operation is completed for a singly linked list. If an item is deleted from the end of a singly linked list, then re- assigning the end pointer takes n operations since we cannot traverse backwards through the list to find the previous node.

6.5 first NULL end singly linked List delete last element

first end NULL Re-assignment of end pointer requires list traversal. singly linked List

When inserting at the end of a linked list, there is no node traversal required as we can re-assign the end pointer directly. 6.5 first NULL end singly linked List inserted element

6.5 first NULL end singly linked List inserted element direct re-assignment of end pointer

In general, if a linked list is used to implement a Queue, insertions should take place at the end of the list and deletions at the start of the list. In other words, the head of the Queue must be the start of the List, while the tail of the Queue is the end of the List.

Linked List Implementation of a Queue Class class Queue : public List { private: Node* end; public: Queue(); void enqueue(Element); void dequeue(Element); }

Queue::Queue() : List(), end(NULL) {} /* This code is written non-defensively. We should first check whether the Queue is not empty */ Element Queue::dequeue() { Element e = retrieve(0); delete(e); return e; }

void Queue::enqueue(Element e) { /* If the queue is empty, insert the element at the front and make the end pointer point to it */ if (end == NULL) { insert(e,0); /* The following requires that first pointer has become a protected member in the List class */ end = first; } else { /* First make a new node */ Node* n = new Node(e); /* Now attach it to the end of the Queue */ end->setNext(n); /* and reset end */ end = n; } }

Queues using Array-based List Implementations Since we will be enqueing or dequeing elements at the end of a list only array-based lists with an end-pointer will be considered. Once again there are two choices when using an array-based list: 1. Enque at the end of the List and Deque at the start; 2. Deque at the end of the List and Enque at the start.

1. Enque at the end of the List and Deque at the start Enqueing at the end of the List takes one operation. We move to the end of the list using the end pointer, insert a new item and re-assign the end pointer. Unfortunately, dequeing at the start of the list now takes n operations. When the element is deleted all subsequent elements need to be shifted towards the beginning of the array.

2. Deque at the end of the List and Enque at the start Dequeing at the end of the List takes one operation. We move to the end of the list using the end pointer, delete the element and re-assign the end pointer to the previous array element. Unfortunately, enqueing at the start of the list now takes n operations. When a new element is inserted all subsequent elements need to be shifted up by one position.

Whichever method is used for an array-based list implementation, the queuing operation at the start of the list will take n operations.