数据结构(考研)

线性表

顺序表

顺序表的静态分配

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;
 } 
相关推荐
怀旧,1 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
凤年徐2 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
浩瀚星辰20244 小时前
C++树状数组详解
java·数据结构·算法
起个数先5 小时前
快速排序算法(Java)
数据结构·排序算法
chao_78912 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说14 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
谭林杰16 小时前
B树和B+树
数据结构·b树
卡卡卡卡罗特17 小时前
每日mysql
数据结构·算法
chao_78917 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找