Dsa CH 4 Queues
Dsa CH 4 Queues
Front of queue
STACKS
❖ A stack is a restricted linear list in which all additions
and deletions are made at one end, the top.
❖ If we insert a series of data items into a stack and then
remove them, the order of the data is reversed.
❖ This reversing attribute is why stacks are known as
last in, first out (LIFO) data structures.
1. queue,
2. enqueue,
3. dequeue and
4. empty,
Representation Of Queues
1. Using an Array
2. Using Linked List
Figure:- Queue implementations
The queue operation
❖ The queue operation creates an empty queue.
❖ The following shows the format.
6-30
Operations on a Queue
dequeue
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
Array Implementation of QUEUE
else
{
printf("Element deleted from queue is : %d\n",
queue_array[front]);
front = front + 1;
}
/*End of delete() */
}
Display items in Queue
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
Display items in Queue
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf(“\n%d ", queue_array[i]);
}
/*End of display() */
}
Drawback of Linear Queue
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
Rear
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Rear
Front
Front
Rear
Front
Rear Front
Front
44
Array Implementation of Circular
Queue
void insert()
{
if((front==0 && rear==SIZE-1) || (front==rear+1))
printf("\n\nQueue is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
Circular Queue
if(rear == -1)
{
rear = 0;
front = 0;
}
else if(rear == SIZE-1)
rear = 0;
else
rear++;
queue[rear] = item;
printf("\n\nItem inserted: %d\n", item);
} //end of else
} //end of function
void display()
{
int i;
if((front == -1) || (front==rear+1))
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
for(i=front; i<=rear; i++)
printf("\t%d",queue[i]);
}
}
Applications of Circular Queue
The circular Queue can be used in the following scenarios:
❖ Memory management: The circular queue provides memory
management. As we have already seen that in linear queue, the
memory is not managed very efficiently. But in case of a circular
queue, the memory is managed efficiently by placing the
elements in a location which is unused.