0% found this document useful (0 votes)
28 views

Dsa CH 4 Queues

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dsa CH 4 Queues

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

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.

Figure:-Three representations of stacks


Operations on stacks
There are four basic operations, stack, push, pop and empty,
that we define in this chapter.

The stack operation


The stack operation creates an empty stack. The following
shows the format.

Figure:- Stack operation


The push operation
The push operation inserts an item at the top of the stack.
The following shows the format.

Figure:- Push operation


The pop operation
The pop operation deletes the item at the top of the stack.
The following shows the format.

Figure:- Pop operation


The empty operation
The empty operation checks the status of the stack. The
following shows the format.

This operation returns true if the stack is empty and false if


the stack is not empty.
QUEUES
❖ Ordered collection of homogeneous elements
❖ Linear data structure.
QUEUES
❖ A queue is a linear list in which data can only be inserted
at one end, called the rear or tail , and deleted from the
other end, called the front or head.

❖ These restrictions ensure that the data is processed


through the queue in the order in which it is received. In
other words, a queue is a first in, first out (FIFO)
structure.

Figure:- Two representation of queues


Operations on queues
Although we can define many operations for a queue, four
are basic:

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.

Figure:- The queue operation


The enqueue operation
The enqueue operation inserts an item at the rear of the
queue.

The following shows the format.

Figure:- The enqueue operation


Enqueue Operation
Step 1: check if the queue is full
Step 2: for the first element, set value
of FRONT to 0
Step 3: increase the REAR index by 1
Step 4: add the new element in the position
pointed to by REAR
The dequeue operation
The dequeue operation deletes the item at the front of the
queue.

The following shows the format.

Figure:- The dequeue operation


Dequeue Operation
Step 1: check if the queue is empty
Step 2: return the value pointed by FRONT
Step 3: increase the FRONT index by 1
Step 4: for the last element, reset the values
of FRONT and REAR to -1
The empty operation
The empty operation checks the status of the queue.

The following shows the format.

This operation returns true if the queue is empty and false if


the queue is not empty.
Queue Applications
➢ Real life examples
✓ Waiting in line
✓ Waiting on hold for tech support
➢ Applications related to Computer Science
✓ Job scheduling (FIFO Scheduling)
✓ Key board Input buffer
✓ GUI event queue (click on buttons, menu items)
✓ Printer queue
Limitation of queue
ADT (Abstract Data Type)
We define a queue as an ADT as shown below:

❖ An abstract data type (ADT) is a mathematical


model for data types where a data type is defined by
its behavior.
❖ We define only the features/Operations
❖ Implementation details are ignored/hidden/not
defined.
❖ For example, a List ADT can be represented using
an array-based implementation or a linked-list
implementation. A List is an abstract data type with
well-defined operations (add element, remove
element, etc.)
Queue ADT (Abstract Data Type)

We define a queue as an ADT as shown below:

❖ The main property of the queue ADT is that


elements are added to the rear of the queue and
are removed from the front of the queue.
Queue ADT
We define a queue as an ADT as shown below:
Operations on a Queue
Operation Description
dequeue Removes an element from the front of the
queue
enqueue Adds an element to the rear of the queue

first Examines the element at the front of the


queue
isEmpty Determines whether the queue is empty

size Determines the number of elements in the


queue
toString Returns a string representation of the queue

6-30
Operations on a Queue
dequeue

enqueue Constant Time


first Or
isEmpty O(1)
Array Implementation of QUEUE
int queue_array[MAX];
int rear = - 1;
int front = - 1;
Array Implementation of QUEUE
void insert( )
{
int value;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1) /*If queue is initially empty */
front = 0;
Array Implementation of QUEUE

printf("Inset the element in queue : ");


scanf("%d", &num);
rear = rear + 1;
queue_array[rear] = num;
}
Array Implementation of QUEUE

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

◼ Once the queue is full, even though few


elements from the front are deleted and
some occupied space is relieved, it is not
possible to add anymore new elements.
◼ As the rear has already reached the
Queue’s rear most position.
Circular Queue
Circular Queue
◼ Queue is a abstract data type, In which
entities are inserted into the rear end and
deleted from the front end.
◼ In circular queue is connected to end to
end, i,e rear and front end are connected.
◼ Compare to normal queue, Circular queue
is more advantages.
Circular Queue
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 1.
Front

Rear

2. Insert 10, Rear = 1, Front = 1. 5. Insert 70, Rear = 4, Front = 1.


Rear Front
Front

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

8. Insert 40, Rear = 1, Front = 2. 11. Delete front, Rear = 1, Front = 4.


Rear
Rear Front

Front

9. Insert 140, Rear = 1, Front = 2. 12. Delete front, Rear = 1, Front = 5.


As Front = Rear + 1, so Queue overflow. Rear

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.

❖ CPU Scheduling: The operating system also uses the circular


queue to insert the processes and then execute them.

❖ Traffic system: In a computer-control traffic system, traffic light


is one of the best examples of the circular queue. Each light of
traffic light gets ON one by one after every interval of time. Like
red light gets ON for one minute then yellow light for one
minute and then green light. After green light, the red light
gets ON.

You might also like