Code for implementing Circular Queue using LinkedĀ List
For learning pointer, dynamic memory allocation, linked list, you should go through this code. This is Code for implementing Circular Queue using Linked List.
In C code practice, this Code for implementing Circular Queue using Linked List logic plays a very important role.
So I have designed this code as easy as possible for the students. They will be able to understand each and every step very easily.
For more code click here. For video tutorial on several topics click here
#include
#include
typedef struct circular {
int val;
struct circular * next;
}
cir;
void enqueue(cir ** , int);
int dequeue(cir ** );
int isempty(cir * );
int peek(cir * );
void disp(cir * );
void main() {
cir * h = NULL;
int v, p;
while (1) {
printf("\n1. Insert value \n2. Delete value \n3. Display Front value \n4. Display Queue elements \n5. Exit");
printf("\n\n Enter the choice : ");
scanf("%d", & p);
switch (p) {
case 1:
printf("\n Enter the value : ");
scanf("%d", & v);
enqueue( & h, v);
break;
case 2:
v = dequeue( & h);
printf("\n\n Deleted value is : %d", v);
break;
case 3:
v = peek(h);
printf("\n\n Front value is : %d", v);
break;
case 4:
printf("\n\n The elements in the queue are : ");
disp(h);
break;
case 5:
exit(0);
default:
printf("\n\n Not a proper choice");
}
}
}
void enqueue(cir ** h, int v) {
cir * ptr, * temp;
temp = (cir * ) malloc(sizeof(cir));
temp -> val = v;
temp -> next = NULL;
if ( * h == NULL) {
* h = temp;
} else {
ptr = * h;
while (ptr -> next != * h)
ptr = ptr -> next;
ptr -> next = temp;
}
temp -> next = * h;
}
int dequeue(cir ** h) {
int p;
cir * temp;
temp = * h;
while (temp -> next != * h)
temp = temp -> next;
p = ( * h) -> val;
* h = ( * h) -> next;
temp -> next = * h;
return p;
}
int isempty(cir * h) {
if (h == NULL)
return 1;
else
return 0;
}
int peek(cir * h) {
return h -> val;
}
void disp(cir * h) {
cir * ptr;
ptr = h;
while (ptr -> next != h) {
printf("|%2d|", ptr -> val);
ptr = ptr -> next;
}
printf("%2d|", ptr -> val);
}