先针构造一个有n个元素组成的有序(按照元素数升序)单项链表,并将打印出来。

来源:百度知道 编辑:UC知道 时间:2024/09/27 21:18:27
先针构造一个有n个元素组成的有序(按照元素数升序)单项链表,并将打印出来。
然后对上述单项链表进行逆向(序)链接;如下
原来链接方式:
A ->B -> C
逆序后:
A <- B <- C
完成逆向(序)链接,输出;
要求:
1,考虑效率;
2,不使用原有链表节点以外的辅助空间;
3,考虑代码可读性

C语言做 别的方法也行

虽然是很久以前的帖子了,不过最近我也在做这个题,还是给你发一下吧,看到的话,回复一下吧
#include <stdio.h>
#include <stdlib.h>
struct stu
{
int x;
struct stu *next;
};
void main()
{
struct stu *head=NULL,*p,*q,*m;
for (int i=0;i<10;i++)
{
p=(struct stu *)malloc(sizeof(struct stu));
p->x=i;
if (head==NULL)
{
head=p;
q=p;
}
else
{
q->next=p;
q=p;
}
q->next=NULL;
}
p=head;
while (p!=NULL)
{
printf("%d\t",p->x);
p=p->next;
}
p=head;
q=p->next;
p->next=NULL;
while(q!=NULL)
{
m=q->next;
q->next=p;
p=q;
q=m;
}
while (p!=NULL)
{
printf("%d\t",p->x);
p=p->next;
}
}

要求:
1,考虑效率;
2,不使用原有链表节点以外的辅助空间;
3,考虑代码可读性
4,你给200分?