有码1----线性表基本操作(C)

时间:2022-05-14 20:40:05

包含两部份,”head.h” and “sqList.c”
英文很烂,所有注释都是随性而写。。
head.h:

/******************** * author : tiaoyu * datetime : 16:22 2016/1/11 * desc : List *******************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define SUCCESS 1
#define FAIL 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Status;

int max(int a, int b){return a>b?a:b;}
int min(int a, int b){return a<b?a:b;}

sqList.c:

/********************
 * author : tiaoyu
 * datetime : 16:22 2016/1/11
 * desc : List
 *******************/

#include "head.h"

#define LIST_INIT_SIZE 100 //List init value
#define LISTINCREMENT 10 //List increment size

typedef int ElemType;

typedef struct {
    ElemType *elem;     //List root address
    int length;         //List current size
    int listsize;       //List current get size (sizeof(ElemType))
} SqList;

//Init SqList
Status InitList(SqList &L) {
    //create a blank SqList
    L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));

    if (! L.elem) exit(OVERFLOW);   //create fail
    L.length = 0;                   //blank SqList length = 0
    L.listsize = LIST_INIT_SIZE;    //Init size

    return SUCCESS;
}

//destroy SqList
Status DestroyList(SqList &L) {
    /*free(&L.elem[0]); L.elem[0] = NULL; */
    for (int i = 0; i <= L.length-1; i += LISTINCREMENT) {
        free(&(L.elem[i]));
        L.elem[i] = NULL;
    }
    return SUCCESS;
}

//clear SqList
Status ClearList(SqList &L) {
    for (int i = 0; i <= L.length - 1; ++i) {
        L.elem[i] = 0;
    }
    return OK;
}

//is or not empty
Status ListEmpty(SqList L) {
    if (L.elem != NULL) return FALSE;
    else return TRUE;
}

//get elem in i index
Status GetElem(SqList L, int i, ElemType &e) {
    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i-1];
    return OK;
}

//the location of e in L
int LocateElem(SqList L, ElemType e, Status (*compare)(ElemType, ElemType)) {
    for (int i = 0; i <= L.length-1; ++i) {
        if (compare(L.elem[i], e))  return i+1;
    }
    return FALSE;
}

//is or not a is equal b
Status compare(ElemType a, ElemType b) {
    return a==b?TRUE:FALSE;
}

//get prior elem of e
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e) {
    for (int i = 1; i <= L.length-1; ++i) {
        if (L.elem[i] == cur_e) {
            pre_e = L.elem[i-1];
            return SUCCESS;
        }
    }
    return ERROR;
}

//get next elem of e
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e) {
    for (int i = 0; i < L.length-1; ++i) {
        if (L.elem[i] == cur_e) {
            next_e = L.elem[i+1];
            return SUCCESS;
        }
    }
    return ERROR;
}

//insert elem in L before i
Status ListInsert(SqList &L, int i, ElemType e) {
    if (i < 1 || i > L.length + 1)  //i is not legal
        return ERROR;
    if (L.length >= L.listsize) {       //if length overflow size
        ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT) * sizeof(ElemType));    //realloc
        if (!newbase)
            exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }

    ElemType *p, *q;
    q = &(L.elem[i-1]);     //q is insert index
    for ( p = &(L.elem[L.length - 1]); p >= q; --p)     //i to i+1
        *(p+1) = *p;

    *q = e;     //insert e
    ++L.length; //increase length

    return OK;
}

//delete elem in L
Status ListDelete(SqList &L, int i, ElemType &e) {
    if ( i < 1 || i > L.length) //if i is not legal
        return ERROR;

    e = L.elem[i-1];    //set value e = delete

    ElemType *p, *q;
    q = &(L.elem[L.length-1]);  //q is last elem
    for ( p = &(L.elem[i-1]); p < q; ++p) {     //change to left
        *p = *(p+1);
    }
    --L.length; //sub 1

    return OK;
}

//print L
void ListTraverse(SqList L, Status (*visit)(ElemType)) {
    int *p, *q;
    for (p = &(L.elem[0]); p <= &(L.elem[L.length-1]); ++p )
        visit(*p);
}

Status visit(ElemType a) {
    printf("%d | ", a);
}

//insert an example to SqList
void insert_Sq(SqList &L) {
    for (int i = 1; i <= LISTINCREMENT; ++i) {
        ListInsert(L, i, i);
    }
}
int main() {
    SqList L;
    ElemType e;
    InitList(L);
    insert_Sq(L);
    ListTraverse(L, (*visit));
    /*test destroy DestroyList(L); ListTraverse(L, (*visit)); */
    /*test delete

    ListDelete(L, 2, e);
    printf("%d-\n", e);
    ListTraverse(L, (*visit)); */
    /*  test prior and next
        PriorElem(L, 3, e);
        printf("%d-\n", e);
        NextElem(L, 3, e);
        printf("%d-\n", e); */


    /*test GetElem and LocateElem and ListEmpty, and ClearList;
    printf("%d-", LocateElem(L, 2, (*compare)));
    GetElem(L, 3, e);
    printf("%d-", e);
    printf("%d\n", ListEmpty(L));

    ClearList(L);
    ListTraverse(L, *visit); */
    return 0;
}