Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

NAME : AKSHAT KUMAR

DEPARTMENT : COMPUTER SCIENCE AND


ENGINEERING
SUBJECT : DATA STRUCTURES & ALGORITHM
SEMSETER : 3RD
YEAR : 2ND YEAR
ROLL NO :
CONTENT
CIRCULAR QUEUE INSERTION

1. INTRODUCTION
2. ABSTRACT
3. CIRCULAR QUEUE INSERTION
(INTRODUCTION)
4. ABSTRACT
5. INSERTION USING REPRESENTATION
6. ALGORITHM
7. IMPLEMENTATION
8. DIAGRAM
9. TERMINOLOGIES
10. ADD OPERATION
11. CODINGS
12. CONCLUSION
13. REFERENCES
CIRCULAR QUEUE INSERTION
INTRODUCTION
Insertion in Circular Queue is similar to how insertion is done in a queue.
Circular Queue is also implemented as FIFO (First In First Out) data structure.

The circular nature of this queue is implemented by linking the last element of
the queue with the first element.

A circular queue is the extended version of a regular queue where the last
element is connected to the first element. Thus forming a circle-like
structure.

ABSTRACT
It is common to use circular queues in a data structure in operating
systems. It is used to manage the execution of computing processes or
programs. You use a circular queue as a buffer to store the processes in
order of their insertion and then remove them at the time of resource
allocation or execution. In this tutorial, you will explore a circular queue
in a data structure along with its implementation and applications.

Why Was the Concept of Circular Queue Introduced?

Implementation of a linear queue brings the drawback of memory wastage.


However, memory is a crucial resource that you should always protect by analyzing
all the implications while designing algorithm or solutions. In the case of a linear
queue, when the rear pointer reaches the MaxSize of a queue, there might be a
possibility that after a certain number of dequeue() operations, it will create an
empty space at the start of a queue.

Additionally, this newly created empty space can never be re-utilized as the rear
pointer reaches the end of a queue. Hence, experts introduced the concept of the
circular queue to overcome this limitation.
As shown in the figure above, the rear pointer arrives at the beginning of a queue
with the help of a circular link to re-utilize the empty space to insert a new element.
This simple addition of a circular link resolves the problem of memory wastage in
the case of queue implementation. Thus, this particular type of queue is considered
the best version of a queue data structure.

What is Circular Queue in a Data Structure?

A circular queue is an extended version of a linear queue as it follows the First In


First Out principle with the exception that it connects the last node of a queue to its
first by forming a circular link. Hence, it is also called a Ring Buffer.

Insert using an Array Representation


When the last index of the queue has been used to insert an element, next element is inserted
at index 0 (if it is not already used).

Two variables FRONT and REAR are maintained to store the index of the first and last
elements of the queue respectively. To insert an element, REAR is compared with MAX (size
of array storing queue-1) or FRONT variable

If REAR is equal to MAX, new REAR will be 0 else REAR variable is incremented to get
the position for the new element.

Algorithm ENQUEUE_CIRCULAR_ARRAY(QUEUE, FRONT, REAR, ITEM)


Step 1 [If overflow, Exit algorithm. Insertion not possible]
IF (FRONT==REAR+1) OR (FRONT==0 AND REAR==MAX) Then Exit
Step 2 [Generate position for new element to be inserted]
IF REAR==MAX then
REAR=0
Else
REAR=REAR+1
Step 3 [Store new element at the rear]
QUEUE[REAR]=ITEM
Step 4 Exit
Insert using an Linked List
Representation
In dynamic Linked List representation of circular queue two pointers FRONT and REAR are
maintained to store the address of the first and last elements. LINK part of REAR stores the
address of first node (address stored in FRONT pointer)

To insert a new element in this implementation, first a node is created by fetching memory
using programming language specific function (malloc in C). This new node is identified by
a pointer NEW. To add this at the end, NEW->LINK gets LINK part of REAR Node, REAR-
>LINK gets NEW and REAR pointer gets NEW. This way the insertion in circular queue is
implemented.

