Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
206 views19 pages

Data Structures Via C++: Queue

The document discusses queues and their implementation using arrays and linked lists. It defines queues as first-in first-out (FIFO) data structures where items are added to the rear and removed from the front. It presents queue operations like enqueue, dequeue, front, and isEmpty. It then provides code examples to implement queues using arrays, with circular queue handling, and using linked lists, with pointers to track the front and rear of the queue.
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)
206 views19 pages

Data Structures Via C++: Queue

The document discusses queues and their implementation using arrays and linked lists. It defines queues as first-in first-out (FIFO) data structures where items are added to the rear and removed from the front. It presents queue operations like enqueue, dequeue, front, and isEmpty. It then provides code examples to implement queues using arrays, with circular queue handling, and using linked lists, with pointers to track the front and rear of the queue.
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/ 19

Data Structures via C++

Chapter 8:
Queue
Dr. Shaker El-Sappagh
Faculty of Computer Science and Engineering,
Galala University
[email protected]
What is a queue?
- Is a special kind of list.
- All of its items are added from one end (tail) and
removed from the other end (head).
- Queue preserve the order of items (FIFO data
structure).
- It is used in operating systems and networks
software (queuing theory).
Queue ADT
Characteristics:
- A queue store items (queueElementType) in FIFO.
Operations
- queueElementType dequeue ().
- void enqueue (queueElementType).
- queueElementType front ().
- bool isEmpty ().
Queue implementation using array
- Queue has double ends (front and rear).
- For array of size 4, some enqueue and dequeue are as follows:

enqueue(‘a’) a ? ? ? dequeue() a b c ?
f r f r
enqueue(‘b’) a b ? ? enqueue(‘d’) a b c d
f r f r
enqueue(‘c’) a b c ? dequeue() a b c d
f r f r
enqueue(‘e’) ???
- The array has empty places however, r can not move to the right.
Queue implementation using array (cont.)
- One solution is the circular queue. The end wraps to the front like
a circle.
- We still use array but we need r a
d
r and f to work in a circular way.
- We need maxQueue – 1 to point to f
the last element in the array, and C b
each add by 1 check if r or f equals
maxQueue – 1 .
- This operation will be done in a separate function e.g., nextPos.
Queue implementation using array (cont.)
- The question is: How to start the value of f and r. f
- When the queue has one element
then f == r, so r must start one step before f. r
- The check for empty queue is nextPos(r) == f.
f
- What is the problem with this implementation? r a

➢ The check for empty and full queue are the same.
- What is the solution to this problem?
➢ One way is to set f == r for empty queue, and f b
do not use the cell of f (real queue data is at a

nextPos(f) ).
d c
r
Queue implementation using array (cont.)
- What is the solution to this problem? f
r
➢ One way is to set f == r for empty queue, and
do not use the cell of f (real queue data is at
nextPos(f) ). r
f a
➢ The queue is full when nextPos(r) == f.
One cell of the array is wasted. Not avoidable.

f b

d c
r
Code Regular
const int maxQueue = 200;

template < class queueElementType >


class Queue {
public:
Queue();
void enqueue(queueElementType e);
queueElementType dequeue();
queueElementType front();
bool isEmpty();
private:
int f; // marks the front of the queue
int r; // marks the rear of the queue
queueElementType elements[maxQueue];
};
Code Regular
int nextPos(int p)
{
if (p == maxQueue - 1) // at end of circle
return 0;
else
return p+1;
}
template < class queueElementType > Queue < queueElementType >::Queue()
{
f = 0;
r = 0;
}
Code Regular
template < class queueElementType > void Queue < queueElementType >
::enqueue(queueElementType e)
{
assert(nextPos(r) != f);
r = nextPos(r);
elements[r] = e;
}

template < class queueElementType > queueElementType Queue < queueElementType>


::dequeue()
{
// advance front of queue, return value of element at the front
assert(f != r);
f = nextPos(f);
return elements[f];
}
Code Regular
template < class queueElementType > queueElementType Queue < queueElementType >
::front()
{
assert(f != r);
return elements[nextPos(f)];
}
template < class queueElementType > bool Queue < queueElementType >
::isEmpty()
{
return bool(f == r);
}
Queue implementation using Dynamic linked list
- We need two pointers for the front and the rear.
- The linked list public part is the same as the array
implementation.
- The private part declares new pointers and structs.
Code Dynamic List
template < class queueElementType >
class Queue {
public:
Queue();
void enqueue(queueElementType e);
queueElementType dequeue();
queueElementType front();
bool isEmpty();
private:
struct Node;
typedef Node * nodePtr;
struct Node {
queueElementType data;
nodePtr next;
};
nodePtr f;
nodePtr r;
};
Code Dynamic List

template < class queueElementType > Queue < queueElementType >


::Queue()
{
// set both front and rear to null pointers
f = 0;
r = 0;
}
Code Dynamic List
template < class queueElementType > void Queue < queueElementType >
::enqueue(queueElementType e)
{
// create a new node, insert it at the rear of the queue
nodePtr n = new Node;
assert(n);
n->next = 0;
n->data = e;
if (f != 0)
{ // existing queue is not empty
r->next = n; // add new element to end of list
r = n;
}
else {// adding first item in the queue
f = n; // so front, rear must be same node
r = n;
}
}
Code Dynamic List
template < class queueElementType > queueElementType Queue < queueElementType >
::dequeue()
{
assert(f); // make sure queue is not empty
nodePtr n = f;
queueElementType frontElement = f->data;
f = f->next;
delete n;
if (f == 0) // we're deleting last node
r = 0;
return frontElement;
}
Code Dynamic List
template < class queueElementType > queueElementType Queue < queueElementType >
::front()
{
assert(f);
return f->data;
}
Code Dynamic List

template < class queueElementType > bool Queue < queueElementType >
::isEmpty()
{
return bool(f == 0);
}
Thanks

You might also like