queue using linked list in c |
- QUEUE USING LINKED LIST
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*first=NULL,*temp=NULL,*newnode=NULL;
struct node* createnode()
{
int x;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter value:");
scanf("%d",&x);
if(temp!=NULL)
{
temp->info=x;
temp->next=NULL;
return temp;
}
else
{
printf("\nMemory not allocated");
return 0;
}
}
void display()
{
if(first!=NULL)
{
for(temp=first;temp->next!=NULL;temp=temp->next)
printf("%d->",temp->info);
printf("%d->NULL",temp->info);
}
else
printf("\nqueue is empty");
}
void ial()
{
newnode=createnode();
if(first==NULL)
{
first=newnode;
}
else
{
for(temp=first;temp->next!=NULL;temp=temp->next);
temp->next=newnode;
newnode->next=NULL;
}
display();
}
void daf()
{
if(first!=NULL)
{
struct node *t;
t=first;
temp=t->next;
first=temp;
free(t);
}
display();
}
void main()
{
int choice;
do
{
printf("\n\nMENU\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
ial();
break;
case 2:
daf();
break;
case 3:
display();
break;
case 4:
exit(0);
default :
printf("\nInvalid choice.");
}
}while(c!=4);
getch();
}
HAPPY CODING👀
#include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *next; }*first=NULL,*temp=NULL,*newnode=NULL; struct node* createnode() { int x; temp=(struct node*)malloc(sizeof(struct node)); printf("\nEnter value:"); scanf("%d",&x); if(temp!=NULL) { temp->info=x; temp->next=NULL; return temp; } else { printf("\nMemory not allocated"); return 0; } } void display() { if(first!=NULL) { for(temp=first;temp->next!=NULL;temp=temp->next) printf("%d->",temp->info); printf("%d->NULL",temp->info); } else printf("\nqueue is empty"); } void ial() { newnode=createnode(); if(first==NULL) { first=newnode; } else { for(temp=first;temp->next!=NULL;temp=temp->next); temp->next=newnode; newnode->next=NULL; } display(); } void daf() { if(first!=NULL) { struct node *t; t=first; temp=t->next; first=temp; free(t); } display(); } void main() { int choice; do { printf("\n\nMENU\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit"); printf("\nEnter choice:"); scanf("%d",&choice); switch(choice) { case 1: ial(); break; case 2: daf(); break; case 3: display(); break; case 4: exit(0); default : printf("\nInvalid choice."); } }while(c!=4); getch(); } |
Post a Comment
Hii