0% found this document useful (0 votes)
33 views

Data Structures and Algorithms: Queue

The document discusses queues as a data structure. It covers implementing queues using dynamic arrays and linked lists. Key points include: - Queues follow a first-in, first-out (FIFO) approach for adding (enqueue) and removing (dequeue) elements. - Array implementations use a front and rear pointer to track the start and end of the queue. Linked list implementations use front and rear pointers to track the head and tail nodes. - Methods for queues include enqueue, dequeue, peek/front, isEmpty, isFull, and size. - Queues have applications in simulation, scheduling, and buffering.

Uploaded by

Muhammed khaled
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)
33 views

Data Structures and Algorithms: Queue

The document discusses queues as a data structure. It covers implementing queues using dynamic arrays and linked lists. Key points include: - Queues follow a first-in, first-out (FIFO) approach for adding (enqueue) and removing (dequeue) elements. - Array implementations use a front and rear pointer to track the start and end of the queue. Linked list implementations use front and rear pointers to track the head and tail nodes. - Methods for queues include enqueue, dequeue, peek/front, isEmpty, isFull, and size. - Queues have applications in simulation, scheduling, and buffering.

Uploaded by

Muhammed khaled
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/ 24

Data Structures and Algorithms

Queue
Lecture 5

Lecture 5

QUEUE ADT And Problem Solving 11


◼Introduction to the Queue data structure
◼Designing a Queue class using dynamic
arrays
◼Linked Queues
Lecture 5

◼An Application of Queues

QUEUE ADT And Problem Solving 2


▪ A simple data container consisting of a linear list
of elements
▪ Access is by position (order of insertion)
▪ Insertions at one end (rear) , deletions at another
end (front)
▪ First In First Out (FIFO) structure
Lecture 5

▪ Two basic operations:


enqueue: add to rear
dequeue: remove from front

QUEUE ADT And Problem Solving 3


4

1 2 3 1 2 3

Enqueue
Lecture 5

1 2 3 4 2 3 4

Dequeue
QUEUE ADT And Problem Solving 4
o Problem of Array representation
o Solution using Circular Array.
o When last array element is reached, we move back to start
o The queue is viewed as a circular array
o To enqueue:
rear = (rear + 1) % size
o To dequeue:
front = (front + 1) % size
Lecture 5

o Both rear and front advance clockwise

QUEUE ADT And Problem Solving 5


Lecture 5

QUEUE ADT And Problem Solving


Fall 2010 Prof. Amr Goneid, AUC
◼Simulation of waiting lines
◼Simulation of serviceable events
◼Job scheduling
◼Input/Output Buffering
Lecture 5

QUEUE ADT And Problem Solving 7


◼ construct: construct an empty queue
◼ queueIsEmpty → bool : return True if queue is
empty
◼ queueIsFull → bool : return True if queue is full
◼ enqueue(el) : add element (el) at the rear
◼ dequeue(el): retrieve and remove the front element
Lecture 5

◼ queueFront(el): retrieve front without removing it


◼ queueLength → int : return the current queue length

QUEUE ADT And Problem Solving 8


◼The queue may be implemented as a
dynamic array.
◼The capacity (MaxSize) will be input as
a parameter to the constructor (default
is 128)
Lecture 5

◼The queue ADT will be implemented as


a template class to allow for different
element types.

QUEUE ADT And Problem Solving 9


// File: Queuet.h
// Queue template class definition
// Dynamic array implementation

#ifndef QUEUET_H
#define QUEUET_H

template <class Type>


Lecture 5

class Queuet
{
public:

Queuet (int nelements = 128); // Constructor


Queuet (const Queuet <Type> &); // Copy Constructor
~Queuet (); // Destructor

QUEUE ADT And Problem Solving10


// Member Functions
void enqueue(Type ); // Add to rear
void dequeue(Type &); // Remove from front
void queueFront(Type &) const; // Retrieve front
bool queueIsEmpty() const; // Test for Empty queue
bool queueIsFull() const; // Test for Full queue
int queueLength() const; // Queue Length

private:
Lecture 5

Type *queue; // pointer to dynamic array


int front, rear, count, MaxSize;

};

