Friday, June 22, 2012

Searching values in Link List using C Program


In this program we will create five nodes and then the linked list will be traversed one after another to display the value stored within the node.
Our next program will create a linked list ( size depends on the user ) and each node will contain the Name , Surname , Address and Telephone number .Then the user has to enter a Name which will be searched in the linked list. If found the details record against the name will be displayed, otherwise a not found message will be displayed.

# include < stdio.h >
# include < malloc.h >
struct link
{
char name[100];
char surname[100];
char add[100];
long ph;
struct link *next;
};
int x=0;
struct link *node;
void search(char ch[],struct link *n)
{
node=n - > next;
for(; node != NULL; node = node->next)
{
if(strcmp(node-> name,ch)==0)
{
x++;
printf("Surname is %s \n", node->surname);
printf("Address is %s \n",node-> add);
printf("Phone number is %ld \n",node->ph);
}
}
if(x == 0)
printf("Item not found in the list \n");
}
void get(struct link *n)
{
char ch='y';
node=n;
while(ch !='n')
{
node->next = (struct link* ) malloc (sizeof (struct link));
node = node->next;
node->next =NULL;
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);
printf("\n Input choice n for break: ");
ch = getche();
}
}
void main()
{
struct link *start=NULL;
char name[15];
clrscr();
get(start);
printf("\nEnter the name to search :");
scanf("%s",name);
search(name,start);
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner