1. 线性结构

1 . ArrayList

// ArrarList
# include 

# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
# define MAXSIZE 20

typedef int Status;
typedef int ElemType;
typedef struct{
    ElemType data[MAXSIZE];
    int length;
}ArrayList;

Status InitList(ArrayList *);
Status ListEmpty(ArrayList);
Status ClearList(ArrayList *);
Status GetElem(ArrayList, int, ElemType *);
Status ListInsert(ArrayList *, int, ElemType);
Status ListDelete(ArrayList *, int, ElemType *);
void ListPrint(ArrayList);

int main(){
        ArrayList list;
        InitList(&list);

        ListInsert(&list, 1, 10);
        ListInsert(&list, 2, 20);
        ListInsert(&list, 3, 30);
        ListInsert(&list, 4, 40);
        ListPrint(list);
        printf("-----------------------\n");

        int val;
        GetElem(list, 4, &val);
        printf("%d\n", val);
        printf("-----------------------\n");

        ListDelete(&list, 3, &val);
        printf("%d\n", val);
        printf("-----------------------\n");
        ListPrint(list);
        printf("-----------------------\n");

        ClearList(&list);
        ListPrint(list);
    return 0;
}

Status InitList(ArrayList * L){
    L->length = 0;
    return OK;
}

Status ClearList(ArrayList * L){
    for (int i = 0; i < L->length; i++) {
            i[L->data] = -1;
    }
    L->length = 0;
    return OK;
}

Status GetElem(ArrayList L, int i, ElemType * e){
    if(L.length == 0 || i < 1 || i > L.length){
        return ERROR;
    }

    *e = L.data[i-1];
    return OK;
}

Status ListInsert(ArrayList * L, int i, ElemType e){
    if(L->length == MAXSIZE){
        return ERROR;
    }

    if(i < 1 || i > L->length + 1){
        return ERROR;
    }

    // 从List尾 往i 移动交换位置
    if(i <= L->length){
        for(int k = L->length-1; k >= i - 1; k--){
            L->data[k+1] = L->data[k];
        }
    }
    L->data[i-1] = e;
    L->length++;

    return OK;
}

Status ListDelete(ArrayList * L, int i, ElemType * e){
    if(L->length == 0){
        return ERROR;
    }

    if(i < 1 || i > L ->length){
        return ERROR;
    }

    *e = L->data[i-1];
    if(i < L->length){
        for(int k = i; k < L->length; k++){
            L->data[k-1] = L->data[k];
        }
    }
    L->length--;
    return OK;
}


void ListPrint(ArrayList L){
    for (int i = 0; i < L.length; i++) {
        printf("%d ", i[L.data]);
    }
    printf("\n");
}

  1. LinkedList
// LinkedList
# include 
# include 
# include 

# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
# define MAXSIZE 20

typedef int Status;
typedef int ElemType;

typedef struct Node{
    ElemType data;
    struct Node *next;;
} Node;
typedef struct Node *LinkedList;

void CreateListHead(LinkedList *, int);
void CreateListTail(LinkedList *, int);
Status ClearList(LinkedList *);
Status GetElem(LinkedList, int, ElemType *);
Status ListInsert(LinkedList *, int, ElemType);
Status ListDelete(LinkedList *, int, ElemType *);
void ListPrint(LinkedList);

int main(){
    LinkedList list;
    CreateListHead(&list, 10);
    ListPrint(list);
    ClearList(&list);

    CreateListTail(&list, 10);
    ListPrint(list);

    ListInsert(&list, 1, 101);
    int val;
    GetElem(list, 11, &val);
    printf("%d\n", val);
    ListPrint(list);

    ListDelete(&list, 10, &val);
    printf("%d\n", val);
    ListPrint(list);
    return 0;
}

// 头插法
void CreateListHead(LinkedList * L, int n){
    LinkedList p;
    int i;
    srand(time(0));

    *L = (LinkedList)malloc(sizeof(Node));
    (*L)->next = NULL;
    for (int i = 0; i < n; i++)
    {
        p = (LinkedList)malloc(sizeof(Node));
        p->data = rand() % 100 + 1;
        p->next = (*L)->next;
        (*L)->next = p;
    }
}

// 尾插法
void CreateListTail(LinkedList * L, int n){
    LinkedList p,r;
    int i;
    srand(time(0));

    * L = (LinkedList)malloc(sizeof(Node));
    r = *L;
    for (int i = 0; i < n; i++) {
        p = (LinkedList)malloc(sizeof(Node));
        p->data = rand() % 100 + 1;
        r->next = p;
        r = p;
    }
    r->next = NULL;
}

Status ClearList(LinkedList * L){
    LinkedList p,q;
    p = (*L)->next;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }

    (*L)->next=NULL;
    return OK;
}

Status GetElem(LinkedList L, int i, ElemType * e){
    int j;
    LinkedList p = L->next;
    j = 1;
    while (p && j < i)
    {
        p = p->next;
        j++;
    }

    if(!p || j > i){
        return ERROR;
    }

    *e = p->data;
    return OK;
}

Status ListInsert(LinkedList * L, int i, ElemType e){
    int j;
    LinkedList p,s;

    p  = * L;
    j = 1;
    while (p && j < i)
    {
        p = p->next;
        j++;
    }

    if(!p || j > i){
        return ERROR;
    }

    s = (LinkedList)malloc(sizeof(Node));
    s->data = e;
    s->next = p->next;
    p->next = s;

    return OK;
}

Status ListDelete(LinkedList * L, int i, ElemType * e){
    int j;
    LinkedList p,q;

    p  = * L;
    j = 1;
    while (p && j < i)
    {
        p = p->next;
        j++;
    }

    if(!(p->next) || j > i){
        return ERROR;
    }

    q = p->next;
    p->next = q->next;
    *e = q->data;
    free(q);

    return OK;
}

void ListPrint(LinkedList L){
    LinkedList p = L->next;
    while (p)
    {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
}

你可能感兴趣的:(1. 线性结构)