(36)4.20 数据结构(线性表)初始化,输入,删除,查找

#include<stdio.h>

#include<stdlib.h>

#include<errno.h>

#define InitSize 10

#define Max_Size 10

//1.顺序表上基本操作的实现
//顺序表的初始化

typedef struct

{

int* data; //指示动态分配数组指针

int MaxSize;//顺序表的最大容量

int length;//顺序表的当前长度

} SeqList;

typedef struct

{

int* data[Max_Size]; //指示动态分配数组指针

int length;//顺序表的当前长度

} SqList;

void InitList(SeqList* L)

{

L->data = (int*)malloc(InitSize * sizeof(int));

L->length = 0;

L->MaxSize = InitSize;

}

void IncreaseSize(SeqList* L, int len)

{

int* p = L->data;

L->data = (int*)malloc(InitSize * sizeof(int) + 5);

if (L->data == NULL)

{

printf("%s\n", strerror(errno));

}

for (int i = 0; i < L->length; i++)

{

L->data[i] = p[i];

}

L->MaxSize =L-> MaxSize + len;

free(p);

}

void ListInsert(SqList* L, int i,int e)

{

if (i<1 || i>L->length + 1)

return 1;

if (L->length >= Max_Size)

return 1;

for (int j = L->length; j >= i; j--)

{

L->data[j] = L->data[j - 1];

L->data[j - 1] = e;

L->length++;

}

}

void ListDelete(SqList* L, int i, int e)

{

if (i<1 || i>L->length)

return 0;

e = L->data[i - 1];

for (int j = i; j < L->length; j++)

{

L->data[j-1] = L->data[j];

L->length--;

}

return 1;

}

int main()

{

SeqList L;//声明一个顺序表

InitList(&L);//初始化顺序表

IncreaseSize(&L, 5);//增加内存

ListInsert(&L, 3, 3);//插入数据

int e = -1;

ListDelete(&L, 3, &e);

if (ListDelete)//删除数据

printf("已删除第3个元素,删除元素的值为=%d\n", e);

else

printf("位序不合法,删除失败\n");

return 0;

}

相关推荐
故事和你913 小时前
洛谷-数据结构1-4-图的基本应用1
开发语言·数据结构·算法·深度优先·动态规划·图论
破浪前行·吴5 小时前
数据结构概述
数据结构·学习
小欣加油6 小时前
leetcode2078 两栋颜色不同且距离最远的房子
数据结构·c++·算法·leetcode·职场和发展
我真不是小鱼6 小时前
cpp刷题打卡记录30——轮转数组 & 螺旋矩阵 & 搜索二维矩阵II
数据结构·c++·算法·leetcode
码完就睡9 小时前
数据结构——栈和队列的相互模拟
数据结构
iiiiyu9 小时前
常用API(SimpleDateFormat类 & Calendar类 & JDK8日期 时间 日期时间 & JDK8日期(时区) )
java·大数据·开发语言·数据结构·编程语言
故事和你919 小时前
洛谷-数据结构1-4-图的基本应用2
开发语言·数据结构·算法·深度优先·动态规划·图论
꧁细听勿语情꧂11 小时前
数据结构概念和算法、时间复杂度、空间复杂度引入
c语言·开发语言·数据结构·算法
Felven11 小时前
B. The 67th 6-7 Integer Problem
数据结构·算法
研☆香11 小时前
聊一聊如何分析js中的数据结构
开发语言·javascript·数据结构