生命不息,奋斗不止。又到双十一,别人都在扫货,程序员还在改BUG~~~~~
先贴上别人讲解的链接:单链表逆序
直接上程序,链表没有注释,只有在逆序的函数加了一下自己的理解。
list.h
/*************************************************************************
> File Name: list.h
> Author: bairutai
> Mail: bairutai@aliyun.com
> Created Time: 2015年11月10日 星期二 19时09分17秒
************************************************************************/
#ifndef LIST_H
#define LIST_H
#include<stdbool.h>
typedef int T;
typedef struct Node *PNode;
typedef struct Node {
T data;
PNode next;
}List;
PNode initList();
void destoryList(PNode*);
void insert(PNode head,PNode p,PNode s);
PNode makenode(T i);
PNode Reverse(PNode);
PNode Reverse2(PNode);
bool isEmpty(PNode);
#endif
list.c
/*************************************************************************
> File Name: list.c
> Author: bairutai
> Mail: bairutai@aliyun.com
> Created Time: 2015年11月10日 星期二 19时40分24秒
************************************************************************/
#include<stdio.h>
#include"list.h"
#include<malloc.h>
#include<stdlib.h>
PNode initList()
{
PNode node = (PNode)malloc(sizeof(List));
node->next = NULL;
return node;
}
void destoryList(PNode* list)
{
if(NULL == *list){
return;
}
PNode node = *list;
PNode tmp = NULL;
while(NULL != node)
{
tmp = node;
node->next = NULL;
free(node);
node = tmp->next;
}
*list = NULL;
}
bool isEmpty(PNode list)
{
if(NULL == list){
return true;
}
else {
return false;
}
}
void insert(PNode head,PNode p,PNode s)
{
if(NULL == head){
return;
}
if(p == head){
s->next = head->next;
head->next = s;
// head->data = s->data;
}
else{
s->next = p->next;
p->next = s;
}
}
PNode makenode(T i)
{
PNode p = (PNode)malloc(sizeof(List));
p->data = i;
p->next = NULL;
return p;
}
//非递归方式
//第一次,cur结点为空,next结点记录第二个结点,头结点断开,cur结点移到头结点的位置,头结点移到第二个结点位置
//第二次,cur结点为头结点,next结点记录第三个结点,第二个结点的next指针与头结点连接,
// cur结点移动到第二个结点位置,头结点移动到第三个结点位置
//然后重复
PNode Reverse(PNode list)
{
PNode next = NULL;
PNode cur = NULL;
while(NULL != list)
{
next = list->next;
list->next = cur;
cur = list;
list = next;
}
return cur;
}
//递归方式
//先递归找到最后一个结点,如果next指针为空,则是最后一个结点,返回新的头结点
//倒数第二次递归,将最后一个结点的next指针指向倒数第二个结点,并将倒数第二个结点的next指针断开(会在倒数第三次递归的时候指向倒数第三个结点).
PNode Reverse2(PNode list)
{
PNode newhead = NULL;
if(NULL == list || NULL == list->next){
return list;
}
newhead = Reverse2(list->next);
list->next->next = list;
list->next = NULL;
return newhead;
}
test.c
/*************************************************************************
> File Name: test.c
> Author: bairutai
> Mail: bairutai@aliyun.com
> Created Time: 2015年11月10日 星期二 20时20分37秒
************************************************************************/
#include<stdio.h>
#include"list.h"
main()
{
PNode list = initList();
PNode p1 = makenode(1);
PNode p2 = makenode(2);
PNode p3 = makenode(3);
PNode p4 = makenode(4);
insert(list,list,p1);
insert(list,p1,p2);
insert(list,p2,p3);
insert(list,p3,p4);
PNode p = list;
while(NULL != p){
printf("data is %d\n",p->data);
p = p->next;
}
PNode list2 = Reverse(list);
PNode p_ = list2;
while(NULL != p_){
printf("data2 is %d\n",p_->data);
p_ = p_->next;
}
PNode list3 = Reverse2(list2);
PNode p__ = list3;
while(NULL != p__){
printf("data3 is %d\n",p__->data);
p__ = p__->next;
}
destoryList(&list3);
if(isEmpty(list3)){
printf("list3 是个空链表\n");
}
else {
printf("list3 不是个空链表\n");
}
}
结果
data is 0
data is 1
data is 2
data is 3
data is 4
data2 is 4
data2 is 3
data2 is 2
data2 is 1
data2 is 0
data3 is 0
data3 is 1
data3 is 2
data3 is 3
data3 is 4
list3 是个空链表
makefile
OBJECT =list.o test.o
CC =gcc
CFLAG =-c
test:$(OBJECT)
$(CC) -o $@ $(OBJECT)
test.o:test.c list.c list.h
$(CC) $(CFLAG) -o $@ $<
list.o:list.c list.h
$(CC) $(CFLAG) -o $@ $<
clean:
rm $(OBJECT) -rf