数据结构(考研)

线性表

顺序表

顺序表的静态分配

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;
 } 
相关推荐
qqxhb44 分钟前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
晚云与城1 小时前
【数据结构】顺序表和链表
数据结构·链表
FirstFrost --sy2 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Yingye Zhu(HPXXZYY)3 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle4 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
秋说5 小时前
【PTA数据结构 | C语言版】两枚硬币
c语言·数据结构·算法
☆璇6 小时前
【数据结构】栈和队列
c语言·数据结构
chao_7899 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
秋说9 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
qqxhb10 小时前
零基础数据结构与算法——第四章:基础算法-排序(中)
数据结构·算法·排序算法·归并·快排·堆排