可能是顺序表研究的细致了一点,单链表操作一下子就实现了。这里先实现带头结点的单链表操作。

大概有以下知识点.

1;结点:结点就是单链表中研究的数据元素,结点中存储数据的部分称为数据域,存储直接后继地址的部分称为指针域。

2;结点示意图:

3;头指针:头指针始终指向链表第一个元素,当有头结点时头结点就是链表第一个元素。头指针具有标识左右,故头指针命名为链表的名字,这里为linklist。头指针是一定存在的。

4;头结点:引入头结点的目的是,将链表首元结点的插入和删除操作与其他结点的插入和删除操作统一起来。(即头指针地址不在发生变化)

5;单链表结点结构体表示:

1 struct LNode
2 {
3 int data; //姑且认为其数据为整型
4 struct LNode * next;
5 };
6
7 typedef struct LNode * linklist

6;单链表的操作集合,头文件 defs.h

 1 #ifndef _DEFS_H_
2 #define _DEFS_H_
3
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include<malloc.h>
7
8 struct LNode //单链表结点的定义
9 {
10 int data;
11 struct LNode * next;
12 }
13 typedef struct LNode * linklist
14
15 //操作集合
16 void InitList(linklist *L); //申请头结点,头指针地址改变
17 void DestroyList(linklist *L); //须释放头结点,头指针地址改变
18 void ClearList(linklist L); //保留头结点,头指针地址不变
19 void ListEmpty(linklist L);
20 int ListLength(linklist L);
21 int GetElem(linklist L, int i, int *e);
22 int LocateElem(linklist L, int e);
23 int PriorElem(linklist L, int cur_e, int *pri_e);
24 int NextElem(linklist L, int cur_e, int *nex_e);
25 int ListInsert(linklist L, int i, int e); //插入不改变头指针的值
26 int ListDelete(linklist L, int i, int *e); //删除操作也不改变头指针的值
27 void TravelList(linklist L);
28 #endif

7;InitList操作实现

 1 #include"defs.h"
2
3 void InitList(linklist *L) //接受头指针的地址值
4 {
5 *L = (linklist)malloc(sizeof(struct LNode)); //*L表示头指针
6
7 if (*L == NULL)
8 {
9 printf("分配结点失败。\n");
10 exit(-1);
11 }
12 (*L)->next = NULL; //置头结点的next域为空
13 }

InitList.c

8;DestroyList操作的实现

 1 #include"defs.h"
2
3 void DestroyList(linklist *L) //接受的参数为头指针的地址值
4 {
5 linklist p ;
6 while (*L)
7 {
8 p = (*L)->next; //释放结点之前先保存结点的下一个地址,防止线索被断
9 free(*L);
10 *L = p; //将下一个结点,作为释放结点
11 }
12 }

DestroyList.c

9;ClearList操作的实现

 1 #include"defs.h"
2
3 void ClearList(linklist L) //保留头结点
4 {
5 linklist p = L->next; //p将头指针的next值保存起来,p指向首元结点
6 L->next = NULL;
7
8 DestroyList(&p); //调用撤销函数,释放后面的结点
9
10 }

ClearList.c

10;ListEmpty操作的实现

1 #include"defs.h"
2
3 void ListEmpty(linklist L)
4 {
5 if (L->next == NULL) //判断头结点指针域是否为空
6 printf("链表为空.\n");
7 else
8 printf("链表不为空.\n");
9 }

ListEmpty.c

11;ListLength操作实现

 1 #include"defs.h"
2
3 int ListLength(linklist L)
4 {
5 int j=0; //作为计数器
6 linklist p = L->next; //p指向首元结点,作为移动指针
7
8 while (p) //从首元结点开始
9 {
10 ++j;
11 p = p->next;
12 }
13 return j;
14 }

ListLength.c

12;GetElem操作实现

 1 #include"defs.h"
2
3 int GetElem(linklist L, int i, int *e) // 1<= i <=ListLength(L)
4 {
5 int j = 0; //j作为计数器
6 linklist p = L;
7
8 while (p && j<i) //找到第i个结点
9 {
10 ++j;
11 p = p->next;
12 }
13 if (!p || i<1) //取值位置不合理
14 exit(0);
15
16 *e = p->data;
17 return 0;
18 }

