数据结构C-2-顺序表基本操作之程序

#include <stdio.h>
#include <stdlib.h>

//静态分配
//#define LIST_MAX_LENTH 100
//typedef struct ElemType{
//    ElemType Elem_array[LIST_INIT_SIZE];
//    int length;
//}Sqlist;

//动态分配
#define LIST_INIT_SIZE 100  //初始的存储容量
#define LISTINCREMENT 10  //每次再分配的存储容量
#define OK 1
#define TRUE 1
#define ERROR -1
#define OVERFLOW -2

typedef int Status;
typedef int ElemType;
typedef struct{
    ElemType *elem;//数组存储空间基地址
    int length;//当前数组长度
    int listsize;//当前分配的存储容量,不够用时可再分配
}Sqlist;

Status InitList(Sqlist &L);
Status DestroyList(Sqlist &L);
Status ClearList(Sqlist &L);
Status ListEmpty(Sqlist L);
Status ListLength(Sqlist L);
Status GetElem(Sqlist L,int i,ElemType &e);
Status ListInsert(Sqlist &L,int i,ElemType e);
Status ListDelete(Sqlist &L,int i,ElemType &e);
int LocateElem(Sqlist L,ElemType e);
Status Locate_Delete_List(Sqlist &L,ElemType x);
void Union(Sqlist &La,Sqlist &Lb);