#endif // QUEUET_H
#include "Queuet.cpp"

QUEUE ADT And Problem Solving 11


Stack::Stack(const Stack& other)
{
stkLength = other. stkLength;
stkSize = other.stkSize;
stkArray = new int[stkLength];
Lecture 5

for (int i = 0; i < stkSize; i++)


stkArray[i] = other.stkArray[i];

stkTop = other.stkTop;
}
QUEUE ADT And Problem Solving
Fall 2010 Prof. Amr Goneid, AUC
template<typename Type >
bool queue< Type>::enqueue(const Type & item)
{
if (count == array_size) return false;
array[tail] = item;
Lecture 5

tail = (tail + 1) % array_size;


count++;
return true;
}

QUEUE ADT And Problem Solving


Fall 2010
◼A Queue can be implemented as a linked
structure.
◼Requires more space than array
implementations, but more flexible in size.
◼Two pointers are needed: front for dequeue
Lecture 5

and rear for enqueue

QUEUE ADT And Problem Solving14


// The linked structure for a node can be
// specified as a Class in the private part of
// the main queue class.
class node // Hidden from user
{
public:
Type e; // stack element
node *next; // pointer to next node
}; // end of class node declaration
Lecture 5

typedef node * NodePointer;

NodePointer front , rear; // pointers

QUEUE ADT And Problem Solving15


rear
front

New
Lecture 5

enqueue(v):
NodePointer pnew = new node;
1 pnew->e = v;
pnew pnew->next = NULL;
rear->next = pnew;
rear = pnew;

QUEUE ADT And Problem Solving16


1 cursor rear
front

2
Lecture 5

dequeue(&v):
v = front->e;
cursor = front;
front = front->next;
delete cursor;
QUEUE ADT And Problem Solving17
// File: QueueL.h
// Linked List Queue class definition
#ifndef QUEUEL_H
#define QUEUEL_H
template <class Type>
class QueueL
{
Lecture 5

public:
QueueL(); // Constructor
~QueueL(); // Destructor
void enqueue(Type ); // Add to rear

QUEUE ADT And Problem Solving18


void dequeue(Type &); // Remove from front
void queueFront(Type &) const; // retrieve front
bool queueIsEmpty() const; // Test for Empty queue
int queueLength() const; // Queue Length

private:
// Node Class
class node
Lecture 5

{
public:
Type e; // queue element
node *next; // pointer to next node
}; // end of class node declaration

QUEUE ADT And Problem Solving19


typedef node * NodePointer;

NodePointer front , rear; // Pointers


int count; // length

};

#endif // QUEUEL_H
Lecture 5

#include "QueueL.cpp"

QUEUE ADT And Problem Solving20


template <class Type>
void QueueL<Type>::enqueue(Type v)
{
NodePointer pnew = new node;
pnew->e = v; pnew->next = NULL;
if(queueIsEmpty())
{
Lecture 5

front = pnew; rear = pnew;}


else
{rear->next = pnew; rear = pnew;}
count++;
}
QUEUE ADT And Problem Solving 21
template <class Type>
void QueueL<Type>::dequeue(Type &v)
{ NodePointer cursor;
if(queueIsEmpty()) cout<<” Queue is empty “<<endl;
else {
V = front-> e;
cursor = front;
Lecture 5

front =front->next;
delete cursor;
count--;
}
}
QUEUE ADT And Problem Solving 22
template <class Type>
void QueueL<Type>::queueFront(Type &v)
const
{
if(queueIsEmpty())
cout << "Queue is Empty" << endl;
Lecture 5

else { v = front->e; }
}

QUEUE ADT And Problem Solving 23


• Implement Queue using ADT of
linked List
• Implement Queue using ADT of
circular dynamic array
Lecture 5

QUEUE ADT And Problem Solving

You might also like