#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define TURE 1
#define FALSE0
#define OVERFLOW -2
#define INFEASIBLE -1
#define OK 1
#define ERROR 0
typedef struct {
int* elem;
int length;
}SqList;
int InitList(SqList& L) {
L.elem = new int[MAXSIZE];
if (!L.elem) return ERROR;
L.length = 0;
return OK;
}
void DestoryList(SqList& L) {
if (L.elem) delete(L.elem);
}
void ClearList(SqList& L) {
L.length = 0;
}
int GetLength(SqList L) {
return L.length;
}
int IsEmpty(SqList L) {
if (L.length == 0) return 1;
else return 0;
}
//取值{将位置为i的值取出,并记录在e中
int GetElem(SqList L, int i, int& e) {
if (i<1 || i>L.length) return ERROR;
e = L.elem[i - 1];
return OK;
}
//查找e的值
int LocateElem(SqList L, int e) {
for (int i = 0; i < L.length; i++)
if (L.elem[i] == e) return i + 1;
return 0;
}
int ListInsert(SqList& L, int i, int e) {
if (i<1 || i>L.length + 1) return ERROR;
if (L.length == MAXSIZE) return ERROR;
for (int j = L.length - 1; j >= i - 1; j--) {
L.elem[j + 1] = L.elem[j];
}
L.elem[i - 1] = e;
L.length++;
return OK;
}
int DeleteList(SqList& L, int i, int e) {
if (i<1 || i>L.length) return ERROR;
for (int j = i; j <= L.length - 1; j++) {
L.elem[j - 1] = L.elem[j];
}
L.length--;
return OK;
}
int main() {
SqList L;
InitList(L);
L.elem[0] = 1;
L.elem[1] = 2;
L.elem[2] = 3;
L.elem[3] = 4;
L.elem[4] = 5;
L.elem[5] = 6;
L.elem[6] = 7;
L.length = 7;
ListInsert(L, 3, 666);
for (int i = 0; i <= L.length - 1; i++) {
printf("%d\n", L.elem[i]);
}
printf("返回值:%d\n", IsEmpty(L));
printf("长度%d\n", GetLength(L));
printf("666是第%d个\n", LocateElem(L, 666));
int e = 0;
GetElem(L, 5, e);
printf("第5个位置的值为%d\n",e);//如果想返回这里必须是e
printf("666所在的位置为为%d\n", LocateElem(L, 666));
printf("--------------------------------华-华-丽-丽-分-割-线---------------------------------------");
return 0;
}
顺序表的一些基本操作的实现
最新推荐文章于 2023-01-14 17:57:53 发布