求助贴,带头结点的单链表按值查找
写一个算法,实现在带头结点的单链表中的按值查找locate(p,x)。若在头结点为P的单链表中找到了数据为X的结点,则返回首次找到的结点的序号,若未找到,则返回一个特定的值-1。
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
# define null 0
#define m 10
#define x 6
typedef struct LNode
{int data;
struct LNode *next;
j++; } Lnode,*Linklist;
if(!p) return -1;
}
void creatlist(Linklist &;l,int n)
{l=(Linklist)malloc(sizeof(Lnode));
l->next=null;
int i;
for(i=0;i<n;i++)
{ Linklist p;
p=(Linklist)malloc(sizeof(Lnode));
scanf("%d",&;p->data);
p->next=l->next; }
l->next=p; }
int locate(Linklist &;l,int n)
{ Linklist p=l->next;
int j=1,e;
while(p) { if(p->data==n) return j;
p=p->next;
}
int main()
{Linklist l;
int q;
creatlist(l,m);
printf("%d 在链表 l 中的位置是:\n",x);
q=locate(l,x); printf("%d\n",q);
return 0; }
有错误,麻烦帮忙修改,谢谢