GetElem.c

13;LocateElem操作实现

 1 #include"defs.h"
2
3 int LocateElem(linklist L, int e)
4 {
5 linklist p = L->next; //p作为移动指针,p指向第一个元素
6 int j = 0; //j作为计数器
7
8 while (p)
9 {
10 ++j;
11 if (p->data == e)
12 return j;
13 p = p->next;
14 }
15 return 0;
16 }

LocateElem.c

14;PriorElem操作实现

 1 #include"defs.h"
2
3 int PriorElem(linklist L, int cur_e, int *pri_e) //第一个元素无前驱
4 {
5 linklist p = L->next; //p指向首元结点
6 linklist q;
7
8 while (p)
9 {
10 q = p->next; //q为p的后继
11
12 if (q && q->data == cur_e) //则p就是q的前驱
13 *pri_e = p->data;
14 p = q; //循环
15 }
16 return 0;
17 }

PriorElem.c

15;NextElem操作的实现

 1 #include"defs.h"
2
3 int NextElem(linklist L, int cur_e, int *nex_e) //最后一个元素无后继
4 {
5 linklist p = L->next; //p指向首元结点
6 linklist q;
7
8 while (p)
9 {
10 q = p->next; //q为p的后继
11
12 if (q && p->data == cur_e) //p有后继,且p的数据域与当前值相等
13 *nex_e = q->data;
14 p = q; //更新p
15 }
16 return 0;
17 }

NextElem.c

16;ListInsert操作的实现

 1 #include"defs.h"
2
3 int ListInsert(linklist L, int i, int e) //1<= i <= ListLength(L)
4 {
5 int j=0; //j为基数器
6 linklist p = L; //p为移动指针,p指向头结点
7 linklist q, s;
8
9 while (p && j<i-1) //找到第i-1个结点
10 {
11 ++j;
12 p = p->next;
13 }
14
15 if (!p || i<1) //插入位置不合理
16 return 0;
17
18 q = p->next; //q指向第i个结点
19
20 s = (linklist)malloc(sizeof(struct LNode));
21 if (!s) //分配失败
22 exit(-1);
23
24 s->data = e; //置s的数据域为e
25 s->next = q; //插入操作,是指针连起来
26 p->next = s;
27 return 0;
28 }

ListInsert.c

17;ListDelete操作的实现

 1 #include"defs.h"
2
3 int ListDelete(linklist L, int i, int *e) // 1<= i <=ListLength(L)
4 {
5 int j = 0; //j为计数器
6 linklist p = L;
7 linklist q;
8
9 while (p && j<i-1) //找到第i-1个结点
10 {
11 ++j;
12 p = p->next;
13 }
14
15 if (!p || i<1) //删除位置不合理
16 exit(0);
17 q = p->next; //q指向第i个结点
18
19 *e = q->data; //先将第i个结点的数据域带出
20 p->next = q->next; //连起指针
21 free(q); //释放第i个结点
22
23 return 0;
24 }

ListDelete.c

18;TravelList操作实现

 1 #include"defs.h"
2
3 void TravelList(linklist L)
4 {
5 linklist p = L->next; //p指向首元结点
6 int j = 0; //j为计数器
7
8 while (p)
9 {
10 ++j;
11 printf("第%d个结点的数据域的值为:%d\n", j, p->data);
12 p = p->next; //更新p
13 }
14 }

TravelList.c

19;makefile的实现,其代码与顺序表的一样,这里换成另一种写法

 1 object = main.o InitList.o DestroyList.o ClearList.o ListEmpty.o \
2 ListLength.o GetElem.o LocateElem.o PriorElem.o NextElem.o \
3 ListInsert.o ListDelete.o TravelList.o
4
5 test : $(object)
6 gcc -g -Wall -o test $(object)
7
8 $(object) : defs.h
9
10 .PHONY : clean
11 clean :
12 rm -f *.o

20;顺序表与单链表的比较

顺序表是用一段地址连续的存储单元来存放数据元素,适合查找,不适合频繁插入和删除(没一次操作都是O(n));

