数据结构 定长顺序表

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<string>
#include<stdlib.h>
#include<assert.h>
using namespace std;
#define max 100
typedef struct arrlist {
	int arr[max];
	int cursize;
	int capacity;
}arrlist;

//1. 初始化顺序表
void initlist(arrlist *a);
//2. 判空
bool isempty(arrlist* a);
//3. 判满
bool isfull(arrlist* a);
//4. 获取当前容量
int getcapacity(arrlist* a);
//5. 尾插
void push_back(arrlist* a, int val);
//6. 打印顺序表
void print_(arrlist* a);
//7. 头插
void push_front(arrlist* a, int val);
//8. 指定下标插入
void insert_index(arrlist* a, int index, int val);
//9. 按值查询下标
int searchval(arrlist* a, int val);
//10. 头删
void popfront(arrlist* a);
//11. 尾删
void popback(arrlist* a);
//12. 按下标删除
void popindex(arrlist* a, int index);
//13. 按值删除
void popval(arrlist* a, int val);
//14. 获取首元素
int getfront(arrlist* a);
//15. 获取尾元素
int getback(arrlist* a);
//16. 获取指定下标元素
int getindex(arrlist* a, int index);
//17. 清空顺序表
void clearlist(arrlist* a);
//18. 销毁顺序表
void destroylist(arrlist* a);
//19. 冒泡排序
void bubblesort_list(arrlist* a);
//20. 二分查找
int binfindval(arrlist* a, int val);
//21. 反转顺序表
void reverse_list(arrlist* a);
//22. 合并两个有序顺序表
void mergelist(arrlist* a, arrlist* b);
//23. 顺序表相加
void addlist(arrlist* a, arrlist* b,arrlist* c);

源文件

cpp 复制代码
#include"定长顺序表.h"


