Using a Queue Chapter 8 introduces the queue data type.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Implementation of QUEUE For more notes and topics visit: eITnotes.com.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Review Array Array Elements Accessing array elements
Using a Stack Chapter 6 introduces the stack data type.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Queues Queues Queues.
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Visit for more Learning Resources
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
CSC212 Data Structure - Section KL
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

Using a Queue Chapter 8 introduces the queue data type. Several example applications of queues are given in that chapter. This presentation describes the queue operations and two ways to implement a queue. This lecture introduces queues. The presentation also shows two common ways of implementing a queue of integers. Data Structures and Other Objects Using C++

The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive. Front Rear

The Queue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear. Front Rear

The Queue Operations When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ When an item is removed from a queue, the removal occurs at the front. Front Rear

The Queue Class The C++ standard template library has a queue template class. The template parameter is the type of the items that can be put in the queue. template <class Item> class queue<Item> { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; … }; These are the four most common queue operations. The empty function tells you whether the queue has any items at the moment. The front operation returns the item at the front of the queue (without removing it from the queue).

Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array. 4 8 6 An array of integers to implement a queue of integers We don't care what's in this part of the array.

Array Implementation size The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). 3 first last 2 The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

A Dequeue Operation size When an element leaves the queue, size is decremented, and first changes, too. 2 first 1 last 2 This shows how the member variables change when an item leaves the queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

An Enqueue Operation size When an element enters the queue, size is incremented, and last changes, too. 3 first 1 last 3 And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 8 6 2

At the End of the Array size There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 3 first 3 last 5 An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so… [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 6 1

At the End of the Array size The new element goes at the front of the array (if that spot isn’t already used): 4 first 3 last …by putting it at location 0 (if that location is not already used). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 4 2 6 1

Array Implementation size Easy to implement But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. 3 first last 2 Here are some of the key aspects of an array implementation of a queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer. 13 15 A linked list can also be used to implement a queue, but we must maintain both a head and a tail pointer because we need access to both the front and the rear of the queue. 10 7 null head_ptr tail_ptr

Linked List Implementation Which end do you think is the front of the queue? Why? 13 15 Does it matter which end of a singly-linked list we use for the front of the queue? 10 7 null head_ptr tail_ptr

Linked List Implementation The head_ptr points to the front of the list. Because it is harder to remove items from the tail of the list. Front 13 15 Of course, we could put the front of the queue at the end of the linked list, but it would be hard to remove an item. Do you see why? 10 7 null head_ptr Rear tail_ptr

Summary Like stacks, queues have many applications. Items enter a queue at the rear and leave a queue at the front. Queues can be implemented using an array or using a linked list. A quick summary . . .

THE END Presentation copyright 2010, Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. Feel free to send your ideas to: Michael Main main@colorado.edu THE END