#include <bits/stdc++.h>
using namespace std;
typedef int DataType;
typedef struct SeqList * PSeqList;
struct SeqList{
int MAXNUM;
int n;
int* element;
};
PSeqList createNullList_seq(int m);//创建空顺序表
int isNullList_seq(PSeqList palist);//判断线性表是否为空
int locate_seq(PSeqList palist, DataType x);//在顺序表中求元素下标
int insertPre_seq(PSeqList palist, int p, DataType x);//在palist所指的顺序表中下标为p的元素之前插入元素
int insertPost_seq(PSeqList palist, int p, DataType x);//在palist所指的顺序表中下标为p的元素之后插入元素
int deleteP_seq(PSeqList palist, int p);//删除下标为p的元素
int deleteV_seq(PSeqList palist, DataType x);//删除值为x的元素
int show_seq(PSeqList palist);//打印所有元素
void inversion_seq(PSeqList palist);//置逆表中元素
int main(){
cout << "创建一个多少大小的顺序表" << endl;
int m;
cin >> m;
PSeqList myList = createNullList_seq(m);
if(isNullList_seq(myList))
cout << "此时表为空" << endl;
else cout << "表不为空" << endl;
cout << "开始插入元素" << endl;
int insertX;
for(int i = 0;i < m;i ++){
cin >> insertX;
insertPre_seq(myList, i, insertX);
}
if(isNullList_seq(myList))
cout << "此时表为空" << endl;
else cout << "表不为空" << endl;
cout << "输出所有元素" << endl;
show_seq(myList);
cout << "删除某个位置的元素" << endl;
int delP;
cin >> delP;
deleteP_seq(myList, delP);
cout << "输出所有元素" << endl;
show_seq(myList);
cout << "还剩" << myList -> n << "个元素" << endl;
cout << "在某个位置后面插入元素" << endl;
cout << "输入插入下标" << endl;
int index;
cin >> index;
cout << "输入插入元素" << endl;
cin >> insertX;
insertPost_seq(myList, index, insertX);
cout << "输出所有元素" << endl;
show_seq(myList);
cout << "置逆顺序表" << endl;
inversion_seq(myList);
cout << "输出所有元素" << endl;
show_seq(myList);
system("pause");
return 0;
}
PSeqList createNullList_seq(int m){//创建空顺序表
PSeqList palist = (PSeqList)malloc(sizeof(struct SeqList));
if(palist != NULL){
palist -> element = (DataType*)malloc(sizeof(DataType) * m);
if(palist -> element){
palist -> MAXNUM = m;
palist -> n = 0;//空表长度为0
return palist;
}
else free(palist);
}
printf("Out of space!!\n");//储存分配失败
return NULL;
}
int isNullList_seq(PSeqList palist){//判断线性表是否为空
return(palist -> n == 0);
}
int locate_seq(PSeqList palist, DataType x){//在顺序表中求元素下标
for(int q = 0;q < palist -> n;q ++){
if(palist -> element[q] == x)
return q;
}
return -1;
}
int insertPre_seq(PSeqList palist, int p, DataType x){//在palist所指的顺序表中下标为p的元素之前插入元素
if(palist -> n >= palist -> MAXNUM){//溢出
DataType * pos1 = (DataType *)malloc(sizeof(DataType) * palist -> MAXNUM * 2);//申请两倍的空间补救
if(pos1 == NULL){
printf("Overflow!\n");
return 0;
}
for(int q = 0;q < palist -> MAXNUM;q++){
pos1[q] = palist -> element[q];
}
free(palist -> element);
palist -> element = pos1;
palist -> MAXNUM *= 2;
}
if(p < 0|| p > palist -> n){//不存在下标为p的元素
printf("Not exist!\n");
}
for(int q = palist -> n - 1;q >= p;q --){//插入位置及以后的元素均后移一个位置
palist -> element[q+1] = palist -> element[q];
}
palist -> element[p] = x;//插入元素
palist -> n ++;//元素个数加1
return 1;
}
int insertPost_seq(PSeqList palist, int p, DataType x){//在palist所指的顺序表中下标为p的元素之后插入元素
if(palist -> n >= palist -> MAXNUM){//溢出
DataType * pos1 = (DataType *)malloc(sizeof(DataType) * palist -> MAXNUM * 2);//申请两倍的空间补救
if(pos1 == NULL){
printf("Overflow!\n");
return 0;
}
for(int q = 0;q < palist -> MAXNUM;q++){
pos1[q] = palist -> element[q];
}
free(palist -> element);
palist -> element = pos1;
palist -> MAXNUM *= 2;
}
if(p < 0|| p > palist -> n){//不存在下标为p的元素
printf("Not exist!\n");
}
for(int q = palist -> n - 1;q >= p + 1;q --){//插入位置后一位及以后的元素均后移一个位置
palist -> element[q+1] = palist -> element[q];
}
palist -> element[p+1] = x;//插入元素
palist -> n ++;//元素个数加1
return 1;
}
int deleteP_seq(PSeqList palist, int p){//删除下标为p的元素
if(p < 0 || p > palist -> n - 1){//不存在下标为p的元素
printf("Not exist!\n");
return 0;
}
for(int q = p;q < palist -> n-1;q ++){//被删除之后的元素均迁移一个位置
palist -> element[q] = palist -> element[q+1];
}
palist -> n --;
}
int deleteV_seq(PSeqList palist, DataType x){//删除值为x的元素
int p = locate_seq(palist, x);
if(p == -1){
printf("Not exsit!\n");
}
else deleteP_seq(palist, p);
}
int show_seq(PSeqList palist){//打印表中所有元素
if(isNullList_seq(palist))
printf("List is NULL!\n");
else for(int i = 0;i < palist -> n;i ++){
printf("%d ", palist -> element[i]);
}
printf("\n");
return 1;
}
void inversion_seq(PSeqList palist){//置逆表中元素
if(palist -> n == 0 || palist -> n == 1)
return;
else{
int count = palist -> n / 2;
for(int i = 0;i < count;i ++){
DataType t = palist -> element[i];
palist -> element[i] = palist -> element[palist -> n-1-i];
palist -> element[palist -> n-1-i] = t;
}
}
}