Here is the program on linear linked list implementation of queue where insertion and deletion can be performed as required, no limit.
# include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *p,*node,*new1,*start=NULL;
struct link *insert(struct link *nn)
{
node = nn;
p=NULL;
new1 = (struct link* ) malloc(sizeof(struct link));
while(node)
{
node=node->next;
p=p->next;
}
p->next=new1;
new1->next=node;
fflush(stdin);
printf("\n Input the node value: ");
gets(new1->info);
node=start->next;
return node;
}
void display(struct link *node)
{
while (node)
{
printf(" %s", node->info);
node = node->next;
}
}
struct link *del(struct link *nn)
{
node=nn;
if(node==NULL)
printf("\nDeletion not possible.");
else
{
p=node->next;
free(node);
node=p;
}
return node;
}
void main()
{
struct link *start=NULL;
int k = 0;
char choice;
clrscr();
do
{
printf("\nInsert->i Delete->d Quit->q:");
printf("\nInput the choice : ");
do
{
choice = getchar();
choice = tolower(choice);
}while(strchr("idq",choice)==NULL);
printf("Your choice is: %c ", choice);
switch(choice)
{
case 'i' :
{
start=insert(start);
printf("\nQueue after inserting ");
display(start);
break;
}
case 'd' :
{
start=del(start);
printf("\nQueue content after deleteion is as follows:\n");
display(start);
break;
}
case 'q':
{
k = 1;
}
}
} while(!k);
getch();
}
No comments:
Post a Comment