结构应用:逆序链表何错之有?

ScorpioZZR 2003-08-19 10:59:28
运行到后来好像进入死循环啦,错在哪里啊?
哪位大虾指点一下。给个正确的程序,指出错误的地方。

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

struct stu
{
int num;
struct stu *next;
}*p1,*p2;
struct stu *creat(void )
{
int temp;
struct stu *head=NULL;
printf("\ninput number:");
while(scanf("%d",&temp))
{
p1=(struct stu *)malloc(sizeof(struct stu));
if(head==NULL)
head=p1;
else
p2->next=p1;
p1->num=temp;
printf("Input number:");
p2=p1;
}
p2->next=NULL;
return head;
}

void output(struct stu *outhead)
{
for(p1=outhead;p1!=NULL;printf("%4d",p1->num),p1=p1->next);
}

struct stu *turnback(struct stu *head)
{
struct stu *myNew,* myNewhead=NULL;
do
{
p2=NULL;
p1=head;
while(p1->next!=NULL)p2=p1,p1=p1->next;
if(myNewhead==NULL)myNewhead=p1,myNew=myNewhead->next=p2;
myNew=myNew->next=p2;
}while(head->next!=NULL);
return myNewhead;
}

void main()
{
struct stu *head;
head=creat();
printf("\n\nthe original list:");
output(head);
head=turnback(head);
printf("\n\nThe myNew list:");
output(head);
}
--
...全文
24 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn5211 2003-08-19
  • 打赏
  • 举报
回复
while(scanf("%d",&temp))
{
p1=(struct stu *)malloc(sizeof(struct stu));
if(head==NULL)
head=p1;
else
p2->next=p1;
p1->num=temp;
printf("Input number:");
p2=p1;
}

没有循环结束的条件呀。
bm1408 2003-08-19
  • 打赏
  • 举报
回复
看看清华的那本练习册吧!

69,371

社区成员

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

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