Monday, April 18, 2011

Circular Doubly link list program by Sweta mam

/* Circular Doubly link list program by sweta mam */

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

struct dclist
{
int info;
struct dclist *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 Forward\n7. Display Backward");
puts("8. Save list to disk.\n9. Read from disk\n10. 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_frd(st);break;
case 7: disp_bck(st); break;
case 8: save_list(st); break;
case 9: read_list(st);break;
case 10: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 10\n");
}
}
}

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

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

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

s = s->next;

}while(s!=st);
printf("%d not found.",n);
}
getch();
}

/* to edited form and modify functins insert before function here */
ins_bef(struct dclist *s)
{
int n;
struct dclist *temp;

if(s!=NULL)
{
printf("Insert before? ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
temp->prev = s->prev;
temp->prev->next = temp;
s->prev = temp;

if(s==st)
st = temp;
return;
}
s = s->next;

}while(s!=st);
}

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

/* insert after */
ins_aft(struct dclist *s)
{
int n;
struct dclist *temp;
if(s!=NULL)
{
printf("Insert after : ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->prev = s;
temp->next = s->next;
temp->next->prev = temp;
s->next = temp;
return;
}
s = s->next;
}while(s!=st);
printf("%d not found.");
}
else printf("\nList not created yet.");
getch();
}

/* display function here */

disp_frd(struct dclist *s)
{
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->next;
}while(s!=st);
getch();
}
else
{ printf("List not create yet.");
getch();
}
}

disp_bck(struct dclist *s)
{
for(;st!=s;s=s->next);
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->prev;
}while(s!=st);
getch();
}
else
printf("List is empty.");
}


disp(struct dclist *s)
{
printf("List are : \n");
printf("\n|\tAddress\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,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

f_disp(struct dclist *s)
{
if(s!=NULL)
{
printf("List is : ");
do
{
printf("%d, ",s->info);
s = s->next;
}while(s!=st);
}
else printf("No list yet.");

}

save_list(struct dclist *s)
{
FILE *fp;
int n;
fp = fopen("dclist.txt", "w");
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(st!=s->next);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dclist *s)
{
FILE *fp;
struct dclist *temp;
int n;
fp = fopen("dclist.txt","r");
clrscr();
do
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
fscanf(fp,"%d\n",&n);
temp->info = n;
temp->next = s;
if(s==NULL) st = s = temp;
else
{
for(;st!=s->next;s=s->next);
s->next = temp;
}
}while(!feof(fp));
for(;st!=s->next; s=s->next);
s->next = st;
printf("\nList imported sucessfull.");
getch();
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites