一、静态链表概念

用数组描述的链表,即称为静态链表。

在C语言中,静态链表的表现形式即为结构体数组,结构体变量包括数据域data和游标。

优点:

这种存储结构,仍需要预先分配一个较大的空间,但在作为线性表的插入和删除操作时不需移动元素,仅需修改指针,故仍具有链式存储结构的主要优点。

二、代码实现

1.构建结构体


typedef struct StaticLinkedNode{char data;int next;
} *NodePtr;typedef struct StaticLinkedList{NodePtr nodes;int* used;
} *ListPtr;

2.初始化结构体

ListPtr initLinkedList(){// The pointer to the whole list space.ListPtr tempPtr = (ListPtr)malloc(sizeof(StaticLinkedList));// Allocate total space.tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);// The first node is the header.tempPtr->nodes[0].data = '\0';tempPtr->nodes[0].next = -1;// Only the first node is used.tempPtr->used[0] = 1;for (int i = 1; i < DEFAULT_SIZE; i ++){tempPtr->used[i] = 0;}// Of for ireturn tempPtr;
}

3.打印结点

void printList(ListPtr paraListPtr){int p = 0;while (p != -1) {printf("%c", paraListPtr->nodes[p].data);p = paraListPtr->nodes[p].next;}// Of whileprintf("\r\n");
}

4.插入元素


void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){int p, q, i;// Step 1. Search to the position.p = 0;for (i = 0; i < paraPosition; i ++) {p = paraListPtr->nodes[p].next;if (p == -1) {printf("The position %d is beyond the scope of the list.\r\n", paraPosition);return;}// Of if} // Of for i// Step 2. Construct a new node.for (i = 1; i < DEFAULT_SIZE; i ++){if (paraListPtr->used[i] == 0){// This is identical to malloc.printf("Space at %d allocated.\r\n", i);paraListPtr->used[i] = 1;q = i;break;}// Of if}// Of for iif (i == DEFAULT_SIZE){printf("No space.\r\n");return;}// Of ifparaListPtr->nodes[q].data = paraChar;// Step 3. Now link.printf("linking\r\n");paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;paraListPtr->nodes[p].next = q;
}

5.删除元素

void deleteElement(ListPtr paraListPtr, char paraChar){int p, q;p = 0;while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){p = paraListPtr->nodes[p].next;}// Of whileif (paraListPtr->nodes[p].next == -1) {printf("Cannot delete %c\r\n", paraChar);return;}// Of ifq = paraListPtr->nodes[p].next;paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;// This statement is identical to free(q)paraListPtr->used[q] = 0;
}// Of deleteElement/*** Unit test.*/
void appendInsertDeleteTest(){// Step 1. Initialize an empty list.ListPtr tempList = initLinkedList();printList(tempList);// Step 2. Add some characters.insertElement(tempList, 'H', 0);insertElement(tempList, 'e', 1);insertElement(tempList, 'l', 2);insertElement(tempList, 'l', 3);insertElement(tempList, 'o', 4);printList(tempList);// Step 3. Delete some characters (the first occurrence).printf("Deleting 'e'.\r\n");deleteElement(tempList, 'e');printf("Deleting 'a'.\r\n");deleteElement(tempList, 'a');printf("Deleting 'o'.\r\n");deleteElement(tempList, 'o');printList(tempList);insertElement(tempList, 'x', 1);printList(tempList);
}

6.原代码展示

#include <stdio.h>
#include <malloc.h>#define DEFAULT_SIZE 5typedef struct StaticLinkedNode{char data;int next;
} *NodePtr;typedef struct StaticLinkedList{NodePtr nodes;int* used;
} *ListPtr;/*** Initialize the list with a header.* @return The pointer to the header.*/
ListPtr initLinkedList(){// The pointer to the whole list space.ListPtr tempPtr = (ListPtr)malloc(sizeof(StaticLinkedList));// Allocate total space.tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);// The first node is the header.tempPtr->nodes[0].data = '\0';tempPtr->nodes[0].next = -1;// Only the first node is used.tempPtr->used[0] = 1;for (int i = 1; i < DEFAULT_SIZE; i ++){tempPtr->used[i] = 0;}// Of for ireturn tempPtr;
}// Of initLinkedList/*** Print the list.* @param paraListPtr The pointer to the list.*/
void printList(ListPtr paraListPtr){int p = 0;while (p != -1) {printf("%c", paraListPtr->nodes[p].data);p = paraListPtr->nodes[p].next;}// Of whileprintf("\r\n");
}// Of printList/*** Insert an element to the given position.* @param paraListPtr The position of the list.* @param paraChar The given char.* @param paraPosition The given position.*/
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){int p, q, i;// Step 1. Search to the position.p = 0;for (i = 0; i < paraPosition; i ++) {p = paraListPtr->nodes[p].next;if (p == -1) {printf("The position %d is beyond the scope of the list.\r\n", paraPosition);return;}// Of if} // Of for i// Step 2. Construct a new node.for (i = 1; i < DEFAULT_SIZE; i ++){if (paraListPtr->used[i] == 0){// This is identical to malloc.printf("Space at %d allocated.\r\n", i);paraListPtr->used[i] = 1;q = i;break;}// Of if}// Of for iif (i == DEFAULT_SIZE){printf("No space.\r\n");return;}// Of ifparaListPtr->nodes[q].data = paraChar;// Step 3. Now link.printf("linking\r\n");paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;paraListPtr->nodes[p].next = q;
}// Of insertElement/*** Delete an element from the list.* @param paraHeader The header of the list.* @param paraChar The given char.*/
void deleteElement(ListPtr paraListPtr, char paraChar){int p, q;p = 0;while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){p = paraListPtr->nodes[p].next;}// Of whileif (paraListPtr->nodes[p].next == -1) {printf("Cannot delete %c\r\n", paraChar);return;}// Of ifq = paraListPtr->nodes[p].next;paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;// This statement is identical to free(q)paraListPtr->used[q] = 0;
}// Of deleteElement/*** Unit test.*/
void appendInsertDeleteTest(){// Step 1. Initialize an empty list.ListPtr tempList = initLinkedList();printList(tempList);// Step 2. Add some characters.insertElement(tempList, 'H', 0);insertElement(tempList, 'e', 1);insertElement(tempList, 'l', 2);insertElement(tempList, 'l', 3);insertElement(tempList, 'o', 4);printList(tempList);// Step 3. Delete some characters (the first occurrence).printf("Deleting 'e'.\r\n");deleteElement(tempList, 'e');printf("Deleting 'a'.\r\n");deleteElement(tempList, 'a');printf("Deleting 'o'.\r\n");deleteElement(tempList, 'o');printList(tempList);insertElement(tempList, 'x', 1);printList(tempList);
}// Of appendInsertDeleteTest/*** The entrance.*/
void main(){appendInsertDeleteTest();
}// Of main

