Best C Programming code for Circular Queue using Dynamic Memory Allocation – February,2021
Introduction
In this blog I am going to explain Circular Queue using dynamic memory allocation. The code I am going to write is very simple to understand. I hope you will get all the required ideas from this code.
If you are finding any difficulties to understand please feel free to contact through my email : creativeresource2020@gmail.com.
You can also put your comment below.
#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct circular { int val; struct circular *next; }cir; cir *createnode(cir *,int ); 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("\n 1.insert \n2.delete \n3.peek \n4.display \n5.exit"); printf("\nEnter the choice : "); scanf("%d",&p); switch(p) { case 1:printf("\nEnter the value : "); scanf("%d",&v); enqueue(&h,v); break; case 2: if(!isempty(h)) { printf("\ndeleted value is: %d",dequeue(&h)); } else { printf("\nNothing to display"); } break; case 3:if(!isempty(h)) { printf("\n front value: %d",peek(h)); } else { printf("\n Nothing to Display"); } break; case 4:printf("\n Values are : "); disp(h); break; case 5: exit(0); default:printf("\nnot a proper choice"); } } } cir *createnode(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; return h; } 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; temp->next=*h; } else { ptr=*h; while(ptr->next!=*h) ptr=ptr->next; temp->next=*h; ptr->next=temp; } } int dequeue(cir **h) { int p; cir *ptr; ptr=*h; p=(*h)->val; while(ptr->next!=*h) ptr=ptr->next; *h=ptr->next=(*h)->next; 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("%d,",ptr->val); ptr=ptr->next; } printf("%d",ptr->val); }