Showing posts with label Link List. Show all posts
Showing posts with label Link List. Show all posts

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();
}

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();
}

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();
}

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();
}

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();
}

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();
}

Friday, March 25, 2011

Menu based program on C linked list


/*Menu based program on C linked list
In this a menu based C linked list program, all the operations, inserting new node

in the linked list and deletion of node from the linked list are done in this

program. Insertion and deletion can be performed on any location of the linked

list.*/

#include< stdio.h>
#include< stdlib.h>
struct tag
{
int a;
struct tag *next;
};
struct tag *p,*node,*new1;

void addL(struct tag *);
void display(struct tag *);
struct tag *addF(struct tag *);
struct tag *delF(struct tag *);
void delL(struct tag *);
struct tag *delA(struct tag *);
struct tag *insA(struct tag *);

void main()
{
struct tag *start;
char ch='a';
clrscr();
start=(struct tag *)malloc(sizeof(struct tag));
printf("\n enter the value =");
scanf("%d",&start->a);
start->next=NULL;
while(ch!='e')
{
do
{
printf("\n press 'c' for add at last location,");
printf("\n'a'for add at first location,");
printf("\n'f'for delete from first location,");
printf("\n 'l' for delete from last location ,");
printf("\n 'i' fot insertion at any location,\n ");
printf("'d' for delete at any location ,");
printf("\n 'e' for exit");

ch=tolower(getche());
clrscr();
}while(strchr("caflide",ch)==NULL);

if(ch=='c')
{
addL(start);
display(start);
}

else if(ch=='a')
{
start=addF(start);
display(start);
}
else if(ch=='f')
{
start=delF(start);
display(start);
}
else if(ch=='l')
{
delL(start);
display(start);
}
else if(ch=='i')
{
start=insA(start);
display(start);
}
else if(ch=='d')
{
start=delA(start);
display(start);
}
else
break;
}
}


void addL(struct tag *n)
{
node=n;
while(node->next!=NULL)
{
node=node->next;
}
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
printf("\n value=");
scanf("%d",&node->a);
node->next=NULL;
}


void display(struct tag *n)
{
node=n;
printf("\nPresently the list is\n");
while(node)
{
printf("%4d",node->a);
node=node->next;
}
}


struct tag *addF(struct tag *n)
{
node=n;
new1=(struct tag *)malloc(sizeof(struct tag *));
printf("\n value=");
scanf("%d",&new1->a);
new1->next=node;
return new1;
}

struct tag *delF(struct tag *n)
{
node=n;
p=node->next;
free(node);
return p;
}

void delL(struct tag *n)
{
node=n;
p=node->next;

while(p->next!=NULL)
{
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}

struct tag *delA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n which location:");
scanf("%d",&count);
if(c==count)
{
free(node);
n=p;
}
else
{
while(p->next!=NULL)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}
return n;
}


struct tag *insA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n in which location:");
scanf("%d",&count);
new1=(struct tag *)malloc(sizeof(struct tag));
printf("\n value=");
scanf("%d",&new1->a);
if(c==count)
{
new1->next=node;
n=new1;
}
else
{
while(p)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=new1;
new1->next=p;
}
return n;
}


/* Technical analysis of the above menu based linked list program

Total number of functions defined in this linked list program is six. They are as

follows

void addL(struct tag *);
This function is used for inserting new node at last location of the linked list.

void display(struct tag *);
This function is used to display the linked list after every operation.

struct tag *addF(struct tag *);
This function is used for inserting new node at first location of the linked list.

struct tag *delF(struct tag *);
This function is used for deleting node from the first location of the linked list.

void delL(struct tag *);
This function is used for deleting node from last location of the linked list.

struct tag *delA(struct tag *);
This function is used for deletion of any specified node of the linked list.

struct tag *insA(struct tag *);
This function is used for inserting new node at any location of the linked list.

Within main () function user choice is taken and accordingly functions are called to

perform the necessary job in the linked list.

About the function calling statement ‘strchr("caflide",ch)==NUL’ – this is a

predefined function in string.h header file which is used to search a specific

character from a string. The first argument in the function is the string and second

argument is the specific character. If the search is successful, it returns the

address of the character in the string otherwise returns NULL.*/

Menu based program on C linked list


/*Menu based program on C linked list
In this a menu based C linked list program, all the operations, inserting new node

in the linked list and deletion of node from the linked list are done in this

program. Insertion and deletion can be performed on any location of the linked

list.*/

#include< stdio.h>
#include< stdlib.h>
struct tag
{
int a;
struct tag *next;
};
struct tag *p,*node,*new1;

void addL(struct tag *);
void display(struct tag *);
struct tag *addF(struct tag *);
struct tag *delF(struct tag *);
void delL(struct tag *);
struct tag *delA(struct tag *);
struct tag *insA(struct tag *);