C语言数据结构学习(4):静态链表相关推荐

  1. C语言数据结构学习用单元测试

    药物名称: C语言数据结构学习用单元测试 主治: 本品是基于CUnit编写的.全部的.纯C的数据结构(ADT)各种实现的单元测试.主治基础不牢,水品增长缓慢.建议配合严蔚敏<数据结构>(C ...

  2. 《数据结构》c语言版学习笔记——单链表结构(线性表的链式存储结构Part1)

    线性表的链式存储结构 数据结构系列文章 第二章 单链表结构 文章目录 线性表的链式存储结构 前言 一.单链表的建立 代码 二.单链表的读取 代码 三.单链表的插入 代码 四.单链表的删除 代码 五.单 ...

  3. 【C语言X数据结构】用静态链表实现的多项式计算器,加减乘除求导求值,输入输出样样在行!(完整代码+注释)

    目录 实验要求 完整代码 逻辑设计 哈喽各位好,我是李博轩,一个刚转到计算机的大二学生.这个标题是随手打上去的,感觉还蛮顺口,就这样了. 这个学期在学[数据结构与算法],而这是我面对的第一个实验题.因 ...

  4. java静态链表_数据结构笔记:静态链表(C语言)

    void CreateList(StaticLinkList *P)//创建一个静态链表 { int i; for(i=0;i此时并没有已占用空间,所以第一个节点中的指针(cur)的值为1,也就是说空 ...

  5. C语言建立简单的静态链表

    学习次内容前需掌握结构体和指针的使用方法,此链表为最基础的静态链表. #include <stdio.h> struct Student{int num;float score;struc ...

  6. c语言数据结构学习心得——线性表

    线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...

  7. 数据结构04:静态链表

    摘要:事实上,静态链表可以让我们更好地理解空间分配机制.由我们对一片空间来进行分配管理,如我们在链表中就用到了int *used来检查这篇空间是否被占用,模拟操作系统是如何判断一片空间是否被占用. 再 ...

  8. 《数据结构》c语言版学习笔记——其他链表(线性表的链式存储结构Part2)

    线性表的链式存储结构 数据结构系列文章 第三章 循环链表.双向链表 文章目录 线性表的链式存储结构 前言 一.循环链表 (一)定义 (二)尾指针 二.双向链表 (一)定义 (二)代码 总结 前言 提示 ...

  9. 数据结构学习笔记之一 链表

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 原貼作者 ...

最新文章

  1. 如果面试官问你 JVM,额外回答逃逸分析技术会让你加分!
  2. 图灵访谈 | 鹅厂专家李成熙:做好规划,才能事半功倍
  3. 单臂路由的配置及应用:
  4. mysql查询语句习题._MySql数据库基本select查询语句练习题,初学者易懂。
  5. Java中常见数据结构Map之LinkedHashMap
  6. 安卓手机玩游戏卡顿怎么解决_手机卡顿怎么办?教你3个实用解决方法,两分钟解决卡顿难题!...
  7. 指针(pointers)和引用(references)区别
  8. Vue之Vue.set动态新增对象属性
  9. canvas填充规则
  10. 2020中国奢侈品消费者数字行为洞察报告
  11. 2019至今TIOBE编程语言排行榜Top 20,Python完胜C++了吗?
  12. C++之运算符重载(前置++和后置++)
  13. uni-app 实现微信授权登录
  14. 【LeetCode】剑指 Offer 54. 二叉搜索树的第k大节点
  15. linux怎么查看定时任务有没有运行,怎么看crontab定时任务是否执行
  16. [转载] python(numpy) 实现神经网络训练 (卷积 全连接 池化)
  17. 霍夫森林(Hough Forest)目标检测算法
  18. MVC4 code first 增加属性,对应自动修改列的方法笔记
  19. GoldenDict 上的那些精美版权词典(附下载地址)(英语、俄语、梵语、印地语)
  20. python--数据导入--read_excel

热门文章

  1. 数据存储(超全,超详细,零基础也都学的会)
  2. fastjson已经导入到项目中但还是提示:java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSON
  3. 《数据科学R语言实践:面向计算推理与问题求解的案例研究法》一一2.6 对个人跑步时间的变化进行建模...
  4. 小小的登陆包括哪些测试点
  5. 维纳滤波进行图像去抖动去模糊
  6. direcrx11 学习日记supersampling (超采样抗锯齿)和(多重采样抗锯齿)的区别
  7. 精彩英语吵架100句
  8. 小白神器 - 一篇博客学会HTML
  9. Embedding 基础
  10. 浅谈配电室在线监控系统设计与工作原理