Suppose a linked list is created with four fields in the information part and the user wants to modify the contents of a node of the linked list.
Creating the linked list and searching the linked list have been discussed earlier. This program on linked list also involves these two operations. After creating the list searching is required for modification of values. On successful seaching the values of the specific node is modified.
The program would be as follows :-
# include < stdio.h>
# include < stdlib.h>
struct link
{
char name[100];
char surname[100];
char add[100];
long ph;
struct link *next;
};
int x=0;
struct link *node;
int search(char ch[],struct link *n)
{
char c;
for(node=n; node->next != NULL; node = node->next)
{
if(strcmp(node-> name,ch)==0)
{
x++;
fflush(stdin);
printf("\nWant to change the surname ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Surname :-");
gets(node->surname);
}
printf("\nWant to change the address ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Address :-");
gets(node->add);
}
printf("\nWant to change the phone number ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Phone number :-");
scanf("%ld",&node->ph);
}
}
}
if(x == 0)
return 0;
else
return 1;
}
void display(struct link *n)
{
for(node=n; node->next != NULL; node = node->next)
{
printf("\n%s\n",node->name);
printf("%s\n",node->surname);
printf("%s\n",node->add);
printf("%ld\n",node->ph);
printf("\n*****************\n");
}
}
void get(struct link *n)
{
char ch='y';
node=n;
while(ch !='n')
{
fflush(stdin);
printf("\n Enter the name: -");
scanf("%s",node->name);
printf("\n Enter the surname: -");
scanf("%s",node->surname);
printf("\n Enter the address: -");
scanf("%s",node->add);
printf("\n Enter the Telephone number: -");
scanf("%ld",&node->ph);
node->next = (struct link* ) malloc (sizeof (struct link));
node = node->next;
node->next =NULL;
printf("\n Input choice n for break: ");
ch=getche();
}
}
void main()
{
struct link *start = (struct link *)malloc(sizeof(struct link));
char name[15];
int i=0;
clrscr();
get(start);
display(start);
while(i==0)
{
printf("\nEnter the name whose record is to be changed :");
scanf("%s",name);
i=search(name,start);
}
display(start);
getch();
}
Program to display a linked list from front side and reverse side
#include
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}
No comments:
Post a Comment