SeqList
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
/*SeqList*/
#define maxsize 1000
typedef struct
{
float *elem;
int length;
}Sqlist;//DS of sql
bool Initlist(Sqlist &L) //initialize the sql
{
L.elem = new float[maxsize];
if(!L.elem) return false;
L.length=0;
return true;
}
bool ListInsert(Sqlist &L,int i,float e) //insert qn elem
{
if((i<1)||(i>L.length+1)||(L.length==maxsize)) return false;
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 true;
}
bool GetElem(Sqlist &L,int i,float &out) //获取线性表中某个位置的值
{
if(i<1 || i>L.length) return false;
out=L.elem[i-1];
return true;
}
int ListSearch(Sqlist L,float x) //顺序表查找
{
for(int j=0; j<L.length; j++)
{
if(x==L.elem[j]) return j+1;
}
return 0;
}
bool ElemDelete(Sqlist &L,int i,float &de_save)//元素删除
{
if((i<1)||i>L.length) return false;
de_save=L.elem[i-1];
if((L.length==1)||(i==L.length))
{
L.elem[i]=0;
}
else
{
for(int j=i-1;j<L.length-1;j++)
{
L.elem[j]=L.elem[j+1];
}
}
L.length--;
return true;
}
void UnionSql(Sqlist &L,Sqlist &L1,Sqlist &L2)//顺序表连接
{
L2.length=L.length+L1.length;
int i;
for(i=0;i<L.length;i++)
{
L2.elem[i]=L.elem[i];
}
for(int j=0;j<L1.length;j++)
{
L2.elem[i+j]=L1.elem[j];
}
}
void PrintSql(Sqlist L)
{
for(int i=0;i<L.length;i++)
{
cout << L.elem[i] << ", " ;
}cout << endl;
}
int main()
{
// Sqlist Sql;
// Initlist(Sql);
// float getelem;
// float de_save;
// int pos;//显示查找位置
// cout << "after initialize,the length is: " << Sql.length << endl;
// ListInsert(Sql,1,12);
// cout << "after insert 12,the length is:" << Sql.length << endl;
// float A[6]={34,56,78,90,12,33};
// for(int i=1;i<7;i++)
// {
// ListInsert(Sql,i+1,A[i-1]);
// }
// cout << "after insert 5 elem,the length is:" << Sql.length << endl;
// PrintSql(Sql);
// GetElem(Sql,4,getelem);
// cout << "have gotten the 4th one is: " << getelem << endl;
// pos=ListSearch(Sql,90);
// cout <<"after search 90,the position is:" << pos << endl;
// ElemDelete(Sql,5,de_save);
// cout <<"after delete 90,de_save is:" << de_save << "the length of Sql is: " << Sql.length << endl;
Sqlist L,L1,L2;
Initlist(L);Initlist(L1);Initlist(L2);
float A[6]={34,56,78,90,12,33},B[6]={1,2,3,4,5,6};
for(int i=0;i<6;i++)
{
ListInsert(L,i+1,A[i]);
ListInsert(L1,i+1,B[i]);
}
PrintSql(L);PrintSql(L1);
UnionSql(L,L1,L2);
cout << "after union L and L1,L2 is:";
PrintSql(L2);
return 0;
}