线性表
啥也不说了,直接上代码
main.cpp
#include<iostream>
using namespace std;
#include"statement.h"
int main()
{
struct Arr arr1;
init(&arr1);
while (true) {
cout << "MENU" << endl;
cout << "1.判断数组是否为空" << endl;
cout << "2.判断数组是否为满" << endl;
cout << "3.遍历数组元素" << endl;
cout << "4.追加" << endl;
cout << "5.插入" << endl;
cout << "6.删除" << endl;
cout << "7.倒置" << endl;
cout << "8.排序" << endl;
cout << "0.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
if (isempty(&arr1) == true) {
cout << "数组为空" << endl;
}
else cout << "数组不为空" << endl;
break;
case 2:
if (isfull(&arr1) == true) {
cout << "数组为满" << endl;
}
else cout << "数组不为满" << endl;
break;
break;
case 3:
show(&arr1);
break;
case 4:
append(&arr1);
break;
case 5:
insert(&arr1);
break;
case 6:
delete_a(&arr1);
break;
case 7:
cout << "正在开发" << endl;
break;
case 8:
cout << "正在开发" << endl;
break;
case 9:
cout << "正在开发" << endl;
break;
case 0:
cout << "您已退出" << endl;
system("pause");
return 0;
}
}
}
statement.cpp
#include<iostream>
#include<malloc.h>
using namespace std;
void init(struct Arr *array);//初始化
bool isempty(struct Arr* array);//是否空
bool isfull(struct Arr* array);//是否满
void insert(struct Arr* array);//插入
void append(struct Arr* array);//追加(末尾插入)
void delete_a(struct Arr* array);//删除
void sort_123(struct Arr* array);//正向排序
void show(struct Arr* array);//遍历
void inversion(struct Arr* array);//倒置
struct Arr {
int* pBase;
int len;
int cnt;
};
fuction.cpp
#include"statement.h"
void init(struct Arr* array) {
int length;
cout << "请输入您需要的数组长度:";
cin >> length;
array->pBase = (int*)malloc(sizeof(int) * length);
array->len = length;
array->cnt = 0;
cout << "您的数组已经初始化" << endl;
return;
}
bool isempty(struct Arr* array){
if (0 == array->cnt) {
return true;
}
else return false;
}
bool isfull(struct Arr* array) {
if (array->cnt>=array->len) {
return true;
}
else return false;
}
void show(struct Arr* array) {
if (isempty(array)) {
cout << "数组为空" << endl;
}
else {
for (int i = 0; i < array->cnt; i++) {
cout <<array->pBase[i] << " ";
}
cout << endl;
}
}
void insert(struct Arr* array) {
if (isfull(array) == true) {
cout << "数组已满无法插入";
}
else {
int val, loc;
cout << "请输入您要插入的元素值:";
cin >> val;
cout << "请输入您要插入的元素位置:";
cin >> loc;
if (loc <= 0 || loc > array->cnt+1) {
cout << "error" << endl;
}
for (int i = array->cnt; i >= loc; i--) {
array->pBase[i] = array->pBase[i-1];
}
array->pBase[loc-1] = val;
array->cnt++;
cout << "插入成功,当前数组为:";
show(array);
cout << endl;
}
return;
}
void append(struct Arr* array) {
if (isfull(array)==true) {
cout << "数组已满无法追加";
}
else {
int val;
cout << "请输入您要追加的元素值:";
cin >> val;
array->pBase[array->cnt] = val;
array->cnt++;
cout << "追加成功,当前数组为:";
show(array);
cout << endl;
}
return;
}
void delete_a(struct Arr* array) {
int dloc;
cout << "请输入您要删除第几个元素:";
cin >> dloc;
if (dloc<0 || dloc>array->cnt) {
cout << "error" << endl;
}
for (int i = dloc; i < array->cnt; i++)
{
array->pBase[i - 1] = array->pBase[i];
}
array->cnt = array->cnt - 1;
cout << "删除成功,当前数组为:";
show(array);
cout << endl;
}
void inversion(struct Arr* array) {
cout << "当前数组为:";
show(array);
cout << endl;
int i = 0, j = array->cnt-1, k=0;
while (i < j) {
k = array->pBase[i];
array->pBase[i] = array->pBase[j];;
array->pBase[j]= k;
}
cout << "倒置成功,当前数组为:";
show(array);
cout << endl;
}
fuctions.cpp才是后期需要着重复习的内容,其实只要搞清楚数组,结构体和指针,这里非常简单,没有难点,多打打草稿很好写出来
还有一个功能块就是排序,在后一个板块会讲出。