数据结构(考研)

线性表

顺序表

顺序表的静态分配

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;
 } 
相关推荐
chenyuhao202433 分钟前
链表的面试题4之合并有序链表
数据结构·链表·面试·c#
水水沝淼㵘1 小时前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
莹莹学编程—成长记2 小时前
list基础用法
数据结构·list
清幽竹客2 小时前
redis数据结构-09 (ZADD、ZRANGE、ZRANK)
数据结构·数据库·redis
葵花日记3 小时前
数据结构——二叉树
c语言·数据结构
越城4 小时前
数据结构中的栈与队列:原理、实现与应用
c语言·数据结构·算法
似水এ᭄往昔4 小时前
【数据结构】——栈和队列OJ
c语言·数据结构·c++
lwewan4 小时前
26考研——中央处理器_指令执行过程(5)
笔记·考研
双叶8364 小时前
(C语言)超市管理系统(测试版)(指针)(数据结构)(二进制文件读写)
c语言·开发语言·数据结构·c++
想睡hhh6 小时前
c++进阶——哈希表的实现
开发语言·数据结构·c++·散列表·哈希