01
程序文件
I、
在单链表/src目录下分别创建main.c、Linklist.c文件
//main.c
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建失败\n");
}
printf ("创建成功\n");
int i;
for (i = 0; i < 10; i++)
{
//头插
//Insert_Head(ls, i);
//尾插
Insert_Last(ls, i);
}
Display(ls);
printf ("在第1位置插入111:");
Insert_Pos(ls, 1, 111);
Display(ls);
printf ("在第6位置插入666:");
Insert_Pos(ls, 6, 666);
Display(ls);
printf ("删除第二个数:");
Delete_Pos(ls, 2);
Display(ls);
printf ("删除666:");
Delete_Num(ls, 666);
Display(ls);
Reverse(ls);
Display(ls);
Destory(ls);
return 0;
}
//Linklist.c
#include <stdio.h>
#include "LinkList.h"
#include <stdlib.h>
List *CreateList(void)
{
List *ls = (List*)malloc(sizeof(List)/sizeof(char));
if (NULL == ls)
{
//数据类型是指针类型,那我们一般成功的时候返回相应的地址,
//失败的时候返回NULL/用NULL‘清零’
/*
空指针:空指针是一个特殊的指针值,
也是唯一一个对任何指针类型都合法的指针值。
指针变量具有空指针值,表示它当时处于闲置状态,
没有指向有意义的东西。
C语言保证这个值不会是任何对象的地址。
给指针值赋零则使它不再指向任何有意义的东西。
*/
return NULL;
}
ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
if (NULL == ls->head)
{
free(ls);
return NULL;
}
//链表中有一个“头指针”变量,以“head”表示,
//它存放一个地址,该地址指向一个元素。
//链表中的每一个元素称为“结点”,
//每个结点都应包含两个部分。
ls->head->next = NULL;//空链表
return ls;
}
//头插
BOOL Insert_Head(List *ls, Data data)
{
if (NULL == ls)
{
return ERROR;
}
//为新插入的数据,分配空间。空间起始地址放入node中
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
{
return ERROR;
}
node->data = data;
//首次:把头结点中的指针变量的值(NULL)传入新结点
//再次:插入处结点后的那个结点的地址传入新插入的结点。。。
node->next = ls->head->next;
//首次:再把新分配空间的首地址,传入头结点。头插
ls->head->next = node;
return TRUE;
}
//尾插
BOOL Insert_Last(List *ls, Data data)
{
if (NULL == ls)
{
return ERROR;
}
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
{
return ERROR;
}
node->data = data;
node->next = NULL;
Node *tmp = ls->head;//头结点
//作表达式时,取指针变量中存放的地址值
while (tmp->next)
{
tmp = tmp->next;
}
//tmp->next相当于int *p = &a中的p
tmp->next = node;
return TRUE;
}
//指定位置插入
BOOL Insert_Pos(List *ls, Data pos, Data data)
{
if (NULL == ls)
{
return ERROR;
}
Node *tmp = ls->head;
int i;
//循环后tmp中存放的是要插入位置处结点的地址,
//或者说tmp代表的是要插入数之前的那个结点
for (i = 0; i < pos - 1; i++)
{
tmp = tmp->next;
if (NULL == tmp)
{
printf ("长度越界");
return ERROR;
}
}
//为新插入的数据,分配空间。空间起始地址放入node中
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
{
return ERROR;
}
node->data = data;
node->next = tmp->next;
tmp->next = node;
}
//指定位置删除
BOOL Delete_Pos(List *ls, Data pos)
{
if (NULL == ls)
{
return ERROR;
}
Node *tmp = ls->head;
int i;
//tmp代表的是要删除数之前的那个结点
for (i = 0; i < pos - 1; i++)
{
tmp = tmp->next;
//删除某个节点,自身要存在,下一节点也要存在
if (NULL == tmp && NULL == tmp->next)
{
printf ("长度越界");
return ERROR;
}
}
//为新插入的数据,分配空间。空间起始地址放入node中
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)//越界
{
return ERROR;
}
//tmp代表的是要删除数之前的那个结点
Node *p = tmp->next;
//p->next:引用址值
tmp->next = p->next;
free(p);
}
//指定值删除数据
BOOL Delete_Num(List *ls, Data data)
{
if (NULL == ls)
{
return ERROR;
}
Node *tmp = ls->head;
//取0x1000中存放的地址/取首结点的地址、或称取头结点中的地址值
//作表达式时,取指针变量中存放的地址值
//tmp = tmp->next;
while (tmp->next)
{
if (tmp->next->data == data)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
//链表逆序
BOOL Reverse(List *ls)
{
if (NULL == ls || NULL == ls->head || NULL == ls->head->next || NULL == ls->head->next->next)
{
return ERROR;
}
Node *pre = ls->head->next;// 第一个结点
Node *cur = pre->next;// 第二个结点
Node *tmp;// 保存当前结点的下一个结点
while (cur)
{
//左值代表地址
//右值代表内容
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
ls->head->next->next = NULL;
ls->head->next = pre;
return TRUE;
}
//打印
void Display(List *ls)
{
if (NULL == ls)
{
return;
}
Node *tmp = ls->head->next;//第一个结点
while (tmp)
{
printf ("%-4d", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
//销毁
void Destory(List *ls)
{
if (NULL == ls)
{
return;
}
Node *tmp = ls->head;
//取0x1000中存放的地址/取首结点的地址、或称取头结点中的地址值
while (tmp->next)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(ls->head);
free(ls);
}
II、
在单链表/seq/include目录下创建Linklist.h文件
//Linklist.c
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef enum {TRUE, FALSE, ERROR} BOOL;
typedef int Data;
//每一个结点中应包含一个指针变量,用它来存放下一结点的地址
typedef struct _node
{
//数据域
Data data;
//指针域
//next是指针变量,指向结构体变量/指向struct _node类型数据
//一个指针类型的成员既可以指向其它类型的结构体数据,也可以指向自己所在的结构体类型的数据
//next:是struct _node类型的一个成员,又指向struct _node类型的数据
struct _node *next;
}Node;
typedef struct _list
{
//头节点
Node *head;
}List;
//创建链表
List *CreateList(void);
//头插
BOOL Insert_Head(List *ls, Data data);
//尾插
BOOL Insert_Last(List *ls, Data data);
//指定位置插入
BOOL Insert_Pos(List *ls, Data pos, Data data);
//指定位置删除
BOOL Delete_Pos(List *ls, Data pos);
//指定值删除数据
BOOL Delete_Num(List *ls, Data data);
//链表逆序
printf ("逆序结果为:");
BOOL Reverse(List *ls);
//打印
void Display(List *ls);
//销毁
void Destory(List *ls);
#endif //_LINKLIST_H
III、
在“单链表”目录下创建Makefile文件
src1 = $(wildcard ./src/*.c)
obj1 = $(patsubst ./src/%.c, ./obj/%.o, $(src1))
target = ./bin/a.out
all:$(target)
$(target):$(obj1)
gcc $(^) -o $(@)
$(obj1):./obj/%.o:./src/%.c
gcc -c $(^) -I ./include -o $(@) -g
.PHONY:clean all
clean:
-rm -rf $(target) $(obj1
02
测试结果
root@lj:/单链表# ./bin/a.out
创建成功
0 1 2 3 4 5 6 7 8 9
在第1位置插入111:111 0 1 2 3 4 5 6 7 8 9
在第6位置插入666:111 0 1 2 3 666 4 5 6 7 8 9
删除第二个数:111 1 2 3 666 4 5 6 7 8 9
删除666:111 1 2 3 4 5 6 7 8 9
逆序结果为:9 8 7 6 5 4 3 2 1 111