Monday, April 18, 2011

Doubly Link List (Menu based program) - Sweta Mam

/* Doubly link list by sweta mam */

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

struct dlist
{
int info;
struct dlist *next, *prev;
} *st;

void main()
{
int ch;
clrscr();
while(1)
{ clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete\n4. Insert After");
puts("5. Insert Before\n6. Display");
puts("7. Save list to disk.\n8. Read from disk\n9. Exit.");
printf("Enter your choice (1-9): ");
scanf("%d",&ch);
switch(ch)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_aft(st);break;
case 5: ins_bef(st); break;
case 6: disp(st);break;
case 7: save_list(st); break;
case 8: read_list(st);break;
case 9: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 9\n");
}
}
}

/* Add function here */
add(struct dlist *s)
{
struct dlist *temp;
int n;
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter info : ");
scanf("%d",&n);
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
temp->prev = s;
}

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

/* Delete function here */
del(struct dlist *s)
{
int n;
printf("Delete?");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
s->prev->next = s->next;
if(s->next!=NULL)
s->next->prev = s->prev;

free(s);
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert before function here */
ins_bef(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert before? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->prev = s->prev;
temp->next = s;
s->prev = temp;
if(s==st)
st = temp;
else
temp->prev->next = temp;
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert after */
ins_aft(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->next = s->next;
temp->prev = s;

if(s->next!=NULL)
{
s->next->prev = temp;
s->next = temp;
return;
}
else
{
temp->next = NULL;
s->next = temp;
return;
}
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* display function here */
disp(struct dlist *s)
{
clrscr();
printf("List are : \n");
printf("\n|\tPrev\t|\tInfo\t|\tNext\t|");
if(s==NULL)
{
printf("\n\n\tNo list created yet.");
getch();
return;
}
while(s!=NULL)
{

printf("\n|\t%5u\t|\t%d\t|\t%5u\t|",s->prev,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

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

save_list(struct dlist *s)
{
FILE *fp;
int n;
fp = fopen("dlist.txt", "a+");
clrscr();
if(s==NULL)
{
printf("\nNo list is to save.");
fclose(fp);
return;
}
do
{
n = s->info;
fprintf(fp,"%d\n",n);
s = s->next;
}while(s!=NULL);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dlist *s)
{
FILE *fp;
struct dlist *temp;
int n;
if((fp = fopen("dlist.txt","r"))==NULL)
{
printf("File cannot open.");
getch();
return;
}

clrscr();
do
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
fscanf(fp,"%d\n",&n);
printf("\nRead form file is: %d",n);
temp->info = n;
temp->next = NULL;
if(s==NULL) st = s = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}while(!feof(fp));
printf("\nList imported sucessfull.");
getch();
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites