Best C Programming code for Linear Queue using Dynamic Memory Allocation – February,2021

#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;
 }

 }

This Post Has 2 Comments

  1. Katia

    Hi there! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look
    forward to all your posts! Carry on the great work!

Leave a Reply