数据结构_C语言_单链表
# include <stdio.h>
# include <stdbool.h>
# include <malloc.h> typedef int DataType; typedef struct Node {
DataType data;
struct Node * next;
} Node; /* 需要完成的所有基本操作 */
void InitList_Head(Node **);
void InitList_Tail(Node **);
int Length(Node *);
int LocateElem(Node *, DataType);
DataType GetElem(Node *, int);
bool ListInsert(Node *, int, DataType);
bool ListDelete(Node *, int, DataType *);
void PrintList(Node *);
bool Empty(Node *);
void DestroyList(Node *); int main(void) { printf("-insert head-\n");
Node * pHead1;
InitList_Head(&pHead1);
PrintList(pHead1);
printf("\n"); printf("-insert tail-\n");
Node * pHead2;
InitList_Tail(&pHead2);
PrintList(pHead2);
printf("size? %d\n", Length(pHead2));
printf("locate 2? %d\n", LocateElem(pHead2, 6));
printf("GetElem loc 4? %d\n", GetElem(pHead2, 4));
printf("\n"); printf("-insert elem-\n");
ListInsert(pHead2, 4, 0);
PrintList(pHead2);
printf("\n"); printf("-delete elem-\n");
int e;
ListDelete(pHead2, 4, &e);
printf("delete elem? %d\n", e);
PrintList(pHead2);
printf("\n"); printf("empty? %d\n", Empty(pHead2));
printf("\n"); printf("-delete elem-\n");
DestroyList(pHead2);
printf("empty? %d\n", Empty(pHead2));
PrintList(pHead2); return 0;
} void InitList_Head(Node ** ppHead) {
/*
1. 建立头结点
2. 循环插入 (头插法)
*/ Node * pHead = (Node *)malloc(sizeof(Node));
/*
此时,pHead中是头结点的地址
*/
pHead->next = NULL;
pHead->data = 9999; // for test int len = 5;
Node * pNew;
for (int i = 0; i < len; i++) {
pNew = (Node *)malloc(sizeof(Node));
pNew->data = i + 1;
pNew->next = pHead->next;
pHead->next = pNew;
} *ppHead = pHead;
return;
} void PrintList(Node * pHead) {
Node * p = pHead->next;
int i = 0; // just for test
/*
注意!!!
pHead存的是头结点的地址 但,
p存的不是pHead存的地址 --> 即头结点的地址
p存的是pHead指向的那个单元中next存的地址 --> 即第一个单元的地址
*/ printf("List's info:\n");
while ((p != NULL) && (i <= 10)) {
printf("data=%d, next=%d\n", p->data, p->next);
p = p->next; i++;
} return;
} void InitList_Tail(Node ** ppHead) {
Node * pHead = (Node *)malloc(sizeof(Node));
pHead->next = NULL;
pHead->data = 999; // just for test int len = 5;
Node * pNew;
Node * pTail = pHead;
for (int i = 0; i < len; i++) {
pNew = (Node *)malloc(sizeof(Node));
pNew->data = i + 10;
pNew->next = NULL; pTail->next = pNew;
pTail = pNew;
} *ppHead = pHead;
return;
} int Length(Node * pHead) {
int len = 0;
Node * p = pHead->next; while (p != NULL) {
len++;
p = p->next;
} return len;
} int LocateElem(Node * pHead, DataType d) {
int loc = 0;
Node * p = pHead->next; while (p != NULL && p->data != d) {
/*
四种情况:
p == NULL && p->data == d --> 跳出循环 --> 尾结点是目标点
p == NULL && p->data != d --> 跳出循环 --> 没有目标点
p != NULL && p->data == d --> 跳出循环 --> 尾结点前是目标点
p != NULL && p->data != d --> 继续循环
*/
p = p->next;
loc++;
} if (p == NULL || p->data != d) {
/*
判定条件不能只写(p->data != d)
因为,若此时p是NULL,则不能访问p->data
*/
return -1; // 没有这个点
} return loc;
} DataType GetElem(Node * pHead, int pos) {
Node * p = pHead->next;
int i = 1; if (pos <= 0) return -888; while(p != NULL) { if (i == pos) {
return p->data;
} p = p->next;
i++; } return -999;
} bool ListInsert(Node * pHead, int pos, DataType d) {
if (pos <= 0) return false; Node * p = pHead->next;
Node * pPrior = pHead;
int i = 1; while(p != NULL) {
if (i == pos) {
Node * pNew = (Node * )malloc(sizeof(Node));
pNew->data = d;
pNew->next = p;
pPrior->next = pNew; return true;
} p = p->next;
pPrior = pPrior->next;
i++;
} return false;
} bool ListDelete(Node * pHead, int pos, DataType * d) {
if (pos <= 0) return false; Node * p = pHead->next;
Node * pPrior = pHead;
int i = 1; while (p != NULL) {
if (i == pos) {
*d = p->data;
pPrior->next = p->next; free(p);
p=NULL; return true;
} p = p->next;
pPrior = pPrior->next;
i++;
} return false;
} bool Empty(Node * pHead) {
if (pHead->next == NULL) return true; return false;
} void DestroyList(Node * pHead) {
pHead->next = NULL;
}
数据结构_C语言_单链表的更多相关文章
- 数据结构C语言版--单链表的基本功能实现
/* * 构造一个链式存储的线性表(当输入9999时,结束构造过程),然后输出该线性表 * 并统计该线性链表的长度 . *注:new和delete是C++的运算符 malloc和free是C++/C的 ...
- 数据结构_C语言_二叉树先序、中序、后序遍历
# include <stdio.h> # include <stdlib.h> typedef struct BiTreeNode { char data; struct B ...
- C语言实现单链表-03版
在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...
- C/C++语言实现单链表(带头结点)
彻底理解链表中为何使用二级指针或者一级指针的引用 数据结构之链表-链表实现及常用操作(C++篇) C语言实现单链表,主要功能为空链表创建,链表初始化(头插法),链表元素读取,按位置插入,(有序链表)按 ...
- C语言实现单链表-02版
我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- 选择排序_C语言_数组
选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...
- 插入排序_C语言_数组
插入排序_C语言_数组 #include <stdio.h> void insertSort(int *); int main(int argc, const char * argv[]) ...
- 快速排序_C语言_数组
快速排序_C语言_数组 #include <stdio.h> void quickSort(int *, int, int); int searchPos(int *, int, int) ...
随机推荐
- 如何通过HibernateDaoSupport将Spring和Hibernate 结合起来?
用 Spring 的 SessionFactory 调用 LocalSessionFactory.集成过程分三步: 配置 the Hibernate SessionFactory. 继承 Hibern ...
- memcached 最大能存储多大的单个 item?
1MB.如果你的数据大于 1MB,可以考虑在客户端压缩或拆分到多个 key 中. 为什么单个 item 的大小被限制在 1M byte 之内? 简单的回答:因为内存分配器的算法就是这样的. 详细的回答 ...
- spring-boot 注解集合
@Configuration 用于定义配置类,可替换XML配置文件,被注解的类内部包含一个或多个@Bean注解方法.可以被AnnotationConfigApplicationContext或者Ann ...
- 给定一个奇数n,比如n=3,生成1到n平方的数,如1到9,填入九宫格,使得横竖斜的和都相等。
对于N阶幻方,从1开始把数字从小到大按以下规则依次写入: 一.在第一行中间一列写入1 二.依次向右上方写入2.3.4...... 三.如果某数字写在了表格的某个方向外面,那就把这个数字向相反方向移动N ...
- 什么是 UML?
UML 是统一建模语言(Unified Modeling Language)的缩写,它发表于 1997 年,综合了当时已经存在的面向对象的建模语言.方法和过程,是一个支持模型 化和软件系统开发的图形化 ...
- websocket 实现简单网页版wechat
1.群聊 web - socket--基于TCP/UDP http - 无状态的短链接 长连接:客户端和服务器保持永久性的链接,除非有一方主动断开, 轮询:客户端和服务端不断连接,然后断开,请求响应; ...
- Linux 安装jdk1.8
Linux安装jdk1.8 总结一句话就是:下载jdk1.8 ==> 解压 ==> 配置环境变量. 一. jdk的下载,这里有两种方法: 1.去Oracle官网下载. 2.jdk1.8的 ...
- matlab拟合函数的三种方法
方法一:多项式拟合polyfit 1 x=[1 2 3 4 5 6 7 8 9]; 2 3 y=[9 7 6 3 -1 2 5 7 20]; 4 P= polyfit(x, y, 3) %三阶多项式拟 ...
- 9_根轨迹_Part3_分离点/汇合点和根的性质
- java中输出一个字符串里面的空格,字母还有数字的数目举例说明
9.6 About string,"I am a teacher",这个字符串中有多少个字,且分别把每个字打印出来. /*本题的思路就是,当我有一个字符串,我需要一个一个字符的处理 ...