CE 221 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
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.
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.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
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.
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 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.”
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 Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)
Queue Overview Queue ADT Basic operations of queue
 Balancing Symbols 3. Applications
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
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 –
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
 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.
Review Array Array Elements Accessing array elements
CS 201 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CS 201 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
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.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues CSE 373 Data Structures.
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
Data Structures – Week #4
Presentation transcript:

CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 Izmir University of Economics

Izmir University of Economics The Queue ADT Like stacks, queues are lists. With a queue, however, insertion is done at one end, whereas deletion is performed at the other end. The basic operations on a queue (FIFO list) are enqueue, which inserts an element at the end of the list (called the rear), and dequeue, which deletes (and returns) the element at the start of the list (known as the front). Izmir University of Economics

Array Implementation of Queues - I As with stacks, both array and linked list (trivial) implementations give O(1) running times. Let’s discuss circular array implementation. An array to store elements, Array, and the positions Front and Rear to represent the ends of the queue are kept. We also keep track of the number of elements: Size. Izmir University of Economics

Array Implementation of Queues – Problem 5 2 7 1 Front Rear To enqueue an element X, increment Size and Rear and set Array[Rear]=X To dequeue an element, set the return value to Array[Front], decrement Size and then increment Front. Assume after several enqueue operations, the Rear is at the last index position. Assume also that some elements, in the meantime, have been dequeued to make up room. The next enqueue would fail, although there would be free slots. Izmir University of Economics

Array Implementation of Queues – Problem Solved The simple solution is whenever Front or Rear gets to the end of the Array, it is wrapped around to the beginning (hence the name circular array). initially 2 4 Front Rear After enqueue(1) 1 2 4 Front Rear After enqueue(3) 1 3 2 4 Front Rear Izmir University of Economics

Array Implementation of Queues – Example After dequeue  2 1 3 2 4 Front Rear After dequeue  4 1 3 2 4 Front Rear After dequeue  1 1 3 2 4 Front Rear After dequeue  3 and queue is empty 1 3 2 4 Front Rear If Size is not kept, queue is empty when Rear=Front-1. In that case, queue is full when there are Capacity-1 items. Why? Only Capacity different sizes can be differentiated and one of these is 0. Izmir University of Economics

Circular Array Implementation of Queues – Sample Code I typedef int ElementType; #define MinQueueSize (5) struct QueueRecord { int Capacity; int Front; int Rear; int Size; ElementType *Array; }; typedef struct QueueRecord *Queue; int IsEmpty( Queue Q ) { return Q->Size == 0; } int IsFull( Queue Q ) { return Q->Size == Q->Capacity; /* MakeEmpty is invoked */ /* after CreateQueue */ /* on the next slide */ /* is called */ void MakeEmpty(Queue Q) { Q->Size = 0; Q->Front = 1; Q->Rear = 0; } void DisposeQueue(Queue Q) { if( Q != NULL ) { free( Q->Array ); free( Q ); Izmir University of Economics

Circular Array Implementation of Queues – Sample Code II Queue CreateQueue(int MaxElements) { Queue Q; if(MaxElements<MinQueueSize) Error("Queue size is too small”); Q = malloc(sizeof(struct QueueRecord)); if(Q == NULL) FatalError("Out of space!!!”); Q->Array = malloc(sizeof(ElementType)*MaxElements); if( Q->Array == NULL ) Q->Capacity = MaxElements; MakeEmpty( Q ); return Q; } Izmir University of Economics

Circular Array Implementation of Queues – Sample Code III static int Succ(int Value, Queue Q) { if( ++Value == Q->Capacity ) Value = 0; return Value; } void Enqueue( ElementType X, Queue Q ) { if( IsFull( Q ) ) Error( "Full queue" ); else { Q->Size++; Q->Rear = Succ( Q->Rear, Q ); Q->Array[ Q->Rear ] = X; ElementType Front( Queue Q ) { if( !IsEmpty( Q ) ) return Q->Array[ Q->Front ]; Error( "Empty queue" ); return 0; /* Return value used to avoid warning */ Izmir University of Economics

Circular Array Implementation of Queues – Sample Code IV void Dequeue( Queue Q ) { if( IsEmpty( Q ) ) Error( "Empty queue" ); else { Q->Size--; Q->Front = Succ( Q->Front, Q ); } ElementType FrontAndDequeue( Queue Q ) { ElementType X = 0; X = Q->Array[ Q->Front ]; return X; Izmir University of Economics

Applications of Queues - I When jobs are submitted to a printer, they are arranged in order of arrival. Virtually every real-life line is (supposed to be) a queue. Users on other machines are given access to a file server on a first-come first-served basis. Calls to large companies are generally placed on a queue when all operators are busy. Izmir University of Economics

Applications of Queues - II A whole branch of mathematics, known as queueing theory, deals with computing, probabilistically, how long users expect to wait on a line, how long the line gets, and other such questions. The answer depends on how frequently users arrive to the line and how long it takes to process a user once the user is served. Both of these parameters are given as probability distribution functions. Izmir University of Economics

Izmir University of Economics Homework Assignments 3.35 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics