写这个写了几次,然后都没写完就关掉了,所以也不想多码字了,直接上代码吧(本来还认真自制了一张图片来理解静态链表的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(静态链表)的更多相关文章

  1. 静态链表 C语言描述

    静态链表1.下标为0的游标存放最后存放数据节点的游标,即是第一个没有存放元素(备用链表)的下标2.最后一个的节点存放第一个由数值得下标3.第一个和最后一个都不存放数据 即是备用链表的第一个的下标 4. ...

  2. 03静态链表_StaticLinkList--(线性表)

    #include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...

  3. 静态链表C语言数据结构

    静态链表就是将数组实现单链表: int Malloc_SLL(StaticLinkList space) { int i = space[0].cur;//取得第一个头节点的下标 if( space[ ...

  4. 【Java】 大话数据结构(3) 线性表之静态链表

    本文根据<大话数据结构>一书,实现了Java版的静态链表. 用数组描述的链表,称为静态链表. 数组元素由两个数据域data和cur组成:data存放数据元素:cur相当于单链表中的next ...

  5. 【数据结构】单链表&&静态链表详解和代码实例

    喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...

  6. 静态链表-C语言实现

    1.静态链表是在没有指针的编程语言里对链表的一种实现2.主要是用数组模拟指针3.在这里,使用结构体使数组的每一个空间可以存储一个数据元素(date)和一个游标(cur),游标的作用相当于链表的指针域, ...

  7. c数据结构链式存储-静态链表

    #include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...

  8. 静态链表 Static Link List

    Static Link List 静态链表 其中上图来自http://www.cnblogs.com/rookiefly/p/3447982.html  参考: http://www.cnblogs. ...

  9. 使用C语言描述静态链表和动态链表

    静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...

随机推荐

  1. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  2. IIS 无法读取配置节"system.web.extensions",因为它缺少节声明

    IIS 无法读取配置节"system.web.extensions",因为它缺少节声明 先安装ASP.NET 4.0  然后: 今天在本地安装iis,搭建网站,应用程序的时候报错下 ...

  3. win10编译OPenBlas

    之前没有编译过OpenBlas,今天试了一下. 与参考博客不同之处,我的系统是win10,opencOpenBlas版本0.2.14,Visual Studio版本15. 编译使用MSYS2安装min ...

  4. Git - 02. git 版本库简述: 类比平行宇宙

    1. 概述 简单描述 平行宇宙世界观 将 git 与 平行宇宙世界观 做一个类比, 方便理解 熟悉科幻, 或者具体点, 是 漫威宇宙 的朋友, 可以稍微轻松一点 这个是 第一次 重写后的版本. 代码在 ...

  5. bootstrap创建带遮罩层的进度条

    <div class="modal fade" id="loadingModal"> <div style="width: 200p ...

  6. .NET Core部署Windows服务

    创建 首先你要确保你已经安装了.NET Core 3.0或以上版本. 老版本的Windows服务请看 这篇文章 使用命令行创建:  dotnet new worker 使用Visual Studio创 ...

  7. RS422接口与RS485接口

    RS422具体接线参考网站 RS485接口 RS485设备为半双工设备,RS485收发器信号相关引脚包括控制引脚.485A.485B,其中控制引脚的高低电平决定当前处于接收模式还是发送模式. RS48 ...

  8. Leet Code 9.回文数

    判断一个整数是否是回文数. 题解 普通解法:将整数转为字符串,然后对字符串做判断. ///简单粗暴,看看就行 class Solution { public boolean isPalindrome( ...

  9. NLP开源工具

    最近有人问我几次NLP有哪些开源工具,这里做个笔记.

  10. 【MySQL】单表查询

    " 目录 where 约束 group by 分组查询 聚合函数 having 过滤 order by 查询排序 limit 限制查询的记录数 # 语法 select 字段1, 字段2 .. ...