数据结构实验二线性表(顺序表)

   #include <stdio.h>
   #include <iostream> 
   using namespace std;
   #define OK 1
   #define ERROR 0
   #define OVERFLOW -1
   typedef int Status;
   

#define MAXSIZE 100  
typedef int ElemType;
typedef struct{
        ElemType *elem;
        int length;
}SqList;

//顺序表的建立 
Status InitList(SqList  &L){
      int i,n;
      L.elem=new ElemType[MAXSIZE];  //为顺序表分配一个大小为MAXSIZE的数组空间    
    if (!L.elem) exit(OVERFLOW);  //存储失败分配退出 
    cout<<"\n数据个数=";
      cin>>n;
      cout<<"\n输入"<<n<<"个数据: "; 
    for (i=0;i<n;i++)
        cin>>L.elem[i];
     L.length=n; //空表长度为n 
     return OK;
}

//顺序表的输出 
void OutputList(SqList L){
    int i;
    for (i=0;i<L.length;i++)
        cout<<" "<<L.elem[i];
    cout<<endl;
}

//顺序表的长度
int ListLength(SqList L){
    return L.length;

//顺序表的判断 
bool ListEmpty(SqList L){
    return L.length==0;

//取表中元素
Status GetElem(SqList L,int i,ElemType &e){
    if(i<1||i>L.length) return ERROR; //判断i值是否合理,若不合理,返回ERROR 
    e=L.elem[i-1]; //elem[i-1]单元存储第i个数据元素 
    return OK;
}

//查找表中元素
int LocateElem(SqList L,ElemType e){
    //在顺序表L中查找值为e的数据元素,返回其序号
    for(int i=0;i<L.length;i++)
        if(L.elem[i]==e) return i+1; //查找成功,返回序号i+1
    return 0; //查找失败,返回0 
}
//插入元素
Status ListInsert(SqList &L,int i,ElemType e){
    //在顺序表L中第i个位置插入新的元素e,i值的合法范围是1<=i<=L.length+1
    if(i<1||i>L.length+1) return ERROR; //i值不合法
    if(L.length==MAXSIZE) return ERROR; //当前存储空间已满
    for(int j=L.length;j>i-1;j--){
        L.elem[j]=L.elem[j-1]; //插入位置及之后的元素后移
    }
    L.elem[i-1]=e; //将新元素e放入第i个位置
    L.length++; //表长加一
    return OK; 
}
//删除元素 
Status ListDelete(SqList &L,int i,ElemType &e){
    //在顺序表中删除第i个元素,i值的合法范围是1<<i<<L.length 
    if(i<1||i>L.length) return ERROR; //i值不合法
    e=L.elem[i-1];
    for(int j=i-1;j<L.length;j++){
        L.elem[j]=L.elem[j+1]; //被删除元素之后的元素前移
    --L.length;
    }
    return OK; 
}

//操作结束 
void DestroyList(SqList &L){
    delete []L.elem;
    L.elem=NULL;
    L.length=0;    
}

//递增顺序表的合并
void MergeList_Sq(SqList LA,SqList LB,SqList &LC){
    LC.length=LA.length+LB.length;
    LC.elem=new ElemType[LC.length];
    ElemType *pc=LC.elem;
    ElemType *pa=LA.elem;
    ElemType *pb=LB.elem;
    ElemType *pa_last=LA.elem+LA.length-1;
    ElemType *pb_last=LB.elem+LB.length-1;
    while((pa<=pa_last)&&(pb<=pb_last)){
        if(*pa<=*pb) *pc++=*pa++;
        else *pc++=*pb++;
    }
    while(pa<=pa_last) *pc++=*pa++;
    while(pb<=pb_last) *pc++=*pb++;

  

#include "comdef.h"
#include "sqlistdef.h"
#include "sqlistapp.h"
int main(){
    SqList L,LA,LB,LC;
    int i,choice,n,m;
    ElemType e;    
    cout<<"\n建立数据表:";
    if (InitList(L)==OVERFLOW){
          cout<<"数据表空间分配失败,程序退出!";
         return 0;
         }
    else{
          cout<<"数据表建立成功,数据表为:";
          OutputList(L);
    } 
    do { 
        cout<<"\n\n===================================";
        cout<<"\n        顺序表的基本操作           ";
        cout<<"\n===================================";
        cout<<"\n           1:表的输出" ;
        cout<<"\n           2:求表的长度" ;
        cout<<"\n           3:判断空表" ;
        cout<<"\n           4:取表中元素" ;         
        cout<<"\n           5:查找表中元素" ;
        cout<<"\n           6:插入元素" ;
        cout<<"\n           7:删除元素" ;            
        cout<<"\n           8:递增元素的合并" ;            
        cout<<"\n           0:操作结束" ;
        cout<<"\n===================================";
        cout<<"\n请输入你的选择:"; 
        cin>>choice;
        switch (choice){
            case 1:    cout<<"\n数据表为:";
                    OutputList(L);
                       break;
            case 2:    cout<<"\n数据表的长度为:"<<ListLength(L); 
                       break;
            case 3:    if(ListEmpty(L))
                        cout<<"\n数据表为空表";
                    else
                        cout<<"\n数据表为非空表"; 
                       break;           
            case 4:    cout<<"\n输出位置:";
                        cin>>i;
                        if(GetElem(L,i,e)==ERROR)
                            cout<<"\n位置不合法,没有取到";
                        else
                            cout<<"\n取值成功,第"<<i<<"个位置的元素为:"<<e; 
                    break;
            case 5:    cout<<"\n输入待查找的数据:";
                        cin>>e;
                        i=LocateElem(L,e);
                        if(i==0)
                            cout<<"\n查找失败,"<<e<<"不在数据表中!";
                        else
                            cout<<"\n查找成功,"<<e<<"在数据表中第"<<i<<"个位置!"; 
                    break;
            case 6:    cout<<"\n插入元素位置和值:";
                        cin>>i>>e;
                    if(ListInsert(L,i,e)==OK){
                        cout<<"\n插入成功!";
                        cout<<"\n插入后的数据表:";
                        OutputList(L);
                    }
                    else
                        cout<<"\n插入位置不合法,或者存储空间已满!"; 
                    break;
            case 7:    cout<<"\n删除元素的位置:";
                        cin>>i; 
                        //ListDelete(L,i,e);
                        if(ListDelete(L,i,e)==OK){
                            cout<<"\n删除成功!"; 
                            cout<<"\n删除元素:"<<e;
                            cout<<"\n删除后的数据表:";
                            OutputList(L); 
                        } 
                    else
                        cout<<"\n删除位置不合法!"; 
                    break;
            case 8: cout<<"\n请输入有序表LA:";
                    InitList(LA);
                    cout<<"\n数据表LA为:";
                    OutputList(LA);
                    cout<<"\n请输入有序表LB:";
                    InitList(LB);
                    cout<<"\n数据表LB为:";
                    OutputList(LB);
                    MergeList_Sq(LA,LB,LC);
                    cout<<"\n数据表LC为:";
                    OutputList(LC);
                    break;
            case 0:    break; 
            default:cout<<"\n输入错误,重新输入!";            
        }
    } while (choice) ;    
    DestroyList(L);
    return 0; 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值