关于单循环链表

jody_go 2011-04-06 05:18:21
为了理解单循环链表,自己用c语言写了个小代码,但就是不晓得哪里错了,好郁闷!!!希望哪位大哥能帮我分析分析啊,感激不尽!

#include <stdio.h>
#include <stdlib.h>

struct cyclink
{
char name[10];
int id;
struct cyclink *next;
};

struct cyclink *creat(struct cyclink **head)
{
struct cyclink *newnode;
struct cyclink *his;

*head = (struct cyclink *)malloc(sizeof(struct cyclink)); /*动态生成一个节点*/
*head = (*head)->next;

newnode = (struct cyclink *)malloc(sizeof(struct cyclink));
printf("first input num:");
scanf("%d", &newnode->id);

if (newnode->id != 0)
{
printf("first input name:");
scanf("%s", newnode->name);
*head = newnode->next;
}

while (newnode->id != 0)
{
if ((*head)->next == *head)
{
newnode = (*head)->next;
}
else
{
his->next = newnode;
}
his = newnode;
newnode = (struct cyclink *)malloc(sizeof(struct cyclink));
printf("more input num:");
scanf("%d", &newnode->id);

if (newnode->id != 0)
{
printf("more input name:");
scanf("%s", newnode->name);
*head = newnode->next;
}
}

return *head;
}

void print(struct cyclink *head)
{
struct cyclink *temp;
temp = head;

if (head == head->next)
{
printf("no node!\n");
}
else
{
while (temp != temp->next)
{
printf("id: %d\n", temp->id);
printf("name: %s\n", temp->name);
temp = temp->next;
}
}

}

int main()
{
struct cyclink *head;
head = creat(&head);

exit(0);
}

...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
newfarmerchi 2011-04-06
  • 打赏
  • 举报
回复

//如果已经搞定,just as reference.
#include <stdio.h>
#include <stdlib.h>

struct cyclink
{
char name[10];
int id;
struct cyclink *next;
};

struct cyclink *creat(struct cyclink **head)
{
struct cyclink *newnode;
struct cyclink *his;

*head = (struct cyclink *)malloc(sizeof(struct cyclink)); /*动态生成一个节点*/
//*head = (*head)->next;


newnode = (struct cyclink *)malloc(sizeof(struct cyclink));
printf("first input num:");
scanf("%d", &newnode->id);
getchar();

if (newnode->id != 0)
{
printf("first input name:");
scanf("%s", newnode->name);
//*head = newnode->next;
}
(*head)->next=newnode;//newnode为第一个节点.
his=newnode;//his ------尾指针
while (newnode->id != 0)
{
/* if ((*head)->next == *head)
{
newnode = (*head)->next;
}
else
{
his->next = newnode;
}
his = newnode;*/
newnode = (struct cyclink *)malloc(sizeof(struct cyclink));
printf("more input num:");
scanf("%d", &newnode->id);
getchar();

if (newnode->id != 0)
{
printf("more input name:");
scanf("%s", newnode->name);
//*head = newnode->next;

his->next=newnode;
his=newnode;//his 后移
}

}
free (newnode);//释放最后一个节点
his->next=*head;//与head相连,形成单循环链表

return *head;
}

void print(struct cyclink *head)
{
struct cyclink *temp;
temp = head->next;//head->next;

if (head == head->next)
{
printf("no node!\n");
}
else
{
while (temp->next != head)//head
{
printf("id: %d\n", temp->id);
printf("name: %s\n", temp->name);
temp = temp->next;
}
printf("id: %d\n", temp->id);
printf("name: %s\n", temp->name);
}

}

int main()
{
struct cyclink *head;
head = creat(&head);
print(head);
exit(0);
}



jody_go 2011-04-06
  • 打赏
  • 举报
回复
哈哈,多谢提醒,搞定!
bEst_02 2011-04-06
  • 打赏
  • 举报
回复
几个NEXT的赋值方向反了吧。

69,371

社区成员

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

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