数据结构(考研)

线性表

顺序表

顺序表的静态分配

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;
 } 
相关推荐
小妖6664 小时前
js 实现快速排序算法
数据结构·算法·排序算法
独好紫罗兰6 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n6 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
2401_841495647 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
独好紫罗兰7 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495647 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
数智工坊7 小时前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构
数智工坊8 小时前
【数据结构-树与二叉树】4.3 二叉树的存储结构
数据结构
独好紫罗兰8 小时前
对python的再认识-基于数据结构进行-a004-列表-实用事务
开发语言·数据结构·python
铉铉这波能秀8 小时前
LeetCode Hot100数据结构背景知识之列表(List)Python2026新版
数据结构·leetcode·list