Pages - Menu

Tuesday, 22 July 2014

Implementation of Queue using Linked list in C.

Here is the code..

//OPERATION ON SIMPLE QUEUE USING LINK LIST

#include<stdio.h>
#include<stdlib.h>
typedef struct q
{
int info;
struct q *next;
} node;
node *start=NULL;
void insert();
int del();
void traverse();
void main()
{
int ch;
char choice;
clrscr();
printf("----1. insertion\n");
printf("----2. deletion\n");
printf("----3. traverse\n");
printf("----4. exit\n");
do
{
printf("Enter your choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: printf("the deleted element is %d\n",del());
break;
case 3: traverse();
break;
case 4: 
exit(2);
default: printf("wrong choice\n");
}
}
while(1);
}
void insert()
{
node *p,*temp;
temp=start;
p=(node *)malloc(sizeof(node *));
printf("Enter the data\t\t");
scanf("%d",&p->info);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int del()
{
node *temp;
int value;
if(start==NULL)
{
printf("queue is empty\n");
return(0);
}
else
{
temp=start;
value=temp->info;
start=start->next;
free(temp);
}
return(value);
}
void traverse()
{
node *temp;
temp=start;
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d\n",temp->info);
}

For Turbo C users.
//OPERATION ON SIMPLE QUEUE USING LINK LIST

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
typedef struct q
{
int info;
struct q *next;
} node;
node *start=NULL;
void insert();
int del();
void traverse();
void main()
{
int ch;
char choice;
clrscr();
printf("----1. insertion\n");
printf("----2. deletion\n");
printf("----3. traverse\n");
printf("----4. exit\n");
do
{
printf("Enter your choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: printf("the deleted element is %d\n",del());
break;
case 3: traverse();
break;
case 4: getch();
exit(2);
default: printf("wrong choice\n");
}
}
while(1);
}
void insert()
{
node *p,*temp;
temp=start;
p=(node *)malloc(sizeof(node *));
printf("Enter the data\t\t");
scanf("%d",&p->info);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int del()
{
node *temp;
int value;
if(start==NULL)
{
printf("queue is empty\n");
return(0);
}
else
{
temp=start;
value=temp->info;
start=start->next;
free(temp);
}
return(value);
}
void traverse()
{
node *temp;
temp=start;
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d\n",temp->info);
}

No comments:

Post a Comment