求助贴,带头结点的单链表按值查找

mk199214 2012-03-19 07:03:17
写一个算法,实现在带头结点的单链表中的按值查找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; }



有错误,麻烦帮忙修改,谢谢
...全文
258 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mk199214 2012-03-19
  • 打赏
  • 举报
回复
不会吧,直接复制过来的啊,

# 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++;
}
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;
}Lnode,*Linklist;
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;

l->next=p; }
int main()
{Linklist l;
int q;
creatlist(l,m);
printf("%d 在链表 l 中的位置是:\n",x); q=locate(l,x);
printf("%d\n",q);
return 0;
}
W170532934 2012-03-19
  • 打赏
  • 举报
回复
我勒个去,你的代码是什么?怎么复制下来都是乱的

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