- #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()
- {
- PtrToNode head=NULL;
- PtrToNode list=NULL;
- int len;
- scanf("%d",&len);
- if(len==0)
- return NULL;
- int num;
- scanf("%d",&num);
- head=(PtrToNode)malloc(sizeof(struct Node));
- head->Data=num;
- head->Next=NULL;
- list=head;
- len--;
- while(len)
- {
- PtrToNode node=(PtrToNode)malloc(sizeof(struct Node));
- scanf("%d",&num);
- node->Data=num;
- node->Next=NULL;
- list->Next=node;
- list=node;
- len--;
- }
- return head;
- }
- void Print( List L )
- {
- if(L==NULL)
- return ;
- while(L!=NULL)
- {
- printf("%d ",L->Data);
- L=L->Next;
- }
- putchar('\n');
- }
- List Reverse( List L )
- {
- if(L==NULL)
- return NULL;
- PtrToNode l1=NULL;
- // PtrToNode l3=L;
- PtrToNode l2=NULL;
- while(L!=NULL)
- {
- l1=L->Next;
- L->Next=l2;
- l2=L;
- L=l1;
- }
- return l2;
笔记:typedef struct Node * PtrToNode
struct Node * = PtrToNode
PtrToNode List 相当于定义了一个 struct Node 类型的指针
1.对List read()解析
先要定义一个结构变量 赋值为NULL;
开辟一个结构空间 。
然后输入数据长度,判断是否为0 ,是则返回NULL
不是则继续输入数据,然后数据弄到结构里面去,记得Next为NULL;
那么要判断了如果有大于等于一个元素,就要在list里面继续添加下去,怎么做呢?
用循环来做啊
while(len) 在 里面开辟一个 PtrToNode 类型的变量 node 那么这个变量就带有 num 和 NULL
每次输入一个数 在把list 指向node 即可一个链表形成了;
OK,going on。
在重新写一个看看
2.对Print函数进行解析
你要输出 首先要看这个它有没有空?没有才可以输出的。
if(L==NULL)
3.Reverse 函数 首先判断空不 要写 if
(L==NULL)?
不空在进行下面的步骤。
设置一个临时储存的函数和一个储存逆反链表的链表。
现在coding
4.写一个完整的试一试;