#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE - 1
#define OVERFLOW - 2
#define MAXSIZE 100
#define SIZE 4
typedef int Status;
//定义一个顺序存储结构的数据类型
typedef struct {
int* elem;
int length;
}SqList;
Status InitList(SqList& L) {
L.elem = new int[MAXSIZE];
if (!L.elem) exit(OVERFLOW);
L.length = 0;
return OK;
}
Status ListDelete(SqList& L, int i) {
if ((i<1) || (i>L.length + 1)) return ERROR;
int e = L.elem[i - 1];
for (int j = i; j <= L.length - 1; j++) {
L.elem[j - 1] = L.elem[j];
}
L.length--;
return OK;
}
void ShowElem(SqList& L, int i) {
if (ListDelete(L, i)) {
for (int i = 0; i < L.length; i++)
cout << L.elem[i] << " ";
}
else cout << "插入位置错误!" << endl;
}
void main() {
SqList L;
InitList(L);
cout << "请输入数组元素:" << endl;
for (int i = 0; i < SIZE; i++) {
cin >> L.elem[i];
L.length++;
}
int i;
cout << "请输入要删除的位置:" << endl;
cin >> i;
cout << "打印新数组的元素:" << endl;
ShowElem(L, i);
}
顺序表的删除(自我练习)
最新推荐文章于 2022-04-22 19:23:53 发布