输入若干个不超过100的整数,建立单链表,然后将链表中所有结点的链接方向逆置,要求仍利用原表的存储空间。输出逆置后的单链表。
输入格式:
首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据在一行上输入数据个数n及n个不超过100的整数。
输出格式:
对于每组测试,输出逆置后的单链表,每两个数据之间留一个空格。
输入样例:
1
11 55 50 45 40 35 30 25 20 15 10 5
输出样例:
5 10 15 20 25 30 35 40 45 50 55
代码长度限制16 KB
时间限制400 ms
内存限制64 MB
代码如下:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
main(){
int T;
scanf("%d",&T);
while(T--){
struct node *head,*title,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
title=head;
int x;
scanf("%d",&x);
int y=x,number[y];
while(x--){
int num;
scanf("%d",&num);
p=(struct node *)malloc(sizeof(struct node));
p->data=num;
title->next=p;
title=p;
}
struct node *q=head->next;
int z=y-1;
while(q!=NULL){
number[z--]=q->data;
q=q->next;
}
for(int i=0;i<y;i++){
if(i!=0)
printf(" ");
printf("%d",number[i]);
}
printf("\n");
}
}