Queues

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Data Structures

Queues
Queues
 A stack is LIFO (Last-In First Out) structure.
 In contrast, a queue is a FIFO (First-In First-Out ) structure.
 A queue is a linear structure for which items can be only inserted at
one end and removed at another end.
Queue Operations
Enqueue(X) – place X at the rear of the queue.
Dequeue() -- remove the front element and return it.
Front() -- return front element without removing it.
IsEmpty() -- return TRUE if queue is empty, FALSE
otherwise
IsFull() -- retrun true if queue is full full, true
Implementing Queue
 Using linked List: Recall
 Insert works in constant time for either end of a linked list.
 Remove works in constant time only.
 Seems best that head of the linked list be the front of the queue so
that all removes will be from the front.
 Inserts will be at the end of the list.
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 9
Implementing Queue
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
Implementing Queue
int front()
{
return front->get();
}

int isEmpty()
{
return ( front == NULL );
}
Queue using Array
 If we use an array to hold queue elements, both insertions and
removal at the front (start) of the array are expensive.
 This is because we may have to shift up to “n” elements.
 For the stack, we needed only one end; for queue we need both.
 To get around this, we will not shift upon removal of an element.
Queue using Array

front rear
1 7 5 2
1 7 5 2
0 1 2 3 4 5 6 7
front rear
0 3
Queue using Array

enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4
Queue using Array

enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5
Queue using Array

dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5

7 5 2 6 8

0 1 2 3 4 5 6 7
front rear
0 4
Queue using Array

dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5
Queue using Array
enqueue(9)
enqueue(12)

front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
2 7
enqueue(21) ??
Queue using Array
 We have inserts and removal running in constant time but we
created a new problem.
 Cannot insert new elements even though there are two places
available at the start of the array.
 Solution: allow the queue to “wrap around”.
Circular Queue
 Basic idea is to picture the array as a circular array.

0 1
front
front rear
7 2 2
12 5
5 2 6 8 9 12
6 9 2
8 6 3 rear
7
5 4
5 2 6 8 9 12

0 1 2 3 4 5 6 7
front rear
2 7
Circular Queue
enqueue(21)
0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear noElements
0 7
5 4
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
Circular Queue
enqueue(7)
0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElements
1 8
5 4
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}
Circular Queue
dequeue()
0 1

front rear front size


7 2 1 8
6 8 9 12 21 7
6 3 rear noElements
1 6
5 4
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
Double ended Queue/deque

You can:
Insert at front
Delete from front // dequeue
Insert at rear // enqueue
Delete from rear
Class of Deque
class Deque {
int arr[MAX], front, rear, size;
public :
Deque(int size) {
front = -1; rear = -1; size = size;
}
// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};
Class of Deque
Void insertfront(int x)
{
If (!isFULL())
{
front--;
}
}
Function Definitions
IsFUll()

 Checks whether Deque is full or not


bool Deque::isFull()
{
return ((front == 0 && rear == size-1)||
rear == front-1);
}
isEmpty()

 Checks whether Deque is empty or not.


bool Deque::isEmpty ()
{
return (front == -1);
}
Insert at front

 Inserts element at front


of the queue
Insert at rear
 Insert an element at rear end of queue
Delete from front
Delete element from rear end
Display front element

int Deque::getFront()
{
// check whether Deque is empty or not
if (isEmpty())
{
cout << " Underflow\n" << endl;
return -1 ;
}
return arr[front];
}
Display element at rear end

int Deque::getRear()
{
// check whether Deque is empty or not
if(isEmpty() || rear < 0)
{
cout << " Underflow\n" << endl;
return -1 ;
}
return arr[rear];
}
insertfront => decrement front:
1. if front == 0, front = size-1
2. front = front-1
3. if front==-1 => queue is empty
front = 0, rear = 0

insertrear => increment rear:


1. rear = (rear+1)%size;
2. if front==-1 => queue is empty
front = 0, rear = 0

deletefront => increment front:


1. front = (front+1)%size
2. if front ==rear => queue has only one element
front =-1, rear =-1

deleterear => decrement rear


1. if rear == 0, rear = size-1
2. rear = rear-1
3. front == rear => rear = -1, front =-1

You might also like