#include <iostream>
#include <time.h>
using namespace std;
#define Maxsize 50 //设置Sqlist表最大存储范围
#define success true
#define failed false
typedef struct{
int data[Maxsize];
int length;
}Sqlist;
//初始化
void initList(Sqlist &L, int len);
//判断是否为空
bool isEmpty(Sqlist L);
//查看Sqlist长度
int Sqlist_length(Sqlist L);
//显示Sqlist
void Sqlist_display(Sqlist L);
//插入元素
bool Sqlist_insert(Sqlist &L, int location, int value);
//删除数据 通过位置
bool Sqlist_delete_index(Sqlist &L, int location);
//删除元素 通过数值
bool Sqlist_delete_value(Sqlist &L, int location);
//查找元素_index return value
int Sqlist_select_index(Sqlist L, int index);
//查找元素_value return index
int Sqlist_select_value(Sqlist L, int value);
int main() {
Sqlist L;
srand((unsigned)time(NULL));
initList(L,0); //初始化Sqlist
for (int i=1; i<=10; i++){
Sqlist_insert(L, i, i); //赋值
}
Sqlist_display(L); //显示数据
cout<<Sqlist_select_index(L, 4)<<endl; //根据该下标查找数据
cout<<Sqlist_select_value(L, 2)<<endl; //更具数值查找下标
Sqlist_delete_index(L, 10); //删除数据
Sqlist_delete_value(L, 1);
Sqlist_display(L);
return 0;
}
void initList(Sqlist &L, int len){
L.length = len;
}
bool isEmpty(Sqlist L) {
if (Sqlist_length(L)==0) {
return success;
}
else{
return failed;
}
}
int Sqlist_length(Sqlist L) {
return L.length;
}
bool Sqlist_insert(Sqlist &L, int location, int value) {
if (Sqlist_length(L) == Maxsize){
cout<<"overflow"<<endl;
return failed;
}
if (location<1 || location>Maxsize){
cout<<"Sorry, Wrong location index"<<endl;
return failed;
}
location--;
for (int j=L.length; j>location; j--){
L.data[j] = L.data[j-1];
}
L.data[location]=value;
L.length++;
return success;
}
bool Sqlist_delete_index(Sqlist &L, int location) {
if (isEmpty(L)){
cout<<"Sqlist is empty"<<endl;
return failed;
}
else if (location<0 || location>Sqlist_length(L)){
cout<<"Sorry, wrong location index"<<endl;
return failed;
}
else{
for(int i =location-1; i<Sqlist_length(L); i++){
L.data[i] = L.data[i+1];
}
L.length--;
return success;
}
}
bool Sqlist_delete_value(Sqlist &L, int value) {
int index=0;
if (isEmpty(L)){
cout<<"Sqlist is empty"<<endl;
return failed;
}
else{
while(index<Sqlist_length(L)&& L.data[index] != value)
index++;
if (index>Sqlist_length(L))
return failed;
else if (Sqlist_delete_index(L, index)){
return success;
}
else
return failed;
}
}
int Sqlist_select_index(Sqlist L, int index) {
if (isEmpty(L)){
cout<<"Sqlist is empty"<<endl;
return 0;
}
else if (index<0 || index>Sqlist_length(L)){
cout<<"Wrong index"<<endl;
return 0;
}
else{
return L.data[index-1];
}
}
int Sqlist_select_value(Sqlist L, int value) {
int index=0;
if (isEmpty(L)){
cout<<"Sqlist is empty"<<endl;
return 0;
}
else{
while(index<L.length && L.data[index] != value)
index++;
if (index>Sqlist_length(L))
return 0;
else{
return index+1;
}
}
}
void Sqlist_display(Sqlist L) {
if (isEmpty(L)) {
cout<<"Sqlist为空,没有数据"<<endl;
}
else{
for (int i=0; i<Sqlist_length(L); i++){
//cout<<i<<endl;
cout<<L.data[i]<<" ";
}
cout<<endl;
}
}