数据结构(考研)

线性表

顺序表

顺序表的静态分配

cs 复制代码
//线性表的元素类型为 ElemType

//顺序表的静态分配 
#define MaxSize=10
typedef int ElemType;
typedef struct{
	ElemType data[MaxSize];
	int length;
}SqList;

顺序表的动态分配

cs 复制代码
//顺序表的动态分配
#define InitSize 10
typedef struct{
	ElemType * data;
	int MaxSize
	int length;
}SqList; 

//初始化
void InitList(SqList &L)
{
	L.data=(ElemType *)malloc(InitSize*sizeof(ElemType));
	L.length=0;
	L.MaxSize=InitSize;
 } 

插入操作 O(n)

cs 复制代码
//插入操作
#define MaxSize=10
typedef int ElemType;
typedef struct{
	ElemType data[MaxSize];
	int length;
}SqList;

bool ListInsert(SqList &L,int i,int e)
{
	if(i<1||i>L.length+1) return false;
	if(L.length==MaxSize) return false;
	for(int j=L.length;j>=i;j--)
	{
		L.data[j]=L.data[j-1];
	}
	L.data[i-1]=e;
	return true;
}

删除操作 O(n)

cs 复制代码
//删除操作
#define MaxSize=10
typedef int ElemType;
typedef struct{
	ElemType data[MaxSize];
	int length;
}SqList;
bool ListDelete(SqList &L,int i,ElemType &e)
{
	if(i<1||i>L.length) return false;
	e=L.data[i-1];
	for(int j=i;j<L.length;j++)
	{
		L.data[j-1]=L.data[j];
	}
	L.length--;
	return true;
	
}

按值查找****O(n)

cs 复制代码
int LocateElem(SqList L,ElemType e)
{
    int i;
    for(i=0;i<L.length;i++)
    {
        if(L.data[i]==e) return i+1;
    }
    return 0;
 } 
相关推荐
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1232 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1123 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19433 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww3 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚3 天前
[数据结构] 排序
数据结构
睡不醒的kun3 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌3 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long3 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn3 天前
Redis7底层数据结构解析
前端·数据结构·bootstrap