数据结构_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) ...
随机推荐
- vue项目给less传递参数,出现 Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
vue-cli创建的项目,想使用less-loader的全局变量配置,但是配置(vue.config.js文件)项出现 error in ./src/components/BookFooter.vue ...
- java-file类 hei
File类 /* java.io.File类 文件和目录轮径的抽象形式 java把电脑种的文件和文件夹,封装为一个file类,我们可以使用file类对文件和文件夹进行曹祖 使用File类的方法 创建一 ...
- 是否可以从一个静态(static)方法内部发出对非静态 (non-static)方法的调用?
不可以,静态方法只能访问静态成员,因为非静态方法的调用要先创建对象,在 调用静态方法时可能对象并没有被初始化.
- State Lattice Planner(状态栅格规划)
参考文献: Efficient constrained path planning via search in state lattices Differentially Constrained Mo ...
- canvas小游戏——flappy bird
前言 如果说学编程就是学逻辑的话,那锻炼逻辑能力的最好方法就莫过于写游戏了.最近看了一位大神的fly bird小游戏,感觉很有帮助.于是为了寻求进一步的提高,我花了两天时间自己写了一个canvas版本 ...
- 微信小程序调研
小程序入口 微信发现,小程序 公众号主体查看小程序 好友分享,群分享 公众号自定义菜单跳转 APP页面跳转 第三方服务 附近的小程序 扫普通链接二维码打开小程序 需要后台开启功能,开启后,用户在微信& ...
- new String比字符串池浪费空间,为什么要用它?
对于下面程序中:ss0 = new String( "hello" );是用new()来新建对象的,存于堆中.每调用一次就会创建一个新的对象.当然从节省空间的角度来讲,肯定不如st ...
- Python:爬取中国各市的疫情数据并存储到数据库
import requests import pymysql import json def create(): # 连接数据库 db = pymysql.connect(host = 'localh ...
- 微信小程序列表拖动排序Demo
wxml页面编写 <view class="container"> <view bindtap="box" class="box&q ...
- 关于webpack,你想知道的都在这;
咱也标题党一回 哈哈哈 要使用webpack优化项目打包构建速度,首先得知道问题出在哪, 要知道问题出在哪,首先得知道webpack 打包的基本原理才能针对性的去做优化,下面首先了解webpack基本 ...