Best C Programming code for Linear Queue using Dynamic Memory Allocation – February,2021
Introduction
In this blog I am going to explain Circular Queue using static 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<stdlib.h>
typedef struct linque
{
int val;
struct linque *next;
}linq;
//linq *createnode(linq *,int );
void enqueue(linq **,int);
int dequeue(linq **);
int isempty(linq *);
int peek(linq *);
void disp(linq *);
void main()
{
linq *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");
}
}
}
/*linq *createnode(linq *h,int v)
{
linq *ptr,*temp;
temp=(linq*)malloc(sizeof(linq));
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(linq **h,int v)
{
linq *ptr,*temp;
temp=(linq *)malloc(sizeof(linq));
temp->val=v;
temp->next=NULL;
if(*h==NULL)
{
*h=temp;
}
else
{
ptr=*h;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
int dequeue(linq **h)
{
int p;
linq *ptr;
ptr=*h;
p=(*h)->val;
*h=(*h)->next;
return p;
}
int isempty(linq *h)
{
if(h==NULL)
return 1;
else
return 0;
}
int peek(linq *h)
{
return h->val;
}
void disp(linq *h)
{
linq *ptr;
ptr=h;
while(ptr!=NULL)
{
printf("%d,",ptr->val);
ptr=ptr->next;
}
}