0% found this document useful (0 votes)
13 views33 pages

Queues Adt

The document provides an overview of queues as a data structure that follows the first-in-first-out (FIFO) principle, contrasting it with stacks that operate on a last-in-first-out (LIFO) basis. It details the operations associated with queues, such as enqueue and dequeue, and discusses different implementations including array-based and linked list approaches. Additionally, it emphasizes the concept of Abstract Data Types (ADTs) in the context of queues and stacks, highlighting their role as specialized data structures compared to arrays.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views33 pages

Queues Adt

The document provides an overview of queues as a data structure that follows the first-in-first-out (FIFO) principle, contrasting it with stacks that operate on a last-in-first-out (LIFO) basis. It details the operations associated with queues, such as enqueue and dequeue, and discusses different implementations including array-based and linked list approaches. Additionally, it emphasizes the concept of Abstract Data Types (ADTs) in the context of queues and stacks, highlighting their role as specialized data structures compared to arrays.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structures: Queues & ADT

Queues

• Definition of a Queue
• Examples of Queues
• Design of a Queue Class
• Different Implementations of the
Queue Class
Introduction to Queues
• A queue is a waiting line

• It’s in daily life:


– A line of persons waiting to check out at a
supermarket
– A line of persons waiting to purchase a ticket for a
film
– A line of planes waiting to take off at an airport
– A line of vehicles at a toll booth
Definition of a Queue
• A queue is a data structure that models/enforces the
first--‐come first-‐‐serve order, or equivalently the first-‐‐
in first--‐out (FIFO) order.
• That is, the element that is inserted first into the
queue will be the element that will deleted first, and
the element that is inserted last is deleted last.
• A waiting line is a good real--‐life example of a queue.
(In fact, the British word for “line” is “queue”.)
Queues
• Unlike stacks in which elements are popped and
pushed only at the ends of the list

Collection of data elements:


• items are removed from a queue at one end,
called the FRONT of the queue;
• and elements are added at the other end, called
the BACK
Introduction to Queues
• Difference between Stack and Queues:

– Stack exhibits last-in-first-out (LIFO)

– Queue exhibits first-in-first-out (FIFO)


The Queue Operations

❒ A queue is like a line


of people waiting for a
bank teller. The queue
has a front and a rear.
$ $

Front
Rear
The Queue Operations

❒ New people must enter the queue at


the rear. This is called an enqueue
operation.
$ $

Front
Rear
The Queue Operations

❒ When an item is taken from the


queue, it always comes from the front.
This is called a dequeue operation.
$ $

Front
Rear
Queue Abstract Data Type
• Basic operations
– Construct a queue
– Check if empty
– Enqueue (add element to back)
– Front (retrieve value of element from front)
– Dequeue (remove element from front)
A Graphic Model of a Queue

Head:
Tail:
All items are
All new items
deleted from
are added on this end
this end
Operations on Queues
• Insert(item): (also called enqueue)
– It adds a new item to the tail of the queue
• Remove( ): (also called delete or dequeue)
– It deletes the head item of the queue, and returns to the caller. If the queue is
already empty, this operation returns NULL
• getHead( ):
– Returns the value in the head element of the queue
• getTail( ):
– Returns the value in the tail element of the queue
• isEmpty( )
– Returns true if the queue has no items
• size( )
– Returns the number of items in the queue
Queue as a Class
• Much like stacks and linked lists were
designed and implemented as classes, a
queue can be conveniently packaged as a class
• It seems natural to think of a queue as similar
to a linked list, but with more basic
operations, to enforce the FIFO order of
insertion and deletion
Designing and Building a Queue Class
Array-Based
• Consider an array in which to store a
queue

• Note additional variables needed


– myFront, myBack
• Picture a queue
object like this
Queue Operation
• Empty Queue

Enqueue(70)
Queue Operation
• Enqueue(80)

• Enqueue(50)
Queue Operation
• Dequeue()

• Dequeue()
Queue Operation
• Enqueue(90)

• Enqueue(60)
Circular Queue

• Problems
– We quickly "walk off the end" of the array
• Possible solutions
– Shift array elements
– Use a circular queue

– Note that both empty


and full queue
gives myBack == myFront
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] ...

4 8 6

An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
Array Implementation

• The easiest implementation also keeps 3 size


track of the number of items in the
queue and the index of the first 0 first
element (at the front of the queue), the
last element (at the rear).
2 last

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

4 8 6
A Dequeue Operation

• When an element leaves the queue, 2 size


size is decremented, and first changes,
too. 1 first

2 last

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

4 8 6
An Enqueue Operation

• When an element enters the queue, 3 size


size is incremented, and last changes,
too. 1 first

3 last

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

8 6 2
At the End of the Array

• There is special behavior at the end of 3 size


the array. For example, suppose we
want to add a new element to this 3 first
queue, where the last index is [5]:
5 last

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

2 6 1
At the End of the Array

• The new element goes at the front of 4 size


the array (if that spot isn’t already
used): 3 first

0 last

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

4 2 6 1
Array Implementation

• Easy to implement 3 size


• But it has a limited capacity with a fixed array
• Or you must use a dynamic array for an 0 first
unbounded capacity
• Special behavior is needed when the rear reaches
the end of the array. 2 last

[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

10

7
null
head_ptr
tail_ptr
Linked List Implementation

• Which end do you think is the


front of the queue? Why?
13

15

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 Front
13
items from the tail of the list.
15

10

7
null
head_ptr
tail_ptr
Rear
Abstract Data Type
• Abstract Data Type as a design tool
• Concerns only on the important concept or
model
• No concern on implementation details.
• Stack & Queue is an example of ADT
• An array is not ADT.
What is the difference?
• Stack & Queue vs. Array
– Arrays are data storage structures while stacks and queues
are specialized DS and used as programmer’s tools.
• Stack – a container that allows push and pop
• Queue -‐‐ a container that allows enqueue and
dequeue
• No concern on implementation details.
• In an array any item can be accessed, while in these
data structures access is restricted.
• They are more abstract than arrays.
Questions?
• Array is not ADT
• Is Linked list ADT?

You might also like