void main()
{
struct tag *start;
char ch='a';
clrscr();
start=(struct tag *)malloc(sizeof(struct tag));
printf("\n enter the value =");
scanf("%d",&start->a);
start->next=NULL;
while(ch!='e')
{
do
{
printf("\n press 'c' for add at last location,");
printf("\n'a'for add at first location,");
printf("\n'f'for delete from first location,");
printf("\n 'l' for delete from last location ,");
printf("\n 'i' fot insertion at any location,\n ");
printf("'d' for delete at any location ,");
printf("\n 'e' for exit");

ch=tolower(getche());
clrscr();
}while(strchr("caflide",ch)==NULL);

if(ch=='c')
{
addL(start);
display(start);
}

else if(ch=='a')
{
start=addF(start);
display(start);
}
else if(ch=='f')
{
start=delF(start);
display(start);
}
else if(ch=='l')
{
delL(start);
display(start);
}
else if(ch=='i')
{
start=insA(start);
display(start);
}
else if(ch=='d')
{
start=delA(start);
display(start);
}
else
break;
}
}


void addL(struct tag *n)
{
node=n;
while(node->next!=NULL)
{
node=node->next;
}
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
printf("\n value=");
scanf("%d",&node->a);
node->next=NULL;
}


void display(struct tag *n)
{
node=n;
printf("\nPresently the list is\n");
while(node)
{
printf("%4d",node->a);
node=node->next;
}
}


struct tag *addF(struct tag *n)
{
node=n;
new1=(struct tag *)malloc(sizeof(struct tag *));
printf("\n value=");
scanf("%d",&new1->a);
new1->next=node;
return new1;
}

struct tag *delF(struct tag *n)
{
node=n;
p=node->next;
free(node);
return p;
}

void delL(struct tag *n)
{
node=n;
p=node->next;

while(p->next!=NULL)
{
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}

struct tag *delA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n which location:");
scanf("%d",&count);
if(c==count)
{
free(node);
n=p;
}
else
{
while(p->next!=NULL)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}
return n;
}


struct tag *insA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n in which location:");
scanf("%d",&count);
new1=(struct tag *)malloc(sizeof(struct tag));
printf("\n value=");
scanf("%d",&new1->a);
if(c==count)
{
new1->next=node;
n=new1;
}
else
{
while(p)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=new1;
new1->next=p;
}
return n;
}


/* Technical analysis of the above menu based linked list program

Total number of functions defined in this linked list program is six. They are as

follows

void addL(struct tag *);
This function is used for inserting new node at last location of the linked list.

void display(struct tag *);
This function is used to display the linked list after every operation.

struct tag *addF(struct tag *);
This function is used for inserting new node at first location of the linked list.

struct tag *delF(struct tag *);
This function is used for deleting node from the first location of the linked list.

void delL(struct tag *);
This function is used for deleting node from last location of the linked list.

struct tag *delA(struct tag *);
This function is used for deletion of any specified node of the linked list.

struct tag *insA(struct tag *);
This function is used for inserting new node at any location of the linked list.

Within main () function user choice is taken and accordingly functions are called to

perform the necessary job in the linked list.

About the function calling statement ‘strchr("caflide",ch)==NUL’ – this is a

predefined function in string.h header file which is used to search a specific

character from a string. The first argument in the function is the string and second

argument is the specific character. If the search is successful, it returns the

address of the character in the string otherwise returns NULL.*/

Circular Doubly linked list


/*Program on circular doubly linked list without any header node.*/


#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
struct list *prev;
};

struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}

node->next=n;
n->prev=node;
}


void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}


void main()
{
struct list *start=NULL;
clrscr();
create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
getch();
}




Circular Doubly linked list


/*Program on circular doubly linked list without any header node.*/


#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
struct list *prev;
};

struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}

node->next=n;
n->prev=node;
}


void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}


void main()
{
struct list *start=NULL;
clrscr();
create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
getch();
}




Circular Doubly linked list


/* Another program on circular doubly linked list with header node*/


/*In this program insertion of new created node at any location
  of the circular doubly linked list can be done.*/

#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;

void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}

node->next=n;
n->prev=node;
}

void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}


void insert(struct list *n)
{
struct list *node,*new1;
int c=1,count;
node=n;
new1=(struct list*)malloc(sizeof(struct list));
printf("\nEnter the location where the new location will be inserted:");
scanf("%d",&count);
printf("\Enter value:-");
scanf("%d",&new1->num);
do
{
if(c==count)
break;
node=node->next;
c++;
}while(node!=n);

new1->next=node->next;
node->next->prev=new1;
node->next=new1;
new1->prev=node;
}



void main()
{
struct list *start;
clrscr();
start=(struct list*)malloc(sizeof(struct list));

/* header node without any value */

create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
insert(start);
printf("\nAfter insertion, the list is\n");
display(start);
getch();
}

Circular Doubly linked list


/* Another program on circular doubly linked list with header node*/


/*In this program insertion of new created node at any location
  of the circular doubly linked list can be done.*/

#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;

void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}

node->next=n;
n->prev=node;
}

void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}


void insert(struct list *n)
{
struct list *node,*new1;
int c=1,count;
node=n;
new1=(struct list*)malloc(sizeof(struct list));
printf("\nEnter the location where the new location will be inserted:");
scanf("%d",&count);
printf("\Enter value:-");
scanf("%d",&new1->num);
do
{
if(c==count)
break;
node=node->next;
c++;
}while(node!=n);

new1->next=node->next;
node->next->prev=new1;
node->next=new1;
new1->prev=node;
}



void main()
{
struct list *start;
clrscr();
start=(struct list*)malloc(sizeof(struct list));

/* header node without any value */

create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
insert(start);
printf("\nAfter insertion, the list is\n");
display(start);
getch();
}

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites