如何创建一个有头结点的单链表

bljswpu 2010-04-20 07:01:34
1,如何创建有头结点的单链表
2
int getlist(link head, char ch)
{
link p;
p = head;
int i = 1, j;
j = length(head);
if(i>j) return 0;
while (p->data != ch && i<j)
{
p = p->next ;
i++;
}
return i;
}
该函数是实现从键盘上输入一个字符,判断它是否在单链表中,如果在,返回其位置,如果不在,返回0,
length是求表长度的函数
这个函数只能实现当我输入的字符在单链表中时,能返回其位置,但当我输入的字符不在表中时,它始终返回的是表的长度,

求各位大侠帮哈忙
...全文
971 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayjune56 2012-06-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
每天回帖有十分!
[/Quote]

真的吗?
thunder__god 2010-07-01
  • 打赏
  • 举报
回复
每天回帖有十分!
匚匚 2010-04-22
  • 打赏
  • 举报
回复
第二题:return (i>=j) ? 0 : i;
匚匚 2010-04-22
  • 打赏
  • 举报
回复
int Creorder(List head,int (*cbscanf)(ElementType *))//手动创建有序单链表
{
static int Inserlist(List ,ElementType);//函数原型声明
while(cbscanf(&head->Element)==1) //注意条件
if(Inserlist(head,head->Element)==-1) return -1;
return 1;
}
static int Inserlist(List head,ElementType e)//插入有序链表<可有相同值>
{
struct Node **cur,*next;
for(cur=&head->next; (next=*cur)!=NULL&&next->Element<e; cur=&next->next);
next=(List)malloc(sizeof(struct Node));
if (next==NULL) return -1;
next->Element=e;
next->next=*cur;
*cur=next;
return 1;
}
匚匚 2010-04-22
  • 打赏
  • 举报
回复
int Crelist(List root,int (*cbscanf)(ElementType *)) /*手动创建无序单链表*/
{
struct Node *cur,*next;
if(root->next!=NULL) root->next=NULL;
while(cbscanf(&root->Element)) //根据实际类型输入应用函数
{
if((next=(List)malloc(sizeof(struct Node)))==NULL)
{
if(root->next!=NULL)
cur->next=NULL;
return -1;
}
next->Element=root->Element;
if(root->next==NULL)//root指向的对象要初始化 next
{
root->next=next;
}
else
{
cur->next=next;
}
cur=next;
}
if(root->next!=NULL)
{
cur->next=NULL;
return 1;
}
else return 0;
}
bljswpu 2010-04-22
  • 打赏
  • 举报
回复
第一题呢?
aleyn 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hqweiei 的回复:]
int getlist(link head, char ch)
{
link p;
p = head;
int i = 1, j;
j = length(head);

while (p->data != ch && i<j)
{
p = p->next ;
i++;
}
return i;
}
if(i>……
[/Quote]

HQWEIEI 2010-04-20
  • 打赏
  • 举报
回复
int getlist(link head, char ch)
{
link p;
p = head;
int i = 1, j;
j = length(head);

while (p->data != ch && i<j)
{
p = p->next ;
i++;
}
return i;
}
if(i>=j) return 0;
else
return i;
ryangio 2010-04-20
  • 打赏
  • 举报
回复
在while后面的语句在加个判断就可以了。
if(i == j)
return 0;
else
return i;

70,020

社区成员

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

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