#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
typedef struct {
ElemType data[MaxSize];
int Length;
}SqList;
//基本接口
bool ListInsert(SqList &L,int i,ElemType e) {
if (i<1 || i>L.Length) { return false; }
if (L.Length > MaxSize) { return false; }
for (int j = L.Length; j >= i; j--) {
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;
L.Length++;
return true;
}
bool ListDelete(SqList& L, int i, ElemType& e) {
if (i<1 || i>L.Length) {
return false;
}
e = L.data[i - 1];
for (int j = i; j < L.Length; j++) {
L.data[j - 1] = L.data[j];
}
L.Length--;
return true;
}
int LocateElem(SqList L, ElemType e) {
int i;
for (i = 0; i < L.Length; i++) {
if (L.data[i] == e) {
return i + 1; //返回位序
}
}
return 0;
}
void PrintList(SqList L) {
for (int i = 0; i < L.Length; i++) {
printf("%3d", L.data[i]);
}
}
void swap(int &a,int &b) {
int temp;
temp = b;
b = a;
a = temp;
}
//基本接口
//输出最小值元素
int printMin(SqList L) {
int temp = L.data[0];
int MaxElem;
for (int i = 0; i < L.Length; i++) {
if (temp < L.data[i]) {}
else {
temp = L.data[i];
}
}
MaxElem = temp;
return MaxElem;
}
//元素逆置
void reverseList(SqList &L) {
for (int i = L.Length; i >= L.Length / 2; i--) {
swap(L.data[L.Length - i], L.data[i-1]);
}
}
//删除所有值为x的元素
void deleteElemX(SqList &L,ElemType x) {
int i = 0;
for (int j = 0; j < L.Length; j++) {
if (L.data[j] == x)
i++;
L.data[j] = L.data[j + i];
}
L.Length = L.Length - i;
}
//删除给定值之间的所有元素
bool delete_Range_Elements(SqList &L,ElemType sta,ElemType end) {
if (sta<1 || sta>L.Length) { return false; }
if (end<1 || end>L.Length) { return false; }
if (sta > end) { return false; }
if (L.Length == 0) { return false; }
for (int i = sta; i <= end; i++) {
L.data[i-1] = L.data[(i-1)+(end-sta+1)];
}
L.Length = L.Length - (end - sta + 1);
return true;
}
//从有序顺序表中删除所有值重复的元素
bool Remove_duplicate_elements(SqList& L) {
int i = 0;
if (L.Length == 0) { return false; }
for (int j = 0; j < L.Length; j++) {
if (L.data[j] == L.data[j + 1]) {
i++;
}
L.data[j] = L.data[j + i];
}
L.Length = L.Length - i;
return true;
}
//将两个有序顺序表合并为一个有序顺序表
SqList union_List1(SqList& L1, SqList& L2)
{
SqList rnew;
rnew.Length = L1.Length + L2.Length;
int i=0, j=0, k = 0;
while (i < L1.Length && j < L2.Length) {
if (L1.data[i] < L2.data[j]) {
rnew.data[k++] = L1.data[i++];
}
else {
rnew.data[k++] = L2.data[j++];
}
}
while (i < L1.Length) {
rnew.data[k++] = L1.data[i++];
}
while (i < L2.Length) {
rnew.data[k++] = L2.data[j++];
}
return rnew;
}