Google

Program to create a FIFO Linked List

Written on:September 30, 2012
Comments are closed

Singly Linked List – Creation FIFO

Program to create a FIFO linked list

If a linked list is created considering FIFO order, then insertions are done at one end, whereas deletions are performed at the other end.  Thus, the first node to be deleted is only that node which is inserted first in the linked list.
/*Program for linked list creation,insertion and deletion.*/
/*FIFO Linked List*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct list
{int n;
struct list *next;
}*head=NULL;

struct list *createnode()
{struct list *temp;
temp=(struct list*)malloc(sizeof(struct list));
return temp;
}

struct list *makenode(int x)
{struct list *temp;
temp=createnode();
temp->n=x;
temp->next=NULL;
return temp;
}

/*Insertion condition-At the end*/
/*New element is inserted at the end*/

void insertend(int x)
{
struct list *temp,*ptr=makenode(x);
if(head==NULL)
{head=ptr;
}
else
{temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=ptr;
}
}

/*Deletion condition-At the beginning*/
/*Since first node inserted is to be removed first*/

void delnode()
{struct list *temp;
if(head==NULL)
{printf(“List is empty!”);
return;
}
temp=head;
head=head->next;
free(temp);
}

/*Displaying list*/

void displaylist()
{
struct list *temp;
if(head==NULL)
{printf(“nList is empty.”);
return;
}
temp=head;
while(temp!=NULL)
{printf(“%d->”,temp->n);
temp=temp->next;
}
printf(“bb “);
}

void main()
{
int ch,num;
while(1)
{
clrscr();
printf(“nLinked List (FIFO)-Menu”);
printf(“nn1. Insert”);
printf(“n2. Delete”);
printf(“n3. Display”);
printf(“n4. Exit”);
printf(“nnEnter your choice?”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“nEnter any number?”);
scanf(“%d”,&num);
insertend(num);
break;
case 2:
delnode();
break;
case 3:
displaylist();
break;
case 4:
exit(0);
default:
printf(“Invalid choice!”);
}
getch();
}
}

Sorry, the comment form is closed at this time.