CSCE 3110 Data Structures & Algorithm Analysis

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

© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
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.
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.”
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
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.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
CSCE 3110 Data Structures & Algorithm Analysis
Review Array Array Elements Accessing array elements
UNIT II Queue.
Queues Chapter 4.
Queues Rem Collier Room A1.02
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
CSCE 3100 Data Structures and Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Stacks and Queues The Stack abstract data type
CSCE 3110 Data Structures & Algorithm Analysis
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.
Queues.
DATA STRUCTURE QUEUE.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
CSCE 3110 Data Structures and Algorithm Analysis
Queues: Implemented using Arrays
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSCE 3110 Data Structures and Algorithm Analysis
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
CSCE 3110 Data Structures and Algorithm Analysis
CE 221 Data Structures and Algorithms
Queue.
CSC 248 – fundamentals of Data structure
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 Queues Reading: Chap. 3 Weiss

Queue Stores a set of elements in a particular order Stack principle: FIRST IN FIRST OUT = FIFO It means: the first element inserted is the first one to be removed Example The first one in line is the first one to be served cinemark

Queue Applications Real life examples Waiting in line Waiting on hold for tech support Applications related to Computer Science Threads Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

First In First Out rear front D C B A B A C B A D C B A rear front

Applications: Job Scheduling

Queue ADT objects: a finite ordered list with zero or more elements. methods: for all queue  Queue, item  element, max_ queue_ size  positive integer Queue createQ(max_queue_size) ::= create an empty queue whose maximum size is max_queue_size Boolean isFullQ(queue, max_queue_size) ::= if(number of elements in queue == max_queue_size) return TRUE else return FALSE Queue Enqueue(queue, item) ::= if (IsFullQ(queue)) queue_full else insert item at rear of queue and return queue

Queue ADT (cont’d) Boolean isEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE Element dequeue(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue.

Array-based Queue Implementation As with the array-based stack implementation, the array is of fixed size A queue of maximum N elements Slightly more complicated Need to maintain track of both front and rear Implementation 1 Implementation 2

createQ, isEmptyQ, isFullQ Implementation 1: createQ, isEmptyQ, isFullQ Queue createQ(max_queue_size) ::= # define MAX_QUEUE_SIZE 100/* Maximum queue size */ typedef struct { int key; /* other fields */ } element; element queue[MAX_QUEUE_SIZE]; int rear = -1; int front = -1; Boolean isEmpty(queue) ::= front == rear Boolean isFullQ(queue) ::= rear == MAX_QUEUE_SIZE-1

Implementation 1: enqueue void enqueue(int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE_1) { queue_full( ); return; } queue [++*rear] = item; }

Implementation 1: dequeue element dequeue(int *front, int rear) { /* remove element at the front of the queue */ if ( *front == rear) return queue_empty( ); /* return an error key */ return queue [++ *front]; }

Wrapped Configuration Implementation 2: Wrapped Configuration EMPTY QUEUE [2] [3] [2] [3] J2 J3 [1] [4] [1] [4] J1 [0] [5] [0] [5] front = 0 front = 0 rear = 0 rear = 3 Can be seen as a circular queue

Leave one empty space when queue is full Why? FULL QUEUE FULL QUEUE [2] [3] [2] [3] J2 J3 J8 J9 [1] [4][1] [4] J1 J4 J7 J6 J5 J5 [0] [5] [0] [5] front =0 rear = 5 front =4 rear =3 How to test when queue is empty? How to test when queue is full?

Enqueue in a Circular Queue void enqueue(int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear) /* reset rear and print error */ return; } queue[*rear] = item; }

Dequeue from Circular Queue element dequeue(int* front, int rear) { element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty( ); /* queue_empty returns an error key */ *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; }

List-based Queue Implementation: Enqueue void enqueue(pnode front, pnode rear, element item) { /* add an element to the rear of the queue */ pnode temp = (pnode) malloc(sizeof (queue)); if (IS_FULL(temp)) { fprintf(stderr, “ The memory is full\n”); exit(1); } temp->item = item; temp->next= NULL; if (front) { (rear) -> next= temp;} else front = temp; rear = temp; }

Dequeue element dequeue(pnode front) { /* delete an element from the queue */ pnode temp = front; element item; if (IS_EMPTY(front)) { fprintf(stderr, “The queue is empty\n”); exit(1); } item = temp->item; front = temp->next; free(temp); return item; }

Algorithm Analysis enqueue O(?) dequeue O(?) size O(?) isEmpty O(?) isFull O(?) What if I want the first element to be always at Q[0] ?