生命不息,奮鬥不止。又到雙十一,別人都在掃貨,程序員還在改BUG~~~~~
先貼上別人講解的鏈接:單鏈表逆序
直接上程序,鏈表沒有註釋,只有在逆序的函數加了一下自己的理解。
list.h
/*************************************************************************
> File Name: list.h
> Author: bairutai
> Mail: [email protected]
> 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: [email protected]
> 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: [email protected]
> 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