Pages

Thursday, December 30, 2010

Single Linked List : Source Code

#include

#include

#include // for malloc() and free()


struct person
{
int personid;
char name[15];

person *next;
}*start;// start points to the first node

void main()
{
start=NULL;// till there is node keep start as null
void add();
void traverse(); // to display all nodes
void search();
void del();
int c;
do
{
printf("\nMAIN MENU");
printf("\n1. Add a person");
printf("\n2. Display all persons");
printf("\n3. Search a person");
printf("\n4. Delete a person");
printf("\n5. Quit");
printf("\nSelect your choice ");
scanf("%d",&c);
if(c==1)
add();
else if (c==2)
traverse();
else if(c==3)
search();
else if(c==4)
del();
}while(c!=5);
}
void add()
{
clrscr();
person *pers,*ptr,*prev;
pers=(person *) malloc(sizeof(person)); // allocating memory using the malloc()
if(pers==NULL)// in case allocation is unsuccessful
{
printf("\nNot enough memory");
return;
}
// if allocation succeeds
printf("\nEnter Person Id ");
scanf("%d",&pers->personid);
printf("\nEnter Person name ");
scanf("%s",&pers->name);
if(start==NULL) // checking if this is the first node
{
start=pers; // to make it the first node, make start point to it
pers->next=NULL;// since it is the first node, its next part will store NULL
}
else // from 2nd node onwards
{
for(ptr=start;(ptr);ptr=ptr->next) // searching for the last node of the list
      prev=ptr;
prev->next=pers; // making next of last node point to the new node
pers->next=NULL; // new node's next is NULL
}
printf("\nPerson Added. Press enter to continue...");
getch();
clrscr();
}

void traverse() // printing the linked list
{
clrscr();
person *ptr;
if(start==NULL) // checking if nodes have been added or not
{
printf("\nNo Person Found");
return;
}
for(ptr=start;(ptr);ptr=ptr->next)// looping from the start till the end of the list
{
printf("\nPerson Id : %d",ptr->personid);//printing the details
printf("\nName : %s",ptr->name);
printf("\n---------------------------");
}
printf("\nEnd of list");
printf("\nPress enter to continue..");
getch();
clrscr();
}
void search()
{
clrscr();
printf("\nSearch a person");
int pid,flag;
char ch;
person *ptr;
if(start==NULL)
{
printf("\nNo Person Found");
return;
}
printf("\nEnter Person Id To Search : ");
scanf("%d",&pid);
for(ptr=start;(ptr);ptr=ptr->next)
{
if(ptr->personid==pid)
{
flag=1;
printf("\nPerson Found");
printf("\n=============");
printf("\nPerson Id : %d",ptr->personid);
printf("\nName : %s",ptr->name);
fflush(stdin);
printf("\nDo you want to modify the data? : ");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
printf("\nEnter New Person Id "); // overwriting the data
scanf("%d",&ptr->personid);
printf("\nEnter New Name ");
scanf("%s",&ptr->name);
printf("\nData Updated !!");
}
}
}


if(flag!=1)
printf("\nPerson Id %d not found",pid);
}


void del()
{
clrscr();
printf("\nDelete a person");
person *ptr,*prev;
int pid,flag;
char ch;
if(start==NULL)
{
printf("\nNo Person Found");
return;
}
printf("\nEnter Person Id To Delete : ");
scanf("%d",&pid);
for(ptr=start;(ptr);ptr=ptr->next)
{
if(ptr->personid==pid)
{
flag=1;
printf("\nPerson Found");
printf("\n=============");
printf("\nPerson Id : %d",ptr->personid);
printf("\nName : %s",ptr->name);
fflush(stdin);
printf("\nDo you want to delete the data? : ");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
if(ptr->next==NULL) // if the last node is to be deleted
prev->next=NULL;
else if(ptr==start) // if the first node is to be deleted
start=ptr->next;
else
prev->next=ptr->next;// deleting the middle node
free (ptr); // freeing memory using the free()
printf("\nPerson Deleted");
}
}
prev=ptr;
}
if(flag!=1)
printf("\nPerson Id %d not found",pid);
}


0 comments:

Post a Comment