Lecture Data structures and other objects using C++ - Chapter 8: Using a queue - Trường Đại học Công nghiệp Thực phẩm Tp. Hồ Chí Minh

7 9 0
Lecture Data structures and other objects using C++ - Chapter 8: Using a queue - Trường Đại học Công nghiệp Thực phẩm Tp. Hồ Chí Minh

Đang tải... (xem toàn văn)

Thông tin tài liệu

The template  The template  parameter is the  parameter is the  type of the items  type of the items  that can be put in  that can be put in  the queue.. Array Implementation[r]

(1)

Chapter 8 introduces the  Chapter 8 introduces the 

queue

queue  data type.data type Several example  Several example 

applications of queues are  applications of queues are 

given in that chapter.  given in that chapter. 

This presentation describes  This presentation describes 

the queue operations and  the queue operations and  two ways to implement a  two ways to implement a 

queue queue Data Structures

(2)

The Queue Operations

A queue is like a line  A queue is like a line 

of people waiting for a  of people waiting for a  bank teller. The queue  bank teller. The queue 

has a 

has a frontfront and a  and a rearrear..

$  $ 

(3)

New people must enter the queue at the  New people must enter the queue at the 

rear. The C++ queue class calls this a  rear. The C++ queue class calls this a 

push

push, although it is usually called an , although it is usually called an  enqueue

enqueue operation. operation

$  $ 

(4)

The Queue Operations

When an item is taken from the queue,  When an item is taken from the queue, 

it always comes from the front.  The  it always comes from the front.  The 

C++ queue calls this a 

C++ queue calls this a poppop, although it , although it  is usually called a 

is usually called a dequeuedequeue operation. operation.

$  $ 

(5)

The C++ standard  The C++ standard  template library has  template library has  a queue template  a queue template  class class The template  The template  parameter is the  parameter is the  type of the items  type of the items  that can be put in  that can be put in  the queue the queue template <class Item> class queue<Item>

(6)

Array Implementation

A queue can be implemented with an array, as  A queue can be implemented with an array, as 

shown here.  For example, this queue contains the  shown here.  For example, this queue contains the 

integers 4 (at the front), 8 and 6 (at the rear) integers 4 (at the front), 8 and 6 (at the rear)

[ 0 ]

[ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

An array of integers 

An array of integers 

to implement a 

to implement a 

queue of integers

queue of integers

4

We don't care what's in

We don't care what's in

this part of the array

(7)

The easiest implementation also keeps  The easiest implementation also keeps 

track of the number of items in the  track of the number of items in the 

queue and the index of the first  queue and the index of the first 

element (at the front of the queue), the  element (at the front of the queue), the 

last element (at the rear) last element (at the rear)

[ 0 ]

[ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

4

3

first

first

0

last

last

Ngày đăng: 01/04/2021, 17:29

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan