Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

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)
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.
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.
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.
Data Structures - Queues
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Stacks And Queues Chapter 18.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Week 15 – Monday.  What did we talk about last time?  Tries.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Linked Data Structures
Queues Chapter 8 (continued)
Elementary Data Structures
Review Array Array Elements Accessing array elements
Week 4 - Monday CS221.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
QueueStack CS1020.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
COMP 53 – Week Eleven Hashtables.
12 C Data Structures.
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
Queues Queues Queues.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Stack and Queue.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
HW-6 Deadline Extended to April 27th
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Queues.
Stacks, Queues, and Deques
Lecture 21 Stacks and Queues Richard Gesick.
Arrays and Linked Lists
Stacks and Queues.
Stacks, Queues, and Deques
Data Structures – Stacks and Queus
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
Stacks and Queues Prof. Michael Tsai 2017/02/21.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Visit for more Learning Resources
CE 221 Data Structures and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues Definition of a Queue Examples of Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Getting queues right … finally (?)
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first to pee on the tree.

FIFO enqueue dequeue Similarly, with queues, the first element inserted will be the first element retrieved. We'll refer to this pattern of insertion and retrieval as "first-in, first-out," or FIFO for short. A queue's enqueue function places a new element at a queue's "tail" end, while dequeue retrieves the element at a queue's "head" (i.e., front). ***Be aware that you may see variations on the names of these functions in different textbooks as they aren't as standardized as push and pop are for stacks.*** Like stacks (and unlike arrays), queues typically don't allow access to elements in the middle.

char* strings[CAPACITY]; int size; } queue; typedef struct { int head; char* strings[CAPACITY]; int size; } queue; This is one way to define a queue for char*s. head is the index of the queue's head element. We'll adjust it as we dequeue elements. Why would we need to keep track of the head of our queue? Why not simply consider the element at strings[0] to be the head and the element at strings[size - 1] to be the tail? This would require us to shift all of the elements from strings[1] to strings[size - 1] down by one position every time we call dequeue, which is a big waste of time especially if we've got a long queue! CAPACITY is a constant and strings is a statically-sized array of char*s that you'll use for storing the char* elements. size stores the number of elements currently in the queue. You'll need to adjust it appropriately so that you can track the location of the "tail" of the queue. Why is this design suboptimal? It imposes a limit on the size of the queue.

Enqueue TODOs: [5] [4] [3] [2] [1] [0] size < CAPACITY? store at tail size++ { head [5] [4] [3] [2] [1] [0] To enqueue an element, first make sure that the array isn't full by comparing size to CAPACITY. If size < CAPACITY, store the element in the next available open slot, which should be at index [(head + size) % CAPACITY]. Don't forget to increment size!

Dequeue TODOs: [5] [4] [3] [2] [1] [0] size > 0? move head size-- return element { head [5] [4] [3] [2] [1] [0] To dequeue an element, first make sure that there are elements in the array by comparing size to 0. If size > 0, the element at the head of the list is the one you'll want to dequeue. Don't forget to reposition head and decrement size!