Algorithm ENQUEUE_CIRCULAR_DYNAMIC (FRONT, REAR, ITEM)


Step 1 [Get a new node from memory]
NEW->DATA=ITEM
Step 2 [Add the new node to the QUEUE at the end]
If FRONT==NULL then [the new node becomes first as well as last
node]
FRONT=NEW
REAR=NEW
REAR->LINK=FRONT
Else
NEW->LINK=REAR->LINK
REAR->LINK=NEW
REAR=NEW
Step 3 Exit
Algorithm
insert(queue, key) −
begin
if front = 0 and rear = n – 1, or front = rear + 1, then queue is full, and return
otherwise
if front = -1, then front = 0 and rear = 0
else
if rear = n – 1, then, rear = 0, else rear := rear + 1
queue[rear] = key
end

why using circular queue ?


Implementation of a linear queue brings the drawback of memory wastage.
However, memory is a crucial resource that you will follow.

Additionally, this newly created empty space can never be re-utilized as the rear
pointer reaches the end of a queue. Hence, experts introduced the concept of the
circular queue to overcome this limitation.
Graphical representation of a circular queue is as follows...

Implementation of Circular Queue


enQueue(value) - Inserting value into the Circular
Queue
In a circular queue, enQueue() is a function which is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at rear position. The
enQueue() function takes one integer value as parameter and inserts that value into the
circular queue. We can use the following steps to insert an element into the circular queue...

• Step 1 - Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front ==
rear+1))
• Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
• Step 3 - If it is NOT FULL, then check rear == SIZE - 1 && front != 0 if it is TRUE, then
set rear = -1.
• Step 4 - Increment rear value by one (rear++), set queue[rear] = value and check
'front == -1' if it is TRUE, then set front = 0.

display() - Displays the elements of a Circular Queue


We can use the following steps to display the elements of a circular queue...

• Step 1 - Check whether queue is EMPTY. (front == -1)


• Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
• Step 4 - Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
• Step 5 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value
by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
• Step 6 - Set i to 0.
• Step 7 - Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the
same until 'i <= rear' becomes FALSE.

Basic terminologies
1. Front: The first pointer points to the first element in the queue.
2. Rear: The rear pointer points to the last element in the queue.
3. Enqueue: inserting an element into the queue is called
enqueue.
4. Dequeue: It is the process of deleting an element from the
queue.

Advantages of circular queue over linear queue


Linear Queue: A Linear Queue is generally referred to as Queue. It is
a linear data structure that follows the FIFO (First In First Out) order. A real-
life example of a queue is any queue of customers.

Circular Queue: Circular Queue is just a variation of the linear queue in


which front and rear-end are connected to each other to optimize the space
wastage of the Linear queue and make it efficient.
Add Operation in Circular Queue

For add operation in the circular queue first, we check if the value of is
equal to the value of size then, we will display a message Queue is full,
else we will increase the value of R by R=(R+1)%Size and add the element
in the array at the new location of R and then increased the value of by 1.

