Saturday, April 9, 2011

Link List Program(Menu Based) By-Sweta Mam

#include"stdio.h"
#include"stdlib.h"

struct list
{
int info;
struct list *next;
}*st;

FILE *fp;
/* ----------------------- main function here --------------------------------*/
int main()
{
int n;
while(1)
{
clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete");
puts("4. Insert Before\n5. Insert After");
puts("6. Display\n7. Save the list");
puts("8. Read form disk\n9. Exit");
fflush(stdin);
printf("\nEnter your choice : ");
fflush(stdin);
scanf("%d",&n);
switch(n)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_bef(st); break;
case 5: ins_aft(st); break;
case 6: disp(st); break;
case 7: save_list(st); break;
case 8: read_list(st); break;
case 9: del_all(st); exit(0);break;
default: puts("\nInvalid Input\nPlease enter 1-9");
getch();
}

}
}
del_all(struct list *s)
{
struct list *t;
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}
}


/*------------------------Add function declaration here -----------------------------*/
add(struct list *s)
{
struct list *temp;
int n;
printf("Enter the info: ");
scanf("%d",&n);

temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}


/* --------------------------modification function declaration here ------------------------*/

mod(struct list *s)
{
int n;
printf("Modify ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info: ");
scanf("%d",&n);
s->info = n;
return;
}
s=s->next;
}
printf("%d not found in list.",n);
getch();
}


/*--------------------Delete function from here ----------------*/

del(struct list *s)
{
int n;
struct list *p=NULL;
printf("Delete ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
p->next = s->next;
free(s);
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.");
getch();
}

/*--------------Insert before function from here-----------*/
ins_bef(struct list *s)
{
int n;
struct list *p=NULL, *temp;
printf("Insert before : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
temp = (struct list *)malloc(sizeof(struct list));
printf("\nEnter the new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
if(s==st)
st = temp;
else
p->next = temp;
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.",n);
getch();
}

/*----------------------Insert After function from here--------------*/
ins_aft(struct list *s)
{
int n;
struct list *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info : ");
scanf("%d",&n);
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = s->next;
s->next = temp;
return;
}
s=s->next;

}
printf("%d not found in the list.",n);
getch();
}



/*--------------Display function from here --------------*/
disp(struct list *s)
{
int n =1,c=0;
clrscr();
printf("\t|---------------------------------------|");
printf("\n\t|Address|\t| Info |\t| Next |\n");
printf("\t|---------------------------------------|\n");
while(s!=NULL)
{
printf("\t|%6u\t|\t|%6d |\t|%6u |\n",s,s->info,s->next);
s=s->next;
c=1;
n++;
}
printf("\t|---------------------------------------|\n");
if(c!=1)printf("\tNo list is created yet.");
getch();
}

/*-----------f_disp-----------------*/
f_disp(struct list *s)
{ if(s!=NULL)
printf("The list is : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s=s->next;
}
return;
}



save_list(struct list *s)
{
int n;
fp = fopen("list.txt", "w");
clrscr();
for(;s!=NULL;s=s->next)
{
n = s->info;
fflush(stdin);
fprintf(fp," %d \n",n);
}
fclose(fp);
getch();
}


/* read list function here */
read_list(struct list *s)
{
struct list *temp;
int n,i;/*
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}*/
fp = fopen("list.txt","r");
clrscr();
do
{

fscanf(fp," %d \n",&n);
printf("\nInfo imported : %d",n);
getch();
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
s = st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}

}while(!feof(fp));
printf("List imported sucessfull.");
getch();
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites