写这个写了几次,然后都没写完就关掉了,所以也不想多码字了,直接上代码吧(本来还认真自制了一张图片来理解静态链表的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. php操作shee学习笔记之(一)PHP操作shell函数

    一.php操作shell 1.system函数:执行普通命令 string system (string $command [,int &$return_var]) 1)$command是命令 ...

  2. Python(二):做题函数记录

    一,10进制 转 2,8,16进制 bin(<int>) ,oct(<int>),hex(<int>) 输出示例 '0b10011010010' '0o2322' ...

  3. 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)

    题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...

  4. 每日扫盲(四):java之Netty原理和使用

    转自:https://www.jdon.com/concurrent/netty.html Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Nett ...

  5. Wx-小程序-长按复制文本

    view: <text bindlongtap='copy' data-name='{{name}}'>{{item.name}}</text> js: copy(e) { v ...

  6. git查漏补缺

    1. commit提交注释规范 2. commit 注释没写完或写错了,在不用删除这条commit的情况下,如何更正注释信息 git commit -m '1' git commit  --amend ...

  7. opencv:opencv概述

    opencv官方:www.opencv.org github:https://github.com/opencv OpenCV OpenCV是一个开放源代码的计算机视觉应用平台,由英特尔公司研发中心俄 ...

  8. numpy函数hstack,vstack,dstack简介

    vstack.hstack和dstack都用于把几个小数组合并成一个大数组.它们的差别是小数组的元素在大数组中的排列顺序有所不同.把两部手机摆到一起有几种方式?水平的左右排列,垂直的上下排列,还可以把 ...

  9. appium常用的类库、对应的方法和属性

    1.driver.swipe(self, start_x, start_y, end_x, end_y, duration=None) tart_x - 滑动开始x轴坐标 start_y - 滑动开始 ...

  10. TensorFlow 模型的保存与载入

    参考学习博客: # https://www.cnblogs.com/felixwang2/p/9190692.html 一.模型保存 # https://www.cnblogs.com/felixwa ...