Mini Project for CSE students: Circular Queue Program Implementing Single Linked list
Mini Project for CSE students :
This is a simple mini project for CSE students. You can do yourself just by understanding the actual concept of Circular Queue. We have seen already how to implement array in stack, linked list implementation and Queue implementation. Circular Queue is an advanced concept.
What is Circular Queue?
- Circular Queue is an abstract data type in which the last node is connected back to the front node making a circle.
- The elements are inserted into the queue at its rear end.
- The elements are deleted from front end of the queue.
- The front and rear pointers of the queue points to the array.
- Circular queue is also called as “Ring Buffer“.
To create a Circular Queue we can use three ways:
- Implementing single linked list
- Implementing double linked list
- Implementing array.
In this post we are going to use single linked list to create, insert, delete, display items of a circular queue.
Circular Queue Program Implementing Single Linked list :
<span style="color: #000000;">#include<alloc.h>
struct node front,*rear;
circ_add(struct node **f,struct node **r,int item)
struct node *q;
q=malloc(sizeof (struct node));
circ_delete(struct node **f,struct node **r)
struct node *q;
printf("Oh!! Queue is Empty!");
Step by Step Demonstration of implementing Circular Queue as linked list in C:
As I said earlier we are going to implement single linked list. Hence the following steps will explain in detail about implementing circular queue as linked list.
Step 1: Since we are trying to code in C language, we use structures here to create and implement linked list.
Now create the structure having elements as “data” to be stored and “link”, pointer to the node.
Step 2: Here we are going to perform three operations: Add, Delete, Display.
Step 3: To add an element to the circular queue:
- Allocate size in the structure using “malloc(sizeof (struct node))”
- Set input as data of the node q.
- Also here we have to check if the front is Null or not: if null then set q to the front.
- Otherwise, create a link rear and set front element to it and link to the rear (previous) element.
- Hence the inserted element is at front of the queue.
Step 4: To delete an element from the circular queue:
- To delete an element first check emptyness of the queue. i.e., if front is null, then queue is empty.
- If the queue is not empty, check it has one or more element.
- i.e., if front ==rear, then it has only one element.
If so, move that element to item(returned to main function as deleted element), then free that space making both front and rear as null.
- If the queue has more than one element, then delete the element at the front by setting next to front node as front.
- Also, here we have to set the link from the rear end to be set to the new front end.
Step 5: To display the elements of a Circular Queue:
- Display the element at the front node and set it as null.
- Then, move the next node using link specifying next node and display it.
- Likewise display all the elements of the queue until it reaches null node which was set already.
Thanks for reading…If you find any trouble with the above code, please let me know….Leave your comments below…