#include<stdio.h>
#include<malloc.h>
typedef struct Node
{
int data;
struct Node * next;
}LNode,*LinkedList;
LinkedList L;
LinkedList LinkedListInit(LinkedList L)
{// init linkedlist
int x;
LNode *p;
LinkedList r;
L=(LNode*)malloc(sizeof(LNode));
L->next = NULL;
r = L;
scanf("%d",&x);
while(x!=0)
{ //帶頭結點的尾插法
p=(LNode*)malloc(sizeof(LNode));
p->data = x;
p->next = r->next;
r->next = p;
r = p;
scanf("%d",&x);
}
if(r!=NULL) r->next = NULL;
return L;
}
LinkedList reverse(LinkedList L) //要用到引用&,才能改變該鏈表
{
LNode *p1,*p2,*temp;
p1=L->next;
p2=p1->next;
p1->next=NULL;
while(p2!=NULL)
{
temp=p2->next;
p2->next=p1;
p1=p2;
p2=temp;
}
L->next=p1;
return L;
}
void travel(LinkedList L)
{// Output single-linked list L of the node element values
LinkedList p;
p=L->next;
while(p)
{
printf("%d ",p->data);
p= p->next;
}
}
void main(void)
{
L = LinkedListInit(L);
L = reverse(L);
travel(L);
}