Created by C and deletions to the sequence table and combined.

Created by C and deletions to the sequence table and combined.

A recent study data structures, some algorithms do a bit of the second chapter, as a white record it.

#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int Status;
typedef int ElemType;

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) exit(OVERFLOW);
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return OK;
} //创建一个空的线性表L。

ElemType GetListElem(SqList const *L, int const i){
    ElemType *e;
    if (i < 1 || i > L->length)
        return ERROR;
    else
        e = L->elem +i -1;
    return *e;
}

Status ListInsert_Sq(SqList *L, int const i, ElemType const e){
    if (i < 1 || i > L->length + 1) return ERROR;
    if(L->length >= L->listsize){
        int *newbase;
        newbase = (ElemType*)realloc(L->elem,
                (L->listsize + LISTINCREMENT) * sizeof(ElemType));
        if (!newbase)exit(OVERFLOW);
        L->elem = newbase;
        L->listsize += LISTINCREMENT;
    }
    ElemType *q = &(L->elem[i-1]);   //q为插入的位置
    for (int *p = &(L->elem[L->length-1]);p >=q; --p)
        *(p + 1) = *p;       //将元素后移一位
    *q = e;
    ++L->length;
    return OK;
}

Status ListDelete_Sq(SqList* L, int i){
    if (i < 1 || i > L->length)
        return (ERROR);
    ElemType *p = &(L->elem[i-1]);    //p为删除元素的位置
    ElemType *q ;
    q = L->elem + L->length -1;
    for(++p; p <= q; ++p)
        *(p-1) = *p;
    --L->length;
    return OK;
}

Status MergeList(SqList *La, SqList *Lb, SqList *Lc){
    InitList_Sq(Lc);
    ElemType i= 1;ElemType j =1; ElemType k = 0;
    ElemType La_len = La->length;
    ElemType Lb_len = Lb->length;
    while ((i <= La_len) && (j <= Lb_len)){
        ElemType ai; ElemType bj;
        ai = GetListElem(La, i);
        bj = GetListElem(Lb, j);
        if(ai <= bj){
            ListInsert_Sq(Lc, ++k, ai);
            ++i;}
        else{
            ListInsert_Sq(Lc, ++k, bj);
            ++j;
        }
    }
    while (i <= La_len){
        ElemType ai;
        ai = GetListElem(La, i++);
        ListInsert_Sq(Lc, ++k, ai);
    }
    while (j <= Lb_len){
        ElemType bj;
        bj = GetListElem(Lb, j++);
        ListInsert_Sq(Lc, ++k, bj);

    }
    return OK;
}

Status PrintList(SqList *L){
    for (int i = 0;i < L->length ;i++) {
        printf("%d ",L->elem[i]);
    }
    return OK;
}

int main() {
    SqList la;
    SqList lb;
    SqList lc;
    InitList_Sq(&la);
    InitList_Sq(&lb);
    InitList_Sq(&lc);
    ListInsert_Sq(&la,1,1);ListInsert_Sq(&la,2,3);ListInsert_Sq(&la,3,5);ListInsert_Sq(&la,4,7);
    ListInsert_Sq(&lb,1,2);ListInsert_Sq(&lb,2,4);ListInsert_Sq(&lb,3,6);ListInsert_Sq(&lb,4,8);ListInsert_Sq(&lb,5,10);
    //ListDelete_Sq(&la, 1);
    int a = GetListElem(&la, 1);
    MergeList(&la,&lb,&lc);
    PrintList(&lc);
    return 0;
}

There are a few time doing questions:
1. In fact, the book is not about the interpretation of Status, Elem stated that
typedef int Status
the Internet search, the phrase is a custom type of statement. typedef used to define the type of alias.
status i; the equivalent int i;
As for why call status, possible reasons for the status of English means that state, programmers want to represent a state with an int value, so a custom type. Thus Status i; see that the variable i indicates a state variable. The int i; it can not convey to the reader such a meaning. But the essence is the same, so writing is to convey information about programming, to facilitate future maintenance, and read the program.

2. I use the book Tsinghua University Press is a data structure (C language version), most of the code book is pseudo-code, you need to define your own functions, and for the book, such as
void MergeList(List ls, List Lb, List &Lc)
interpretation of this definition, the teacher is added "&" array is changed before and after the occurrence of the parameter, but this just seems easy to understand in reading the book, I still take the form of adding pointer in the real code, ask the next class teacher.
3 do not initially understand why the definition to add elem pointer because L.elem L refers to the base address, and the role of the pointer variable for indicating the address.

Released five original articles · won praise 0 · Views 516

Guess you like

Origin blog.csdn.net/pursuingparadise/article/details/101621841