c语言问题,高手进来看一下嘛!急!急!

哪位高手来改正一下这个程序!程序目的是把链表反序.#include<stdio.h>#include<conio.h>#include<malloc.h>#defineL... 哪位高手来改正一下这个程序!程序目的是把链表反序.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define RUN 1
struct student
{
long num;
int score;
struct student *next;
};
int n ;
struct student *creat(void)
{
void print(struct student *head);
struct student *head, *p1, *p2;
n = 0;
p2 = p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n += 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
}
p2->next = NULL;
#if RUN
print(head);
#endif
return (head);
}

void print(struct student *head)
{
struct student *p = head;
if (head != NULL)
do
{
printf("\t%ld\t%d\n", p->num, p->score);
p = p->next;
}while (p != NULL);
}

struct student *paixu(struct student *head)
{
struct student str[20];
struct student *p = str, *p1 = head;
while (p1->next != NULL)
{
p = p1;
p++;
p1 = p1->next;
}
p = p1;
head = p;
for( p1 = head; p != str ; p--)
{
p1->next = p - 1;
p1 = p1->next;
}
p1->next = p - 1;
p1 = p1->next;
p1->next = NULL;
#if RUN
print(head);
#endif
return (head);
}

main()
{
struct student *head;
head = creat();
print(paixu(head));
getch();
}
展开
 我来答
mjl86
2007-05-22 · TA获得超过344个赞
知道小有建树答主
回答量:399
采纳率:0%
帮助的人:423万
展开全部
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define RUN 1
struct student
{
long num;
int score;
struct student *next;
};
int n ;
struct student *creat(void)
{
void print(struct student *head);
struct student *head, *p1, *p2;
n = 0;
p2 = p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n += 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
}
p2->next = NULL;
print(head);
return head;

}

void print(struct student *head)
{
struct student *p = head;
if (head != NULL)
do
{
printf("\t%ld\t%d\n", p->num, p->score);
p = p->next;
}while (p != NULL);
}

struct student *paixu(struct student *head)
{
struct student *str[20];
struct student **p = str, *p1 = head;
while (p1->next != NULL)
{
*p = p1;
p++;
p1 = p1->next;
}

head = p1;
for( p1 = head; p != str ; p--)
{
p1->next = *(p - 1);
p1 = p1->next;
}
p1->next = NULL;

print(head);
return head;

}

main()
{
struct student *head;
head=creat();
paixu(head);
getch();
}
//说真的,这种算法不是很好!!
//反序嘛,用原来地址更好啊^^^
鹤小易1M
2007-05-21 · 超过21用户采纳过TA的回答
知道小有建树答主
回答量:64
采纳率:0%
帮助的人:64.5万
展开全部
懒得看了,不过我有链表反序的源代码,因为不久前的上机课做过,你要的话可以跟我联系,或者去
http://my.mofile.com/alphalwei里的数据结构文件夹中下载,里面的链表操作包含里所需要的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hivxue
2007-05-22
知道答主
回答量:3
采纳率:0%
帮助的人:0
展开全部
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define LEN sizeof(struct student)
#define RUN 1
struct student
{
long num;
int score;
struct student *next;
};
int n ;
struct student *creat(void)
{
void print(struct student *head);
struct student *head, *p1, *p2;
n = 0;
p2 = p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n += 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld%d", &p1->num, &p1->score);
}
p2->next = NULL;
#if RUN
print(head);
#endif
return (head);
}

void print(struct student *head)
{
struct student *p = head;
if (head != NULL)
do
{
printf("\t%ld\t%d\n", p->num, p->score);
p = p->next;
}while (p != NULL);
}

struct student *convert(struct student *head)
{
struct student *f,*m,*p0,*p1;
if(head!=NULL)
{
p0=head;
f=p0->next;
p0=f;
head->next=NULL;
while(f->next!=NULL)
{
while(p0->next!=NULL)
{
m=p0;
p0=p0->next;
}
m->next=NULL;
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p0;
p0->next=NULL;
p0=f;
}
while(p1->next!=NULL)
p1=p1->next;
p1->next=f;
f->next=NULL;
p0=head;
p1=head->next;
head=p1;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p0;
p0->next=NULL;

}

return (head);

}

main()
{
struct student *head;
head = creat();
printf("the convert sort is :\n");
print(convert(head));
getch();
}
这个是用一个链表的变换,
改的有些仓促 可以完成任务 但算法有点烦琐 应该可以改进
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式