int main(void){
    Sqlist La;
    La.elem={3,5,8,11};
    Sqlist Lb;
    La.elem={2,6,8,9,11,15,20};
    Union(La,Lb);
    for(int i=0;i<La.length;i++)
        printf("%d ",La.elem[i]);
    return 0;
}
//初始化L
Status InitList(Sqlist &L){
    L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)
        exit(OVERFLOW);
    L.length=0;
    L.listsize=LIST_INIT_SIZE;
    return OK;
}
//销毁L
Status DestroyList(Sqlist &L){
    if(L.elem){
        L.length=0;
        L.listsize=0;
        free(L.elem);
    }
    return OK;
}
//清空线性表
Status ClearList(Sqlist &L){
    if(!ListEmpty(L)){
        for(int i=0;i<L.length;i++)
            L.elem[i]=0;
        L.length=0;
    }
    return OK;
}
//判断L是否为空
Status ListEmpty(Sqlist L){
    if(L.length==0)
        return TRUE;
    else
        return ERROR;
}
//返回线性表的长度
Status ListLength(Sqlist L){
    return L.length;
}
//用e返回线性表的第i个元素
Status GetElem(Sqlist L,int i,ElemType &e){
    if(i<0||i>L.length)
        return ERROR;
    else{
        e=L.elem[i-1];
        return OK;
    }
}
Status ListInsert(Sqlist &L,int i,ElemType e){
    ElemType *newbase,p,q;
    if(i<0||i>L.length+1)
        return ERROR;
   if(L.length>=L.listsize){
        newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase)
            exit(OVERFLOW);
        L.elem=newbase;
        L.listsize+=LISTINCREMENT;
   }
     *q=&(L.elem[i-1]);
    //第i个数之后全部后移一个位置
    for(p=&(L.elem[L.length-1]);p>=q;--p)
        *(p+1)=*p;
    *q=e;//插入
    ++L.length;
    return OK;
}
//删除第i个位置的元素,用e
Status ListDelete(Sqlist &L,int i,ElemType &e){
    ElemType *p,*q;
    if(L.length===0)
        return ERROR;
    else if(i<1||i>L.length)
        return ERROR;
    p=&(L.elem[i-1]);
    e=*p;
    q=L.elem+L.length-1;
    for(++p;p<=q;++p)
        *(p-1)=*p;
    --L.length;
    return OK;

int LocateElem(Sqlist L,ElemType e){
    int i=1;
    while(i<=L.length&&e!=L.elem[i-1])
        ++i;
    if(i<L.length)
        return i;
    else
        return 0;
}
//查找定位删除
Status Locate_Delete_List(Sqlist &L,ElemType x){
    int i;
    while(i<=L.length){
        if(L.elem[i-1]!=x)
            i++;
        else{
            for(k=i;k<L.length;k++)
                L.elem[k-1]=L.elem[k];
            L.length--;
            break;
        }
    }
    if(i>L.length){
        printf("要删除的数据元素不存在!\n");
        return ERROR;
    }
    return OK;
}
//合并
void Union(Sqlist &La,Sqlist &Lb){
    int la_len=ListLength(La),lb_len=ListLength(Lb),i;
    ElemType e;
    for(i=0;i<lb_len;i++){
        GetElem(La, i,e);
        if(!LocateElem(La,e))
          ListInsert(La,++la_len,e) ;
    }
}

顺序表的合并,La,Lb------>La: Merge()
顺序表的合并,La,Lb------>Lc: Merge_2()

#include <stdio.h>
#include<stdlib.h>
//静态分配
#define LIST_MAX_LENTH 100
typedef int Status;
typedef int ElemType;
typedef struct ElemType{
    ElemType Elem_array[LIST_MAX_LENTH];
    int length;
}List;
void CreateList(List *L);
int LocateElem(List *L,ElemType e);
void Merge(List *La,List *Lb);
void Merge_2(List *La,List *Lb,List *Lc);
void ListInsert(List *L,ElemType e);
void Display(List *L);

int main(void){
    List La,Lb,Lc;
    CreateList(&La);Display(&La);
    CreateList(&Lb);Display(&Lb);
    Merge(&La,&Lb);
    printf("La:The new result is:");
    Display(&La);
    Merge_2(&La,&Lb,&Lc);
    printf("Lc:The new result is:");
    Display(&Lc);
    return 0;
}
//c创建List
void CreateList(List *L){
    int len,n;
    printf("Please enter the size:");
    scanf("%d",&len);
    L->length=len;
    printf("Please enter the elements:");
    for(int i=0;i<L->length;i++){
        scanf("%d",&n);
        L->Elem_array[i]=n;
    }
}
//La,Lb,将Lb元素加到La后面
void Merge(List *La,List *Lb){
    ElemType e;
    for(int  j=0;j<Lb->length;j++){
        e=Lb->Elem_array[j];
        if(!LocateElem(La,e))
            ListInsert(La,e);
    }
}
//La、Lb------->Lc
void Merge_2(List *La,List *Lb,List *Lc){
    int i=0,j=0,k=0;
    while(i<La->length&&j<Lb->length){
        if(La->Elem_array[i]<Lb->Elem_array[j]){
            Lc->Elem_array[k]=La->Elem_array[i];
            k++;i++;
        }
        else if(La->Elem_array[i]>Lb->Elem_array[j]){
            Lc->Elem_array[k]=Lb->Elem_array[j];
            k++;j++;
        }
        else{
            Lc->Elem_array[k]=Lb->Elem_array[j];
            k++;j++;i++;
        }
    }
        //当某顺序表完成后
        while(i<La->length){
            Lc->Elem_array[k]=La->Elem_array[i];
            i++;k++;
        }
        //当某顺序表完成后
        while(j<Lb->length){
            Lc->Elem_array[k]=Lb->Elem_array[j];
            j++;k++;
        }
        Lc->length=k;
}
int LocateElem(List *L,ElemType e){
    int flag=0;
    for(int i=0;i<L->length;i++){
        if(L->Elem_array[i]==e)
            flag=1;
        else
            flag=0;
    }
    return flag;
}
void ListInsert(List *L,ElemType e){
    if(L->length>=LIST_MAX_LENTH)
        exit(-1);
    L->Elem_array[L->length]=e;
    L->length++;
}
void Display(List *L){
    for(int i=0;i<L->length;i++)
        printf("%d ",L->Elem_array[i]);
    printf("\n");
}
发布了24 篇原创文章 · 获赞 9 · 访问量 2746

猜你喜欢

转载自blog.csdn.net/fancyZT/article/details/104435553