Data Structures and Algorithms: Queue
Data Structures and Algorithms: Queue
Data Structures and Algorithms: Queue
Queue
Lecture 5
Lecture 5
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
#ifndef QUEUET_H
#define QUEUET_H
class Queuet
{
public:
private:
Lecture 5
};
#endif // QUEUET_H
#include "Queuet.cpp"
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
New
Lecture 5
enqueue(v):
NodePointer pnew = new node;
1 pnew->e = v;
pnew pnew->next = NULL;
rear->next = pnew;
rear = pnew;
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
private:
// Node Class
class node
Lecture 5
{
public:
Type e; // queue element
node *next; // pointer to next node
}; // end of class node declaration
};
#endif // QUEUEL_H
Lecture 5
#include "QueueL.cpp"
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; }
}