数据结构上机1顺序表

#include <stdio.h>
#include <malloc.h>

#define OK              1
#define OVERFLOW       -1
#define ERROR           0
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10

typedef int ElemType;
typedef int Status;
typedef  struct{
    ElemType * elem;
    int        length;
    int        listsize;
}sqlist;

//初始化
Status InitList_sq(sqlist * L){
    L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if(!L->elem) 
      return OVERFLOW;
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return OK;
}

//插入
Status ListInsert_Sq(sqlist * L, int i, ElemType e){
    ElemType * newbase, * p, * q;

    if(i < 1 || i > L->length + 1) 
      return ERROR;
    if(L->length >= L->listsize){
        newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
        if(!newbase) 
          return OVERFLOW;
        L->elem = newbase;
        L->listsize = L->listsize + LISTINCREMENT;
    }
    q = &(L->elem[i-1]);
    for(p = &(L->elem[L->length-1]); p>=q; --p) 
      *(p+1) = *p;
    *q = e;
    ++L->length;
    return OK;
}

//往表中输入数值
void input_data(sqlist * L){
    int i, n=5;
    ElemType e;

    printf("请输入顺序表的数据:\n");
    for(i=1; i<=n; i++){
        printf("请输入第%d个数据 : ", i);
        scanf("%d", &e) ;
        if(!ListInsert_Sq(L,i,e)){
            printf("赋值失败!!");
            break;
        }
    }
    printf("输入完毕!");
    printf("表La的数据为:\n");
    for(i = 1; i <= L->length; i++)
        printf("e[%d]=%d\n", i-1, L->elem[i-1]);
    printf("长度: %d\n\n", L->length);
}

////合并
Status MergeList(sqlist * La,sqlist * Lb,sqlist * Lc){
        int * pa, * pa_last, * pb, * pb_last, * pc;

        pa = La->elem;
        pb = Lb->elem;
        Lc->listsize = Lc->length = La->length + Lb->length;
        pc = Lc->elem = (ElemType *)malloc(Lc->listsize*sizeof(int));
        if (!Lc->elem) 
            return OVERFLOW;
        pa_last = La->elem + La->length - 1;
        pb_last = Lb->elem + Lb->length - 1;
        while(pa <= pa_last)
            *pc++ = *pa++;
        while(pb <= pb_last)
            *pc++ = *pb++;
     
        return OK;
}

//主函数
int main(void){
    sqlist La, Lb, Lc;
    int i;
    ElemType e;

    printf("/*-------------合并操作----------------*\n");
    if(InitList_sq(&La)){
        printf("La初始化成功\n");
        printf("La长度: %d\n", La.length);
        printf("La容量: %d\n\n", La.listsize);
    }
    else
        printf("La初始化错误!");

    printf("请输入La中数据\n");
    input_data(&La);
    printf("输入完毕\n");

    
    if(InitList_sq(&Lb)){
        printf("Lb初始化成功\n");
        printf("Lb长度: %d\n", La.length);
        printf("Lb容量: %d\n\n", La.listsize);
    }
    else
        printf("Lb初始化错误!");
    printf("请输入Lb中数据\n");
    input_data(&Lb);
    if(MergeList(&La, &Lb, &Lc))
      printf("合并成功\n");
    for(i = 1; i <= Lc.length; i++)
        printf("e[%d]=%d\n", i-1, Lc.elem[i-1]);
    printf("长度: %d\n", Lc.length);

    return 0;
}

 

转载于:https://www.cnblogs.com/startnow/p/5052593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值