3.9 10 12 数据结构 顺序表

直接代码

接口

点击查看代码
#pragma once

typedef int SLDataType;//直接修改 类型(也可以使用c++的模板)
typedef struct Sequence
{

	SLDataType* array;
	size_t size;//实际大小
	size_t volume;//容易大小


}Sequence;

//增 删 查 改 

//顺序表初始化
void qk(Sequence* ps);

//空间判断 空间增加
void SeqListInit(Sequence* ps);
//----------------------------------------------------
///顺序表尾插入
void SeqListPushBack(Sequence* ps, SLDataType x);
//从顺序表头部插入数据
void SeqListPushFront(Sequence* ps, SLDataType x);
//从顺序表中间插入数据
void SeqListInsert(Sequence* ps, size_t pos, SLDataType x);
//----------------------------------------------------

//----------------------------------------------------
//头删
void SeqListPopFront(Sequence* ps);
//尾删
void SeqListPopBack(Sequence* ps);
//随便删
void SeqListErase(Sequence* ps, size_t pos);
//-----------------------------------------------------
//查找
int SeqListFind(Sequence* ps, SLDataType x);

////改
void SeqListErase(Sequence* ps, size_t pos, SLDataType x);

代码实现

点击查看代码
#include"ctj.h"
#include <stdlib.h>
#include <assert.h>
#include<stdio.h>
void qk(Sequence* ps)
{
	ps->array = NULL;
	ps->size = 0;
	ps->volume = 0;

}
//判断顺序表空间
void SeqListInit(Sequence* ps)
{
	//1.应该使用三目操作符判断此时容量是否为空,为空容量则开辟成4,再使用realloc开辟动态内存
	SLDataType* tmp =NULL;
	int NewCapitcy;
	assert(ps);
	//空间满了直接给4
	if (ps->size == ps->volume)
	{
		 NewCapitcy = ps->volume == 0 ? 4 : ps->volume * 2;
		 tmp = (SLDataType*)realloc(ps->array, NewCapitcy * sizeof(SLDataType));
		 ps->array = tmp;
		 ps->volume = NewCapitcy;
		 printf("已增容空间------ 容量大小:%d\n", ps->volume);
	}

	

	
}


//顺序表尾插
void SeqListPushBack(Sequence* ps, SLDataType x)
{
	//判断是否需要扩大容量
	assert(ps);
	SeqListInit(ps);
	ps->array[ps->size] = x;
	ps->size++;

}
//顺序表头插
// 1 2 3 4 5 6 
// 9 1 2 3 4 5 6
void SeqListPushFront(Sequence* ps, SLDataType x)
{
	assert(ps);
	SeqListInit(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->array[i] = ps->array[i - 1];


	}
	ps->array[0] = x;
	ps->size++;


}
//随意插

// 1 2 3 4 5 6 
// 1 9 2 3 4 5 6
void SeqListInsert(Sequence* ps, size_t pos, SLDataType x)
{
	assert(ps);
	SeqListInit(ps);
	for (int i = ps->size; i >= pos; i--)
	{
		ps->array[i] = ps->array[i - 1];
		

	}
	ps->array[pos] = x;
	ps->size++;
}
////---------------------------------///

// 1 2 3 4
// 头
void SeqListPopFront(Sequence* ps)
{

	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		ps->array[i] = ps->array[i +1];

	}
	ps->size--;


}
//尾删
// 1 2 3 4
// 
void SeqListPopBack(Sequence* ps)
{
	ps->array[ps->size - 1] = 0X00;
	ps->size--;


}
//随便删
// 1 2 3 4
// 1 3 4 0
void SeqListErase(Sequence* ps, size_t pos)
{


	assert(ps);
	for (int i = pos-1; i <= ps->size; i++)
	{
		ps->array[i] = ps->array[i + 1];

	}
	ps->size--;

}
//---------------------------
int SeqListFind(Sequence * ps, SLDataType x)
{

	for (int i = 0; i < ps->size; i++)
	{

		if (ps->array[i] == x)
		{
			printf("下标:%d\n",i+1);
			return i+1;
		}

		
	}

	printf("未找到");

}
///-------------- 1 2 3 4
void SeqListErase(Sequence* ps, size_t pos, SLDataType x)
{
	int i = pos - 1;
	

	ps->array[i] = x;
}
posted @   逆向狗  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示