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;
}