//1. 初始化顺序表
void initlist(arrlist* a) {
	a->capacity = max;
	a->cursize = 0;
}
//2. 判空
bool isempty(arrlist* a) {
	assert(a != nullptr);
	return a->cursize == 0;
}
//3. 判满
bool isfull(arrlist* a) {
	assert(a != nullptr);
	return a->cursize == a->capacity;
}
//4. 获取当前容量
int getcapacity(arrlist* a) {
	assert(a != nullptr);
	return a->capacity;
}
//5. 尾插
void push_back(arrlist* a, int val) {
	assert(a != nullptr);
	if (isfull(a))return;
	a->arr[a->cursize++] = val;
}
//6. 打印顺序表
void print_(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	for (int i = 0; i < a->cursize; i++) {
		cout << a->arr[i] << " ";
	}cout << endl;
}
//7. 头插
void push_front(arrlist* a, int val) {
	assert(a != nullptr);
	if (isfull(a))return;
	memmove(a->arr + 1, a->arr, sizeof(a->arr[0]) * a->cursize++);
	a->arr[0] = val;
}
//8. 指定下标插入
void insert_index(arrlist* a, int index, int val) {
	assert(a != nullptr);
	if (isfull(a) || index < 0 || index > a->cursize)return;
	memmove(a->arr + index + 1, a->arr + index, sizeof(a->arr[0]) * (a->cursize++ - index));
	a->arr[index] = val;
}
//9. 按值查询下标
int searchval(arrlist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return -1;
	for (int i = 0; i < a->cursize; i++) {
		if (a->arr[i] == val)return i;
	}return -1;
}
//10. 头删
void popfront(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	memmove(a->arr, a->arr + 1, sizeof(a->arr[0]) * (--a->cursize));
}
//11. 尾删
void popback(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	--a->cursize;
}
//12. 按下标删除
void popindex(arrlist* a, int index) {
	assert(a != nullptr);
	if (isempty(a)||index<0||index>=a->cursize)return;
	memmove(a->arr + index, a->arr + index + 1, sizeof(a->arr[0]) * (--a->cursize - index));
}
//13. 按值删除
void popval(arrlist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return;
	for (int i = 0; i < a->cursize; i++) {
		if (a->arr[i] == val)memmove(a->arr + i, a->arr + i + 1, sizeof(a->arr[0]) * (--a->cursize - i--));
	}return;
}
//14. 获取首元素
int getfront(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return -1;
	return a->arr[0];
}
//15. 获取尾元素
int getback(arrlist* a) {
	assert (a!= nullptr);
	if (isempty(a))return -1;
	return a->arr[a->cursize - 1];
}
//16. 获取指定下标元素
int getindex(arrlist* a, int index) {
	assert(a != nullptr);
	if (isempty(a)||index<0||index>=a->cursize)return -1;
	return a->arr[index];
}
//17. 清空顺序表
void clearlist(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	a->cursize = 0;
}
//18. 销毁顺序表
void destroylist(arrlist* a) {
	assert(a != nullptr);
	a->cursize = a->capacity = 0;
}
//19. 冒泡排序
void bubblesort_list(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	for (int i = 0; i < a->cursize - 1; i++) {
		for (int j = 0; j < a->cursize - i - 1; j++) {
			if (a->arr[j] > a->arr[j + 1]) {
				a->arr[j] = a->arr[j] ^ a->arr[j + 1];
				a->arr[j + 1] = a->arr[j] ^ a->arr[j + 1];
				a->arr[j] = a->arr[j] ^ a->arr[j + 1];
			}
		}
	}
}
//20. 二分查找
int binfindval(arrlist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return -1;
	int left = 0, right = a->cursize-1;
	while (left <= right) {
		int mid = (right - left) / 2 + left;
		if (a->arr[mid] == val)return mid;
		else if (a->arr[mid] > val)right = mid - 1;
		else left = mid + 1;
	}return -1;
}
//21. 反转顺序表
void reverse_list(arrlist* a) {
	assert(a != nullptr);
	if (isempty(a)||a->cursize==1)return ;
	int* left = a->arr, *right = a->arr + a->cursize - 1;
	while (left!= right) {
		int temp = *left;
		*left = *right;
		*right = temp;
		left++; right--;
	}
}
//22. 合并两个有序顺序表
void mergelist(arrlist* a, arrlist* b) {
	assert(a != nullptr && b != nullptr);
	if (isempty(a) || isempty(b))return;
	int i = a->cursize-1, j = b->cursize-1;
	a->cursize += b->cursize;//这里假设容量够用
	int k = a->cursize-1;
	while (i >=0&&j>=0) {
		if (a->arr[i] < b->arr[j])a->arr[k--] = b->arr[j--];
		else a->arr[k--] = a->arr[i--];
	}
	while (j >= 0)a->arr[k--] = b->arr[j--];
}
//23. 顺序表相加
void addlist(arrlist* a, arrlist* b,arrlist *c) {
	assert(a != nullptr && b != nullptr&&c!=nullptr);
	if (isempty(a) || isempty(b))return;
	int i = a->cursize - 1, j = b->cursize - 1; int val = 0;
	while (val || i >= 0 || j >= 0) {
		if (i >= 0 && j >= 0)val += a->arr[i] + b->arr[j];
		else if (i >= 0 && j < 0)val += a->arr[i];
		else if (i < 0 && j >= 0)val += b->arr[j];
		push_front(c, val % 10); val /= 10;
	}
}

int main() {
	arrlist a;
	initlist(&a);
	return 0;
}
相关推荐
qq_419203231 小时前
深浅拷贝、STL迭代器失效
c++·深浅拷贝·stl迭代器失效
再卷也是菜2 小时前
C++篇(21)图
数据结构·c++·算法
星轨初途2 小时前
C++入门(算法竞赛类)
c++·经验分享·笔记·算法
Bona Sun2 小时前
单片机手搓掌上游戏机(十三)—pico运行fc模拟器之硬件准备
c语言·c++·单片机·游戏机
Bona Sun2 小时前
单片机手搓掌上游戏机(十八)—pico运行fc模拟器之更大屏幕
c语言·c++·单片机·游戏机
chenyuhao20242 小时前
MySQL索引特性
开发语言·数据库·c++·后端·mysql
没书读了3 小时前
数据结构-考前记忆清单
数据结构
小龙报3 小时前
【算法通关指南:数据结构和算法篇 】队列相关算法题:3.海港
数据结构·c++·算法·贪心算法·创业创新·学习方法·visual studio
辞旧 lekkk3 小时前
【c++】封装红黑树实现mymap和myset
c++·学习·算法·萌新