Sunday, June 3, 2012

Modification of values of a specific node in linked list


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.

To modify values, firstly we have to search the specific node in the linked list. In this program there will be no insertion or deletion, simply modification of values.

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

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner