数据结构_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) ...
随机推荐
- 启动一个线程是调用 run()还是 start()方法?
启动一个线程是调用 start()方法,使线程所代表的虚拟处理机处于可运行状态, 这意味着它可以由 JVM 调度并执行,这并不意味着线程就会立即运行.run()方 法是线程启动后要进行回调(callb ...
- centos云服务器配置redis外网可访问
找到这行,注释 bind 127.0.0.1 #bind 127.0.0.1 找到这行,修改 protected-mode yes protected-mode no 重启服务 ./redis-ser ...
- 11_二阶系统的单位阶跃响应_详细数学推导部分_2nd order system unit step response
- javascript入门教程(二):变量
大家好,我从今天开始就会正式讲javascript的语法方面.变量 js中的变量一般使用var来声明(es6的let不在本教程讨论范围内),可以用来定义任何种类的变量,如果只对变量进行了定义而没有赋值 ...
- vue-baidu-map 进入页面自动定位的解决方案!
写在前面:我只是一个前端小白,文章中的提到可能会有不足之处,仅提供一个参考.若有不完善的地方,欢迎各位大佬指出!,希望对你有帮助! 好了,入正题.其实之前也被这问题困扰过,在网上也查了一番,没找到解决 ...
- java中hashCode和equals什么关系,hashCode到底怎么用的
Object类的hashCode的用法:(新手一定要忽略本节,否则会很惨) 马 克-to-win:hashCode方法主要是Sun编写的一些数据结构比如Hashtable的hash算法中用到.因为ha ...
- git的下载安装以及基本操作
版权声明:本文为CSDN博主「~李疆」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.转载原文链接:https://blog.csdn.net/qq_403232 ...
- 使用Socket实现HttpServer(一)
使用Socket实现HttpServer(一) Socket 编程 socket 翻译过来叫插槽,一张图你就明白 socket 就插在 TCP 也就是传输层上,对用户的请求和服务器的响应进行处理. 下 ...
- python---输出函数运行时间的装饰器
""" 装饰器: 不改变原函数的调用方式和函数的前提下, 增加额外的功能, 其本质就是一个闭包 ---输出函数的运行时间 """ impor ...
- 微信小程序,制作属于自己的Icon图标
前言 最近在接手一个微信小程序,发现里面的图标都是使用的image组件,看起来非常别扭,加载也不太顺畅. 就想着看看微信有没有类似自带的图标库可以使用. 有是有,就是太少了,翻来翻去好像也就 8 种, ...