Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 15 January 2018

C program to implement a queue using linklist

#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype{
int info;
struct nodetype *next;
}node;
node *front=NULL,*rear=NULL;
void display(){
if(front==NULL){
printf("UNDERFLOW");
return;
}
node *p=front;
while(p!=NULL){
printf("-->%d",p->info);
p=p->next;
}
return;
}
void insert(ele)
{
node *temp=(node*)malloc(sizeof(node));
if(temp==NULL){
printf("OVERFLOW");
return;
}
temp->info=ele;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else{
rear->next=temp;
rear=temp;
}
display();
}
delete(){
if(front==NULL){
printf("UNDERFLOW");
return;
}
int p=front->info;
if(front->next==NULL)
front=rear=NULL;
else{
front=front->next;
}
printf("Deleted Element is :%d\n",p);
display();
}
main()
{
int i=0;
int ch,n;
do
{
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to insert :");
scanf("%d",&n);
insert(n);
break;
case 2:
delete();
break;
case 3:
printf("The remaining queue is :");
display();
break;
default:
printf("Wrong Choice\n");
break;
}
printf("press 1 to continue......");
scanf("%d",&i);
}
while(i==1);
}
x

No comments:

Post a Comment