Queues Chapter 4.

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

Stacks, Queues, and Linked Lists
Ceng-112 Data Structures I Chapter 5 Queues.
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.
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.
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.
Data Structures(数据结构) Course 5:Queue
Data Structure Dr. Mohamed Khafagy.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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 16 Stack and Queues part2
Objectives of these slides:
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
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 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Review Array Array Elements Accessing array elements
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
Queues Chapter 4.
Using Queues: Coded Messages
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
CENG 213 Data Structure Queue 7/2/2018.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Array Based Stacks.
Queues Mohammad Asad Abbasi Lecture 5
Data Structures: A Pseudocode Approach with C
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Stacks, Queues, and Deques
C++ Plus Data Structures
DATA STRUCTURE QUEUE.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues: Implemented using Arrays
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CSC 248 – fundamentals of Data structure
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Data Structures Using C++ 2E
Presentation transcript:

Queues Chapter 4

4.2 Queues It is a data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front or first. A queue is simply a waiting line that grows or shrinks by adding or taking elements form it. Unlike stack, it’s a structure in which both ends are used. A queue is an FIFO structure. One possible queue implementation is an array(circular array). A more natural queue implementation is a doubly linked list.

Operations to manage the queue are: Clear() isEmpty() enqueue(el) dequeue() firstEl()

Basic Operations on a Queue InitializeQueue: Initializes the queue to an empty state DestroyQueue: Removes all the elements from the queue, leaving the queue empty IsEmptyQueue: Checks whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value false IsFullQueue: Checks whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false

Basic Operations on a Queue Front: Returns the front (first) element of the queue; the queue must exist Back: Returns the last (rear) element of the queue; the queue must exist AddQueue: Adds a new element to the rear of the queue; the queue must exist and must not be full. DeleteQueue: Removes the front element of the queue; the queue must exist and must not be empty.

A queue is a linear list in which data can be inserted at one end, called rear, and deleted from the other end, called the front. It is a first in-first out (FIFO) data structure. no search, no adding in arbitrary positions, no sorting, no access to anything beyond the front and rear elements.

Basic Queue Operations Enqueue: inserts an element at the rear of the queue. grape Data Enqueue apple kiwi apple kiwi grape front rear front rear queue queue operation

Basic Queue Operations cont’ Dequeue: deletes element at the front of the queue. apple Data Dequeue kiwi grape apple kiwi grape front rear front rear queue queue operation

Basic Queue Operations cont’ Queue Front: Examines the element at the front of the queue. apple Data Queue Front apple kiwi grape apple kiwi grape front rear front rear queue operation

Basic Queue Operations cont’ Queue Rear: Examines the element at the rear of the queue. grape Data Queue Rear apple kiwi grape apple kiwi grape front rear front rear queue operation

Queue Linked List Design For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig front rear Conceptual queue 4 front count rear apple kiwi grape fig Physical queue

Queue Linked List Design cont’ queueHead front <node pointer> count <integer> rear <node pointer> end queueHead node data <datatype> next <node pointer> end node 4 front count rear Queue head structure data next node structure

Queue Algorithms Create Queue algorithm createQueue queue.front = null queue.rear = null queue.count = 0 end createQueue count rear front Queue head structure

Enqueue 1 1 2 Queue Queue Before After Case 1: insert into null queue front count rear front count rear Queue Queue 1 plum plum data next newPtr data next newPtr Before After Case 1: insert into null queue front count rear front count rear Queue Queue 1 2 newPtr plum kiwi plum data next data next data next kiwi Before After data next newPtr Case 2: insert into queue

Enqueue If (queue full) end if allocate (newptr) algorithm enqueue Insert (push) data into a queue. Post dataIn has been inserted Return true if successful, false if overflow If (queue full) return false end if allocate (newptr) newptr->data = dataIn newptr->next = null pointer if (queue.count = zero) queue.front = newPtr else queue.rear->next = newPtr queue.rear = newptr queue.count = queue.count + 1 return true end enqueue Enqueue

(Recycled) (Recycled) Dequeue 1 2 1 Queue Queue After Before front count rear Queue front count rear Queue 1 plum plum (Recycled) data next deleteLoc data next After Before Case 1: delete only item in queue front count rear front count rear Queue Queue 2 1 plum kiwi plum kiwi (Recycled) data next data next data next data next deleteLoc Before After Case 2: delete item at front of queue

Dequeue If (queue.count is 0) end if Item = queue.front->data algorithm dequeue This algorithm deletes a node from a queue. Post data at front of queue returned to user through item and front element deleted and recycled Return true if successful, false if overflow If (queue.count is 0) return false end if Item = queue.front->data deleteLoc = queue.front if (queue.count is 1) queue.rear = null pointer queue.front = queue.front->next queue.count = queue.count – 1 recycle (deleteLoc) return true end dequeue Dequeue

Queue Front if (queue.count is 0) end if algorithm QueueFront This algorithm receives the data at the front of the queue without changing the queue contents. Post data passed back to caller Return true if successful, false if underflow if (queue.count is 0) return false end if dataout = queue.front->data return true end QueueFront

Empty Queue return (if queue.count equal 0) end emptyQueue algorithm emptyQueue This algorithm checks to see if a queue is empty. Return true if empty, false if queue has data return (if queue.count equal 0) end emptyQueue

Full Queue allocate (tempPtr) If (allocation successful) else end if algorithm fullQueue This algorithm checks to see if a queue is full. The queue is full if memory cannot be allocated for another node. Return true if full, false if there is room for another node allocate (tempPtr) If (allocation successful) recycle(tempPtr) return false else return true end if end fullQueue

Queue Count return queue.count end Queuecount algorithm Queuecount Returns the number of elements currently in queue. return queue.count end Queuecount

Destroy Queue ptr = queue.front Loop (ptr not null) end loop algorithm destroyQueue This algorithm deletes all data from a queue. Post all data have been deleted and recycled ptr = queue.front Loop (ptr not null) deletePtr = ptr ptr = ptr->next recycle (deletePtr) end loop queue.front = null queue.rear = null queue.count = 0 return end destroyQueue

Priority Queues A priority queue is an ADT with an inserting accessing protocol: only the highest-priority element can be accessed. It is arranged to support access to the highest priority. A priority queue stores collection of entries. Each entry is a (key, value) pair. Applications: Hospital Emergency Rooms Stock market Keys in a priority queue can be arbitrary objects on which an order is defined. Two distinct items in a priority queue can have the same key.

Priority Queues Methods Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k and value x removeMin() removes and returns the entry with smallest key Additional methods minKey(k, x) returns, but does not remove, an entry with smallest key minElement() returns, but does not remove, the element of an item with smallest key size() isEmpty()