c2.LinerList(SeqList)

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值