0% found this document useful (0 votes)
118 views8 pages

Queue Data Structure & Implementation

This document describes and compares array-based implementations of queue data structures. It defines common queue operations like enqueue, dequeue, peek, and isEmpty. It provides examples of using queues and pseudocode for an array-based queue implementation. It notes disadvantages like unused space in the array and introduces a circular array-based queue to address this by using the entire array space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views8 pages

Queue Data Structure & Implementation

This document describes and compares array-based implementations of queue data structures. It defines common queue operations like enqueue, dequeue, peek, and isEmpty. It provides examples of using queues and pseudocode for an array-based queue implementation. It notes disadvantages like unused space in the array and introduces a circular array-based queue to address this by using the entire array space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

The Queue Data Structure

Mugurel Ionuț Andreica

Spring 2012
Operations
• enqueue(x)
– Adds the element x at the tail of the queue
• dequeue()
– Removes the element from the head of the queue and returns it
– Returns an error if the stack is empty
• peek()
– Returns (but does not remove) the element at the head of the
queue
• isEmpty()
– Returns 1 if the queue is empty and 0 otherwise
• The axioms are given implicitly – they determine the
expected behavior of the queue for a given sequence of
operations
– See the examples on the upcoming slides
Queue - Example

enqueue(7) 7 enqueue(8) 7 8 enqueue(6) 7 8 6

dequeue() 8 6 peek() dequeue() 6 enqueue(4) 6 4


returns 7 returns 8 returns 8
Queue – Array-based
Implementation (queue1.h)
#define NMAX 100 T peek() {
if (isEmpty()) {
template<typename T> class Queue {
private: fprintf(stderr, "Error 103 - The queue is empty!\n");
T queueArray[NMAX]; T x;
int head, tail; return x;
public: }
void enqueue(T x) { return queueArray[head];
if (tail >= NMAX) {
fprintf(stderr, "Error 101 - The queue is full!\n"); }
return;
} int isEmpty() {
queueArray[tail] = x; return (head == tail);
tail++; }
}

T dequeue() { Queue() {
if (isEmpty()) { head = tail = 0; // the queue is empty in the beginning
fprintf(stderr, "Error 102 - The queue is }
empty!\n");
T x; };
return x;
}
T x = queueArray[head];
head++;
return x;
}
Using the Queue
#include <stdio.h> [Link](2);
#include “queue1.h” printf("%d\n", [Link]());
int main() { printf("%d\n", [Link]());
printf("%d\n", [Link]());
Queue<int> q; [Link]();
printf("%d\n", [Link]());
[Link](7); printf("%d\n", [Link]());
[Link](8);
[Link](6);
printf("%d\n", [Link]()); return 0;
printf("%d\n", [Link]()); }
printf("%d\n", [Link]());
[Link](4);
Disadvantages of the Array-based
Queue Implementation
• The head and tail variables are constantly
increasing
• As elements are removed from the queue, the
portion of the array which is effectively used
shifts to the right
• We may reach the end of the array and be
unable to enqueue any other elements, although
a large fraction of the array (its left part) is empty
(unused)
• Improved solution: circular array
Circular Array-based Queue -
Example
• Circular array with
NMAX=3 entries

- - - enqueue(7) 7 - - enqueue(8) 7 8 -

dequeue() peek()
enqueue(6) 7 8 6 - 8 6
returns 7 returns 8

dequeue() - - 6 4 - 6
enqueue(4) enqueue(10)
returns 8

dequeue()
4 10 6 4 10 - enqueue(13) 4 10 13
returns 6
Queue – Circular Array-based
Implementation (queue2.h)
#define NMAX 100 T peek() {
if (isEmpty()) {
template<typename T> class Queue {
private: fprintf(stderr, "Error 103 - The queue is empty!\n");
T queueArray[NMAX]; T x;
int head, tail, size; return x;
public: }
void enqueue(T x) { return queueArray[head];
if (size == NMAX) {
fprintf(stderr, "Error 101 - The queue is full!\n"); }
return;
} int isEmpty() {
queueArray[tail] = x; return (size == 0);
tail = (tail + 1) % NMAX; }
size++;
}
Queue() {
T dequeue() { head = tail = size = 0;
if (isEmpty()) { }
fprintf(stderr, "Error 102 - The queue is empty!\n"); };
T x;
return x;
}
T x = queueArray[head];
head = (head + 1) % NMAX;
size--;
return x;
}

You might also like