当前位置:网站首页>(1), the sequential storage structure of linear table chain storage structure
(1), the sequential storage structure of linear table chain storage structure
2022-08-04 17:02:00 【Little Monk Hanshui Temple Wuxin】
一、前言
This book combines Cheng Jie's《大话数据结构》learning from a book.I have forgotten a lot of the data structure knowledge I learned before,Reread it recently.No theoretical knowledge is involved,Mainly code implementation.
二、线性表
线性表:0个或多个数据元素的有限序列.
线性表的抽象数据类型:顺序存储结构、链式存储结构
A sequential storage structure for a linear list,指的是用一段地址连续的存储单元依次存储线性表的数据元素.
三、线性表的顺序存储结构
线性表的顺序存储代码:
#define MAXSIZE 20
typedef char ElemType;
typedef struct{
ElemType data[MAXSIZE];
int length;
}SqList;
#define OK 1
#define ERROR 0
typedef int Status;
//获取元素
Status GetElem(SqList L, int i, ElemType *e)
{
if(L.length <= 0)
return ERROR;
if((i <= 0) || (i > L.length))
return ERROR;
*e = L.data[i-1];
return OK;
}
//插入
Status ListInsert(SqList *L, int i; ElemType e)
{
int k;
if(!L || (i <= 0) || (i>L->length +1))
return ERROR;
if(L->length == MAXSIZE)
return ERROR;
for(k = L->length - 1; k >= i-1; k--)
{
L->data[k+1] = L->data[k];
}
L->data[i-1] = e;
L->length++;
return OK;
}
//删除
Status ListDelete(SqList *L, int i, ElemType *e)
{
int k;
if(!L || (i<1) || (i>L->length))
{
return ERROR;
}
*e = L->data[i-1];
for(k = i-1; k < L->length - 1; k++)
{
L->data[k] = L->data[k+1];
}
L->length--;
return OK;
}
四、线性表的链式存储结构
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct Node{
ElemType data;
struct Node *next;
}Node;
typedef struct Node *LinkList;
#include <stdio.h>
#include "linklist.h"
//获取第iThe data field value of a node
Status GetElem(LinkList L, int i, ElemType *e)
{
int j;
LinkList p;
p = L->next;
if(i<1)
return ERROR;
j = 1;
while(p && j <i)
{
p = p->next;
j++;
}
if(!p)
return ERROR;
*e = p->data;
return OK;
}
Status ListInsert(LinkList *L, int i, ElemType e)
{
int j = 1;
LinkList p, s;
p = *L;
if(!L || i < 0)
return ERROR;
while(p || j < i)
{
p = p->next;
j++;
}
if(!p)
return ERROR;
s = (LinkList)malloc(sizeof(Node))
s->data = e;
s->next = p->next;
p-next = s;
return OK;
}
Status ListDelete(LinkList *L, int i, ElemType *e)
{
int j = 1;
LinkList p, q;
p = *L;
if(!L || i < 1)
return ERROR;
while(p->next || j < i)
{
p = p->next;
j++;
}
if(!p->next)
return ERROR;
q = p->next;
p->nex = q->next;
*e = q->data;
free(q);
return OK;
}
边栏推荐
猜你喜欢
随机推荐
HCIP WPN 实验
博云入选Gartner中国云原生领域代表性厂商
从-99打造Sentinel高可用集群限流中间件
Mobile magic box CM201-1_CW_S905L2_MT7668_wire brush firmware package
华为云数据治理生产线DataArts,让“数据‘慧’说话”
并发编程原理学习-reentrantlock源码分析
越来越火的图数据库到底能做什么?
Minecraft HMCL 使用认证服务器LittleSkin进行登录
WEB 渗透之逻辑漏洞
Go语言gin框架返回json格式里,怎么把某个int属性转成string返回?
dotnet core 隐藏控制台
WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
一张图片怎么旋转90度。利用ps
R语言使用yardstick包的gain_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的增益(gain)曲线(gain curve)
Qt自动补全之QCompleter使用
Analysis of the gourd baby
pygame的freetype模块
【LeetCode每日一题】——374.猜数字大小
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
Boost库学习笔记(一)安装与配置









