Searching in a single linked list with c

/*searching in linked list
@author Mitra N Mishra
*/

#include<stdio.h>
struct node
{
int value;
struct node *link;
};

void main()
{
int n;
printf(“Enter value to search\n”);
scanf(“%d”,&n);

struct node *p,n1,n2,n3,n4,n5;//for searching purpose we create a structure pointer p.and also five nodes.
struct node *start=&n1;//In linked list we only access or operation perform by the help pointer start.
n1.value=100;
n1.link=&n2;
n2.value=200;
n2.link=&n3;
n3.value=300;
n3.link=&n4;
n4.value=400;
n4.link=&n5;
n5.value=500;
n5.link=NULL;
p=start;
int pos=1;
while(p!=NULL)
{
if(p->value==n){
printf(“Item %d found at position %d\n”, n,pos);
return;
}

p=p->link;
pos++;
}
printf(“Item not found \n”);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s