数据结构—顺序表(数据类型int)

以下是实现代码,代码仅供参考。

#include<malloc.h>
#include<assert.h>
typedef int DataType;
typedef struct SeqList
{
	DataType* _a;
	size_t _size; // 有效数据个数
	size_t _capacity; // 容量
}SeqList;

void SeqInit(SeqList* pSeq);//初始化表

void Checkcapacity(SeqList* pSeq);//检查容量/增容

void SeqPushBack(SeqList* pSeq, DataType x);//尾插
bool SeqPopBack(SeqList* pSeq);//尾删

void SeqPushFront(SeqList* pSeq, DataType x);//头插
void SeqPopFront(SeqList* pSeq);//头删


bool SeqInsert(SeqList* pSeq, size_t pos, DataType x);//节点前插入
bool SeqErase(SeqList* pSeq, size_t pos);//删除节点

bool SeqAt(SeqList* pSeq, size_t pos, DataType x);//修改 

void SeqDestory(SeqList* pSeq);//销毁表 

int SeqFind(SeqList* pSeq, DataType x);//查找 
int BinarySearch(SeqList* pSeq, DataType x); //二分查找节点

void Swap(int* a, int *b);//交换
void BubbleSort(SeqList* pSeq);//冒泡排序
void SelectSort(SeqList* pSeq);// 选择排序

void Mergelist(SeqList* pSeq1, SeqList* pSeq2, SeqList* pSeq);//合并两个非递减有序表

void SeqInit(SeqList* pSeq)//初始化表
{
	pSeq->_capacity = 10;
	pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capacity);
	assert(pSeq->_a);
	pSeq->_size = 0;
}

void Checkcapacity(SeqList* pSeq)//检查容量/增容
{
	assert(pSeq->_a);
	if (pSeq->_size >= pSeq->_capacity){
		pSeq->_a = (DataType*)realloc(pSeq->_a, sizeof(DataType)*(pSeq->_capacity * 2));
		assert(pSeq->_a);
		pSeq->_capacity *= 2;
	}
}

void SeqPushBack(SeqList* pSeq, DataType x)//尾插
{
	assert(pSeq->_a);
	Checkcapacity(pSeq);
	pSeq->_a[pSeq->_size] = x;
	++pSeq->_size;
}

bool SeqPopBack(SeqList* pSeq)//尾删
{
	assert(pSeq->_a);
	if (pSeq->_size == 0){
		return false;
	}
	else{
		pSeq->_a[pSeq->_size - 1] = 0;
		pSeq->_size--;
		return true;
	}
}

void SeqPushFront(SeqList* pSeq, DataType x)//头插
{
	assert(pSeq->_a);
	Checkcapacity(pSeq);
	for (size_t index = pSeq->_size; index > 0; --index) {
		pSeq->_a[index] = pSeq->_a[index - 1];
	}
	pSeq->_a[0] = x;
	++pSeq->_size;
}

void SeqPopFront(SeqList* pSeq)//头删
{
	assert(pSeq->_a);
	Checkcapacity(pSeq);
	for (size_t index = 0; index < pSeq->_size - 1; ++index){
		pSeq->_a[index] = pSeq->_a[index + 1];
	}
	--pSeq->_size;
}

bool SeqInsert(SeqList* pSeq, size_t pos, DataType x)//节点前插入
{
	assert(pSeq->_a);
	Checkcapacity(pSeq);
	if (pos > pSeq->_size){
		return false;
	}
	else{
		size_t index = pSeq->_size;
		for (size_t count = 0; count < pSeq->_size - pos; ++count){
			pSeq->_a[index] = pSeq->_a[index - 1];
			--index;
		}
		pSeq->_a[pos] = x;
		++pSeq->_size;
		return true;
	}
}

bool SeqErase(SeqList* pSeq, size_t pos)//删除节点
{
	assert(pSeq->_a);
	if (pSeq->_size == 0 || pos >= pSeq->_size){
		return false;
	}
	else{
		for (size_t index = pos; index < pSeq->_size - 1; ++index){
			pSeq->_a[index] = pSeq->_a[index + 1];
		}
		--pSeq->_size;
		return true;
	}
}

bool SeqAt(SeqList* pSeq, size_t pos, DataType x)//修改节点
{
	assert(pSeq->_a);
	if (pSeq->_size == 0 || pos >= pSeq->_size){
		return false;
	}
	else {
		pSeq->_a[pos] = x;
		return true;
	}
}

void SeqDestory(SeqList* pSeq)//销毁表
{
	assert(pSeq->_a);
	free(pSeq->_a);
	pSeq->_size = 0;
	pSeq->_capacity = 0;
}

int SeqFind(SeqList* pSeq, DataType x)//查找节点
{
	assert(pSeq->_a);
	if (pSeq->_size == 0){
		return -1;
	}
	else{
		for (size_t index = 0; index < pSeq->_size; index++){
			if (x == pSeq->_a[index]){
				return index;
			}
		}
		return -1;
	}
}

int BinarySearch(SeqList* pSeq, DataType x)//二分查找节点
{
	assert(pSeq->_a);
	int left = 0;
	int right = pSeq->_size - 1;
	while (left <= right){
		int mid = left + ((right - left) >> 1);
		if (pSeq->_a[mid] > x){
			right = mid - 1;
		}
		else if (pSeq->_a[mid] < x){
			left = mid + 1;
		}
		else{
			return mid;
		}
	}
	return -1;
}

void Swap(DataType*a, DataType* b)//交换
{
	DataType tmp = *a;
	*a = *b;
	*b = tmp;
}

void BubbleSort(SeqList* pSeq)//冒泡排序
{
	assert(pSeq->_a);
	for (size_t end = pSeq->_size - 1; end > 0; --end){
		for (size_t begin = 0; begin < end; ++begin){
			if (pSeq->_a[begin] > pSeq->_a[begin + 1]){
				Swap(&pSeq->_a[begin], &pSeq->_a[begin + 1]);
			}
		}
	}
}

void SelectSort(SeqList* pSeq)//选择排序
{
	assert(pSeq->_a);
	for (size_t begain = 0; begain < pSeq->_size - 1; ++begain){
		for (size_t index = begain + 1; index < pSeq->_size; ++index){
			if (pSeq->_a[begain] > pSeq->_a[index]){
				Swap(&pSeq->_a[begain], &pSeq->_a[index]);
			}
		}
	}
}

void Mergelist(SeqList* pSeq1, SeqList* pSeq2, SeqList* pSeq)//合并两个非递减有序表
{
	size_t i, j;
	i = j = 0;
	while (i < pSeq1->_size && j < pSeq2->_size){
		if (pSeq1->_a[i] <= pSeq2->_a[j]){
			SeqPushBack(pSeq, pSeq1->_a[i]);
			i++;
		}
		else{
			SeqPushBack(pSeq, pSeq2->_a[j]);
			j++;
		}
	}
	while (i <= pSeq1->_size - 1){
		SeqPushBack(pSeq, pSeq1->_a[i]);
		i++;
	}
	while (j <= pSeq2->_size - 1){
		SeqPushBack(pSeq, pSeq2->_a[j]);
		j++;
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c++实现的顺序:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值