Google Search

Saturday, January 28, 2012

C PROGRAM TO IMPLEMENT QUEUE AS LINKED LIST


ALGORITHM TO IMPLEMENT QUEUE AS LINKED LIST

CREATE
1.  t = new node
2.  Enter info to be inserted 
3.  Read n
4.  t   info = n
5.  t   next = front 
6.  front = t

INSERTION
1.  r   next = t
2.  t   next = NULL
3.  Return

DELETION
1.  x = front
2.  front = front   next
3.  delnode(x)
4.  Return

DISPLAY
1.  If (front = NULL)
        Print “ empty queue”
        Return
    Else
       P = start
       Repeat until (p< > NULL)
       Print p   info
       P = p  next
       Return


Program 

#include<stdio.h>
#include<conio.h>
struct queue
{
int no;
struct queue *next;
}
*start=NULL;
void add();
int del();
void traverse();
void main()
{
int ch;
char choice;
do


{
clrscr();
printf("----1. add\n");
printf("----2. delete\n");
printf("----3. traverse\n");
printf("----4. exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
break;
case 2: printf("the delete element is\n%d",del());
break;
case 3: traverse();
break;
case 4: return;
default : printf("wrong choice\n");
};
fflush(stdin);
scanf("%c",&choice);
}
while(choice!=4);
}
void add()
{
struct queue *p,*temp;
temp=start;
p=(struct queue*)malloc(sizeof(struct queue));
printf("Enter the data");
scanf("%d",&p->no);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int del()
{
struct queue *temp;
int value;
if(start==NULL)
{
printf("queue is empty");
getch();
return(0);
}
else
{


temp=start;
value=temp->no;
start=start->next;
free(temp);
}
return(value);
}
void traverse()
{
struct queue *temp;
temp=start;
while(temp->next!=NULL)
{
printf("no=%d",temp->no);
temp=temp->next;
}
printf("no=%d",temp->no);
getch();

0 comments:

Post a Comment