改了改双向链表就行了,因为之前写为了AC,就没考虑那么多,其实头指针和尾指针都应该指向NULL的
加上之后就行了。
如果不用双向链表,改单向链表的话,只需要遍历,把next改成pre就行了,the same
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct node
{
int val;
struct node *next;
struct node *pre;
}Sqlist;
int n,k;
Sqlist *create()
{
Sqlist *l1,*l2,*temp;
l1 = (Sqlist *)malloc(sizeof(Sqlist));
scanf("%d",&l1->val);
l2 = l1;
l2->pre = NULL;
for(int i=1;i<n;++i)
{
l1 = (Sqlist *)malloc(sizeof(Sqlist));
scanf("%d",&l1->val);
l2->next = l1;
temp = l2;
l2 = l1;
l2->pre = temp;
}
l2->next = NULL;
return l2; //尾指针
}
int main()
{
while(scanf("%d",&n) != EOF)
{
if(n == 0)
{
printf("NULL\n");
continue;
}
Sqlist *list;
list = create();
while(list->pre != NULL)
{
printf("%d ",list->val);
list=list->pre;
}
printf("%d\n",list->val);
}
return 0;
}