#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;
List Read();
void Print(List L);
List Reverse(List L);
int main()
{
List L1, L2;
L1 = Read();
L2 = Reverse(L1);
Print(L1);
Print(L2);
return 0;
}
/* 建立链表 */
List Read()
{
List head = NULL;
List current;
List prev = NULL;
int len = 0;
scanf("%d", &len);
if (len == 0) return NULL;
while (len--)
{
current = (List)malloc(sizeof(struct Node));
if (head == NULL)
head = current;
else
prev->Next = current;
current->Next = NULL;
scanf("%d", ¤t->Data);
prev = current;
}
return head;
}
void Print(List L)
{
List p = L;
List s = L;
List temp;
if (p == NULL)
printf("NULL");
else
printf("\n");
while (p!=NULL) {
printf("%d ", p->Data);
p = p->Next;
}
}
List Reverse( List L )
{
List Temp, Prev;
Prev = NULL;
while(L)
{
Temp = L->Next;
L->Next = Prev;
Prev = L;
L = Temp;
}
return Prev;
}
【数据结构】单链表逆转
最新推荐文章于 2021-07-26 17:18:01 发布