StaticLinkList(静态链表)
写这个写了几次,然后都没写完就关掉了,所以也不想多码字了,直接上代码吧(本来还认真自制了一张图片来理解静态链表的cursor与sub之间的关系)但其实也就那么回事:通过游标来找下标通过下标找到对应的数据。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> #define MAXSIZE 100 #define TRUE 1
#define FALSE 0 typedef int Status;
typedef int Element; typedef struct {
Element data;
int cursor;
/* 游标的作用是用来寻找当前元素指向的下标的位置的元素 */
}SqList, staticLinkList[MAXSIZE]; /* 声明所有函数 */
Status InitList();
int GetLength();
int Malloc();
int GetElem();
Status InsertNode();
void Free();
Status DeleteNode(); /* 初始化链表中的cursor */
Status InitList(staticLinkList space) {
int i;
for (i = 0; i < MAXSIZE - 1; i++) space[i].cursor = i + 1;
space[MAXSIZE - 1].cursor = 0;
return TRUE;
} /* 获取链表长度 */
int GetLength(staticLinkList space) {
int j = 0;
int i = MAXSIZE - 1;
while (space[i].cursor) {
i = space[i].cursor;
j++;
}
return j;
} int Malloc(staticLinkList space) {
int i = space[0].cursor;
if (space[0].cursor) space[0].cursor = space[i].cursor;
return i;
} /* 读取当前位置的元素 */
int GetElem(staticLinkList space, int i) {
int j = 1, k = space[MAXSIZE - 1].cursor;
Element receData;
if (i < 1 || i > GetLength(space)) return 0;
while (k && j < i) {
k = space[k].cursor;
j++;
}
receData = space[k].data;
return receData;
} /* 数据插入 */
Status InsertNode(staticLinkList space, int i, Element receData) {
int j, k, l;
k = MAXSIZE - 1;
if (i < 1 || i > GetLength(space)) return FALSE;
j = Malloc(space);
if (j) {
space[j].data = receData;
for (l = 1; l < i; l++) k = space[k].cursor;
space[j].cursor = space[k].cursor;
space[k].cursor = j;
return TRUE;
}
return 0;
} void Free(staticLinkList space, int i) {
space[i].cursor = space[0].cursor;
space[0].cursor = i;
} /* 数据删除 */
Status DeleteNode(staticLinkList space, int i) {
int j, k;
if (i < 1 || i > GetLength(space)) return FALSE;
k = MAXSIZE - 1;
for (j = 1; j < i; j++) k = space[k].cursor;
j = space[k].cursor;
space[k].cursor = space[j].cursor;
Free(space, j);
return TRUE;
} int main() {
char c;
int n, Pos, receData;
/* receData用来接收数据、Pos是位置 */
Element receElem;
staticLinkList space; InitList(space); //初始化列表游标
printf("请问您要建立一个多大的表格?\n");
scanf("%d", &n); int k = MAXSIZE - 1;
space[k].cursor = 1;
/** 从下标为1的位置开始赋值 */
for (int i = 0; i < n; i++) {
k = space[k].cursor;
space[k].data = i + 1;
}
space[0].cursor = space[k].cursor;
/** 输入完毕后将当前下标 k 对应的游标赋给下标0的游标 */
space[k].cursor = 0;
/**再将当前下标 k 的游标变为0 (即将最后输入的数据的游标变为0,
* 也就是说最后一个数据的后面指向第一个备用空间NULL,
* 而备用空间的下一个数据是备用空间的游标指向的最后一个数据的后一位(NULL))
*/ printf("显示数据:\n");
k = MAXSIZE - 1;
space[k].cursor = 1;
for (int i = 0; i < n; i++) {
k = space[k].cursor;
printf("%d ", space[k].data);
} puts("按 1 可进行查询 按 2 可进行数据插入");
puts("按 3 可进行数据删除 按 q 退出程序");
while ((c = getch()) != 'q') {
int j;
if (c == '1') {
printf("请输入要查询的数据位置:");
scanf("%d", &Pos);
receData = GetElem(space, Pos);
printf("%d\n", receData);
}
if (c == '2') {
printf("请输入要插入数据和位置:");
scanf("%d,%d", &receElem, &Pos);
InsertNode(space, Pos, receElem);
j = MAXSIZE - 1;
space[j].cursor = 1;
while (space[j].cursor) {
j = space[j].cursor;
printf("%d ", space[j].data);
}
putchar('\n');
}
if (c == '3') {
printf("请输入要删除数据的位置:");
scanf("%d", &Pos);
DeleteNode(space, Pos);
j = MAXSIZE - 1;
space[j].cursor = 1;
while (space[j].cursor) {
j = space[j].cursor;
printf("%d ", space[j].data);
}
putchar('\n');
}
} return 0;
}
StaticLinkList(静态链表)的更多相关文章
- 静态链表 C语言描述
静态链表1.下标为0的游标存放最后存放数据节点的游标,即是第一个没有存放元素(备用链表)的下标2.最后一个的节点存放第一个由数值得下标3.第一个和最后一个都不存放数据 即是备用链表的第一个的下标 4. ...
- 03静态链表_StaticLinkList--(线性表)
#include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...
- 静态链表C语言数据结构
静态链表就是将数组实现单链表: int Malloc_SLL(StaticLinkList space) { int i = space[0].cur;//取得第一个头节点的下标 if( space[ ...
- 【Java】 大话数据结构(3) 线性表之静态链表
本文根据<大话数据结构>一书,实现了Java版的静态链表. 用数组描述的链表,称为静态链表. 数组元素由两个数据域data和cur组成:data存放数据元素:cur相当于单链表中的next ...
- 【数据结构】单链表&&静态链表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...
- 静态链表-C语言实现
1.静态链表是在没有指针的编程语言里对链表的一种实现2.主要是用数组模拟指针3.在这里,使用结构体使数组的每一个空间可以存储一个数据元素(date)和一个游标(cur),游标的作用相当于链表的指针域, ...
- c数据结构链式存储-静态链表
#include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...
- 静态链表 Static Link List
Static Link List 静态链表 其中上图来自http://www.cnblogs.com/rookiefly/p/3447982.html 参考: http://www.cnblogs. ...
- 使用C语言描述静态链表和动态链表
静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...
随机推荐
- Python求1000以内所有3或5的倍数的和。
a=0 for n in range(1,1000): if n%3==0 or n%5==0: a=a+n print(a) 运行结果: 233168
- CSS input
去除激活 input 的默认边框 // 三种方法都能实现 input{ outline: none; outline: medium; outline:; } 修改光标颜色 input{ outl ...
- 最详细的linux安装php过程
本文主要和大家分享最详细的linux安装php过程,然后写好了nginx的安装配置,后面就是php的安装和mysql的安装,不过时间有限,而且放篇里也太长,所以都是分开来写,php安装完毕后就是mys ...
- AVL树的详细实现
[原文:https://cloud.tencent.com/developer/article/1155143] AVL树简介 AVL树的名字来源于它的发明作者G.M. Adelson-Velsky ...
- itest(爱测试) 4.3.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
4.3.0 发布后有三个用户强烈要求的更新,所以一周后4.3.1出炉,有点版本帝的味道哈,用户的反馈是我们持续升级的动力...... itest 简介:查看简介 test 开源敏捷测试管理,testO ...
- Lenet 神经网络-实现篇(2)
Lenet 神经网络在 Mnist 数据集上的实现,主要分为三个部分:前向传播过程(mnist_lenet5_forward.py).反向传播过程(mnist_lenet5_backword.py). ...
- 实现手写体 mnist 数据集的识别任务
实现手写体 mnist 数据集的识别任务,共分为三个模块文件,分别是描述网络结构的前向传播过程文件(mnist_forward.py). 描述网络参数优化方法的反向传播 过 程 文件 ( mnist_ ...
- 解释查询和本地查询 区分 Enumerable 和 Queryable
https://www.cnblogs.com/gosky/p/5757575.html 简单介绍:Enumerable 和 Queryable 他们都是静态类,位于命名控件 System.Linq下 ...
- [Python] CondaHTTPError: HTTP 000 CONNECTION FAILED for url
CondaHTTPError: HTTP 000 CONNECTION FAILED for url 遇到这个问题 解决方法如下两个 一.C:\Users\Administrator 目录下 编辑 . ...
- Django中csrf_token验证原理
我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...