CODINGS
1. #include <stdio.h>
2.
3. # define max 6
4. int queue[max]; // array declaration
5. int front=-1;
6. int rear=-1;
7. // function to insert an element in a circular queue
8. void enqueue(int element)
9. {
10. if(front==-1 && rear==-1) // condition to check queue is empty
11. {
12. front=0;
13. rear=0;
14. queue[rear]=element;
15. }
16. else if((rear+1)%max==front) // condition to check queue is full
17. {
18. printf("Queue is overflow..");
19. }
20. else
21. {
22. rear=(rear+1)%max; // rear is incremented
23. queue[rear]=element; // assigning a value to the queue at the rear posi
tion.
24. }
25. }
26.
27. // function to delete the element from the queue
28. int dequeue()
29. {
30. if((front==-1) && (rear==-1)) // condition to check queue is empty
31. {
32. printf("\nQueue is underflow..");
33. }
34. else if(front==rear)
35. {
36. printf("\nThe dequeued element is %d", queue[front]);
37. front=-1;
38. rear=-1;
39. }
40. else
41. {
42. printf("\nThe dequeued element is %d", queue[front]);
43. front=(front+1)%max;
44. }
45. }
46. // function to display the elements of a queue
47. void display()
48. {
49. int i=front;
50. if(front==-1 && rear==-1)
51. {
52. printf("\n Queue is empty..");
53. }
54. else
55. {
56. printf("\nElements in a Queue are :");
57. while(i<=rear)
58. {
59. printf("%d,", queue[i]);
60. i=(i+1)%max;
61. }
62. }
63. }
64. int main()
65. {
66. int choice=1,x; // variables declaration
67.
68. while(choice<4 && choice!=0) // while loop
69. {
70. printf("\n Press 1: Insert an element");
71. printf("\nPress 2: Delete an element");
72. printf("\nPress 3: Display the element");
73. printf("\nEnter your choice");
74. scanf("%d", &choice);
75.
76. switch(choice)
77. {
78.
79. case 1:
80.
81. printf("Enter the element which is to be inserted");
82. scanf("%d", &x);
83. enqueue(x);
84. break;
85. case 2:
86. dequeue();
87. break;
88. case 3:
89. display();
90.
91. }}
92. return 0;

1. #include <stdio.h>
2. // Declaration of struct type node
3. struct node
4. {
5. int data;
6. struct node *next;
7. };
8. struct node *front=-1;
9. struct node *rear=-1;
10. // function to insert the element in the Queue
11. void enqueue(int x)
12. {
13. struct node *newnode; // declaration of pointer of struct node type.
14. newnode=(struct node *)malloc(sizeof(struct node)); // allocating the me
mory to the newnode
15. newnode->data=x;
16. newnode->next=0;
17. if(rear==-1) // checking whether the Queue is empty or not.
18. {
19. front=rear=newnode;
20. rear->next=front;
21. }
22. else
23. {
24. rear->next=newnode;
25. rear=newnode;
26. rear->next=front;
27. }
28. }
29.
30. // function to delete the element from the queue
31. void dequeue()
32. {
33. struct node *temp; // declaration of pointer of node type
34. temp=front;
35. if((front==-1)&&(rear==-
1)) // checking whether the queue is empty or not
36. {
37. printf("\nQueue is empty");
38. }
39. else if(front==rear) // checking whether the single element is left in the qu
eue
40. {
41. front=rear=-1;
42. free(temp);
43. }
44. else
45. {
46. front=front->next;
47. rear->next=front;
48. free(temp);
49. }
50. }
51.
52. // function to get the front of the queue
53. int peek()
54. {
55. if((front==-1) &&(rear==-1))
56. {
57. printf("\nQueue is empty");
58. }
59. else
60. {
61. printf("\nThe front element is %d", front->data);
62. }
63. }
64.
65. // function to display all the elements of the queue
66. void display()
67. {
68. struct node *temp;
69. temp=front;
70. printf("\n The elements in a Queue are : ");
71. if((front==-1) && (rear==-1))
72. {
73. printf("Queue is empty");
74. }
75.
76. else
77. {
78. while(temp->next!=front)
79. {
80. printf("%d,", temp->data);
81. temp=temp->next;
82. }
83. printf("%d", temp->data);
84. }
85. }
86.
87. void main()
88. {
89. enqueue(34);
90. enqueue(10);
91. enqueue(23);
92. display();
93. dequeue();
94. peek();
CONCLUSION
There was one limitation in the array implementation of Queue. If the
rear reaches to the end position of the Queue then there might be
possibility that some vacant spaces are left in the beginning which
cannot be utilized. So, to overcome such limitations, the concept of
the circular queue was introduced.

he circular queue is an important data structure where you need to


make the first element follow after traversing or accessing the last
element.

· A circular queue is an extended version of a linear queue as it follows


the First In First Out principle with the exception that it connects the
last node of a queue
REFERENCES
www.programiz.com

www.tutorialspoint.com

csveda.com

www.simplilearn.com

www.studytonight.com

www.tutorialride.com

btechsmartclass.com

www.includehelp.com

You might also like