单链表查找结点按值

#include
#include
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;

//单链表的创建
LinkList *Create_link(int n)
{
LNode *head,*p,*q;
int i;
int x;
p=(LNode *)malloc (sizeof(LNode));
p->next=NULL;
head=p;
printf("enter:");
scanf("%d",&x);
for(i=1;i<=n;i++)
{
q=(LNode *)malloc (sizeof(LNode));
q->data=x;
q->next=NULL;
p->next=q;
p=q;
scanf("%d",&x);
}
return(head);
}
//单链表按值查找
LNode *SerchList(LNode *L,int x)
{
LNode *p=L;
int j=0;
while (p->next!=NULL&&p->data!=x)
{
p=p->next;
j++;
}
if(p->next==NULL) printf("not found!\n");
else printf("小标是%d\n",j);
}
//主程序
void main()
{
LinkList L;int x;//int i;
LNode *p;
L=Create_link(10);
p=L->next;
while(p)
{
printf("\n p->data=%d\n ",p->data);
p=p->next;
}
printf("输入要查找的结点的值:");
scanf("%d",&x);
SerchList(L,x);
}

相关文档
最新文档