顺序表的基础操作代码
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREAMENT 10
#define TRUE 0
#define FALSE 1
#define HS 2
/*这里是后续添加的,并且只在1,2,3中使用到*/
typedef int Status;
typedef int ElemType;
typedef struct ListNode{ //定义结构体
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList &L) { //1,初始化线性表
L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(L.elem)
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return TRUE;
//cout<<"线性表初始化成功"<<endl;
}
Status DestroyList(SqList &L) { //2,销毁线性表
if(L.elem) {
free(L.elem);
L.elem = NULL;
L.length = 0;
L.listsize = 0;
return TRUE;
//cout<<"线性表已销毁"<<endl;
} else {
return FALSE;
//cout<<"线性表不存在"<<endl;
}
}
Status ClearList(SqList &L) { //3,清空线性表
if(L.elem) {
if(L.length == 0) {
return TRUE;
// cout<<"线性表已为空"<<endl;
} else {
L.length = 0;
return FALSE;
//cout<<"线性表已清空"<<endl;
}
} else {
return HS;
//cout<<"线性表不存在"<<endl;
}
}
Status ListEmpty(SqList &L) { //4,判断线性表是否为空
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
cout<<"线性表不为空"<<endl;
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status GetLength(SqList &L) { //5,获取线性表长度
if(L.elem) {
cout<<"线性表长度为"<<L.length<<endl;
} else {
cout<<"线性表不存在"<<endl;
}
}
Status GetElem(SqList &L) { //6,获取线性表中指定位置元素
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
while(true) {
int i;
cout<<"请输入所查找的位置:";
cin>>i;
if(i>0&&i<=L.length) {
cout<<"第"<<i<<"个元素是"<<L.elem[i-1]<<endl;
break;
} else {
cout<<"输入位置有误!请重新输入"<<endl;
}
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status GetSite(SqList &L) { //7,获取线性表元素位置
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
int i,flag = 0;
cout<<"请输入所要查找的元素:";
cin>>i;
for (int j = 0;j<L.length;j++) {
if (L.elem[j] == i) {
int k = j + 1;
cout<<"该元素的位置有"<<k<<endl;
flag++;
}
}
if (flag == 0) {
cout<<"该线性表中无此元素"<<endl;
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status GetPrior(SqList &L) { //8,求前驱
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
int i,flag = 0;
cout<<"请输入元素:";
cin>>i;
for (int j = 0;j<L.length;j++) {
if (L.elem[j] == i) {
if (j == 0) {
cout<<"该元素在第一个位置,没有前驱元素"<<endl;
flag++;
} else {
cout<<"该元素的前驱元素有:"<<L.elem[j-1]<<endl;
flag++;
}
}
}
if (flag == 0) {
cout<<"该线性表中没有该元素!"<<endl;
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status GetNext(SqList &L) { //9,求后继
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
int i,flag = 0;
cout<<"请输入元素:";
cin>>i;
for (int j = 0;j<L.length;j++) {
if (L.elem[j] == i) {
if (j == L.length-1) {
cout<<"该元素在最后一个位置,没有后继元素"<<endl;
flag++;
} else {
cout<<"该元素的后继元素有:"<<L.elem[j+1]<<endl;
flag++;
}
}
}
if (flag == 0) {
cout<<"该线性表中没有该元素!"<<endl;
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status InsertElem(SqList &L) { //10,指定位置插入元素
if(L.elem) {
int i,flag = 0;
while(true) {
while (true) {
cout<<"需要插入元素的位置:";
cin>>i;
cout<<endl;
if(i<1||i>L.length+1) {
cout<<"表中没有该位置!是否重新进行插入操作?(R/0):";
int m;
cin>>m;
cout<<endl;
if (m == 0) {
cout<<"插入操作已停止!"<<endl;
flag++;
break;
} else {
continue;
}
} else {
break;
}
}
if(flag == 1) {
break;
}
if (L.length>=L.listsize) {
cout<<"当前存储空间已满!输入1重新分配地址,输入其他则停止插入操作"<<endl;
int k; cin>>k;
if(k == 1) {
ElemType *NewElem;
NewElem = (ElemType*)realloc(L.elem,(L.listsize + LISTINCREAMENT)*sizeof(ElemType));
if(!NewElem) {
cout<<"新地址分配失败!程序停止运行!"<<endl;
break;
} else {
L.elem = NewElem;
L.listsize += LISTINCREAMENT;
}
}
}
ElemType e;
cout<<"请输入需要插入在"<<i<<"位置的元素:";
cin>>e;
cout<<endl;
ElemType *q;
q = &(L.elem[i-1]);
ElemType *p;
for(p = &(L.elem[L.length-1]);p>=q;--p) {
*(p + 1) = *p;
}
*q = e;
++L.length;
cout<<"是否继续插入元素?(R/0)";
cout<<endl;
int d;
cin>>d;
if(d == 0) {
cout<<"插入操作结束!"<<endl;
break;
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status DeleteElem(SqList &L) { //11,指定位置删除
if(L.elem) {
int i,flag = 0;
while(true) {
while (true) {
cout<<"需要删除元素的位置:";
cin>>i;
if(i<1||i>L.length) {
cout<<"表中没有该位置!是否重新进行删除操作?(R/0)"<<endl;
int m;
cin>>m;
if (m == 0) {
cout<<"插入操作已停止!"<<endl;
flag++;
break;
break;
} else {
continue;
}
} else {
break;
}
}
if (flag == 1) {
break;
}
ElemType *p,*q;
p = &(L.elem[i-1]);
q = L.elem + L.length - 1;
for (++p; p <= q;++p) {
*(p-1) = *p;
}
--L.length;
cout<<"是否继续删除元素?(R/0)"<<endl;
int d; cin>>d;
if(d == 0) {
cout<<"删除操作结束!"<<endl;
break;
}
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status ShowElem(SqList &L) { //12,遍历线性表
if(L.elem) {
if(L.length == 0) {
cout<<"线性表为空"<<endl;
} else {
for (int i = 0;i<L.length;i++) {
cout<<L.elem[i]<<" ";
}
cout<<endl;
}
} else {
cout<<"线性表不存在"<<endl;
}
}
Status MergeList() { //13,合并两个非递减有序线性表
SqList LA,LB,LC;
cout<<"建立第一个线性表:";
InitList(LA);
cout<<"向第一个线性表中插入元素"<<endl;
InsertElem(LA);
cout<<"建立第二个线性表:" ;
InitList(LB);
cout<<"向第二个线性表中插入元素"<<endl;
InsertElem(LB);
cout<<"第一个线性表:";
ShowElem(LA);
cout<<endl;
cout<<"第二个线性表:";
ShowElem(LB);
cout<<endl;
cout<<"初始化最终线性表:";
InitList(LC);
int i = 0;
int j = 0;
int k = 0;
int lalen = LA.length;
int lblen = LB.length;
while((i < lalen) && (j < lblen)) {
if (LA.elem[i] <= LB.elem[j]) {
if (LA.elem[i] == LB.elem[j]) {
LC.elem[k] = LA.elem[i];
i++; j++; k++;LC.length++;
} else {
LC.elem[k] = LA.elem[i];
i++; k++; LC.length++;
}
} else {
LC.elem[k] = LB.elem[j];
j++; k++; LC.length++;
}
}
while(i<lalen) {
LC.elem[k] == LA.elem[i];
i++; k++; LC.length++;
}
while(j<lblen) {
LC.elem[k] = LB.elem[j];
j++; k++; LC.length++;
}
for(int g = 0;g < k;g++) {
cout<<LC.elem[g]<<" ";
}
}
menu() {
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<<"9----求后继"<<endl;
cout<<"10---在线性表指定位置插入元素"<<endl;
cout<<"11---删除线性表指定位置的元素"<<endl;
cout<<"12---显示线性表"<<endl;
cout<<"13---合并两个非递减有序的线性表"<<endl;
cout<<" 退出,输出一个负数!"<<endl;
}
int main() {
int a;
SqList L;
L.elem = NULL;
menu();
while (true) {
cout<<"请选择程序:";
cin>>a;
if (a<0) {
cout<<"程序已退出!"<<endl;
getch();
}
if (a == 0 && a > 13) {
cout<<"程序选择错误!请重新选择:";
getch();
}
if (a > 0 && a < 14) {
switch(a) {
case 1:{
//InitList(L);
if (InitList(L) == TRUE) {
cout<<"线性表初始化成功"<<endl;
break;
} else {
cout<<"程序出现问题,请联系技术人员"<<endl;
}
}
case 2:{
DestroyList(L);
break;
}
case 3:{
ClearList(L);
break;
}
case 4:{
ListEmpty(L);
break;
}
case 5:{
GetLength(L);
break;
}
case 6:{
GetElem(L);
break;
}
case 7:{
GetSite(L);
break;
}
case 8:{
GetPrior(L);
break;
}
case 9:{
GetNext(L);
break;
}
case 10:{
InsertElem(L);
break;
}
case 11:{
DeleteElem(L);
break;
}
case 12:{
ShowElem(L);
break;
}
case 13:{
MergeList();
break;
}
}
}
}
}
第一次写代码,还望大佬给些建议优化程序