单链表是用不连续的存储单元,其数据元素之间是用指针来连接,每一次的查找都必须通过头指针来遍历,因此其不适合频繁的查找。但是对于插入和删除操作,它不需要移动其它元素,算法时间复杂度为O(1),因此特别适合频繁的插入和删除操作。

c语言实现--带头结点单链表操作的更多相关文章

  1. 数据结构之 线性表---单链表操作A (删除链表中的指定元素)

    数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...

  2. Java带头节点单链表的增删合并以及是否有环

    带头节点单链表 1.优势: 1)当链表为空时,指针指向头结点,不会发生null指针异常 2)方便特殊操作(删除第一个有效节点或者插入一个节点在表头) 3)单链表加上头结点之后,无论单链表是否为空,头指 ...

  3. 单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏

    数据结构上机测试2-2:单链表操作B TimeLimit: 1000ms Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除 ...

  4. c语言实现--不带头结点的单链表操作

    1,不带头结点的单链表操作中,除了InitList(),GetElem(),ListInsert(),ListDelete()操作与带头结点的单链表有差别外,其它的操作基本上一样. 2,不带头结点单链 ...

  5. C语言,单链表操作(增删改查)(version 0.1)

    这天要面试,提前把链表操作重新写了一遍.备份一下,以备不时之需. 希望有人能看到这篇代码,并指正. // File Name : list.h #include "stdafx.h" ...

  6. C单链表操作

    #include <stdio.h> #include <stdlib.h> #define ElemType int #define Status int #define O ...

  7. C++学习(三十五)(C语言部分)之 单链表

    单链表 就好比火车 火车头-->链表头部火车尾-->链表尾部火车厢-->链表的节点火车厢连接的部分-->指针火车中的内容-->链表节点的的数据链表节点包含数据域和指针域数 ...

  8. C语言版本:循环单链表的实现

    SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...

  9. C++ 单链表操作总结

    第一.单链表的定义和操作 #include <iostream> using namespace std; template <typename T> struct Node ...

随机推荐

  1. 基于vue2.x的webpack升级与项目搭建指南--基础篇

    first thing fitrst 博主声明:绝对不当标题党 有人看最好不过的背景: 十月初对公司产品的前端构建做了一些优化,但还遗留了不少问题(可了解我的前一篇博文:一次webpack小规模优化经 ...

  2. Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)

    概述 在Excel中,应用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种条件格式.本篇文章,将通过Java程序示例介绍条件格式的设置方法,设置条 ...

  3. Java内存模型与线程(二)线程的实现和线程的调度

    先行先发生原则(happen-before原则) 先行先发生是指Java内存模型中定义的两项操作之间的偏序关系. 如果说A先行于B,其实就是说在发生B操作之前,操作A产生的影响能被操作B观察到,至于这 ...

  4. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  5. python模块详解 | unittest(单元测试框架)(持续更新中)

    目录: why unittest? unittest的四个重要概念 加载测试用例的三个方法 自动加载测试用例 忽略测试和预期失败 生成html测试报告 why unittest? 简介: Unitte ...

  6. 使用Jenkins+Blue Ocean 持构建自动化部署之安卓源码打包、测试、邮件通知

    什么是BlueOcean? BlueOcean重新考虑了Jenkins的用户体验.BlueOcean由Jenkins Pipeline设计,但仍然兼容自由式工作,减少了团队成员的混乱,增加了清晰度. ...

  7. appium识别工具介绍

  8. WEB开发框架性能排行与趋势分析2-三大惊喜变化

    WEB开发框架性能排行与趋势分析2-三大惊喜变化 Web框架性能排名 上一次基于TechEmpower的<Web Framework Benchmarks>性能基准测试的解读之后,时隔两年 ...

  9. python—打开图像文件报错

    今天使用python打开一张图像文件的时候报错了 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illeg ...

  10. npm i 报错 'match' of undefined 错误以及删除node_modules失败

    简单粗暴的解决办法就是一个字'删', 1.先把node_modules给删了 手动删除的话,window系统经常会有部分删不了,说需要个权限什么的,直接用rimraf 就能解决 先安装npm inst ...