1. 程式人生 > >數據結構---線性表---順序存儲結構

數據結構---線性表---順序存儲結構

return 下標 include pan 獲取元素 nbsp lists mem 順序存儲結構

頭文件

header.h

#ifndef _LIST_H
#define _LIST_H

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

#define MY_TYPE int   /*0:int 1:double 2:float 3:char*/
#define _MY_TYPE_ 0
#define LIST_INIT_SIZE 3   /*單位:MY_TYPE*/
#define LIST_INCRE_SIZE 2   /*單位:MY_TYPE*/

typedef 
enum status {ERROR, OK} St; /*0 1*/ typedef struct list_array{ MY_TYPE *head_pointer; /*指向存儲空間基址*/ int length; /*當前長度*/ int listsize; /*當前分配的存儲容量*/ }ALIST; typedef St (*compare)(const MY_TYPE, const MY_TYPE); /*初始化*/ St init_alist(ALIST *Lpointer); /*銷毀*/ St destroy_alist(ALIST
*Lpointer); /*清空*/ St clear_alist(ALIST *Lpointer); /*判斷線性表是否為空*/ St alist_empty(const ALIST *Lpointer); /*表長*/ int alist_length(ALIST *Lpointer); /*取給定位置的元素用*pe返回*/ St get_elem(const ALIST *Lpointer, const int i, MY_TYPE *pe); /*查看滿足compare關系的元素的位置*/ /*pc簡單設定為大於小於等於*/ int locate_elem(const
ALIST *Lpointer, const MY_TYPE e, compare pc); /*取某個元素的前驅*/ St prior_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e); /*取某個元素的後繼*/ St next_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e); /*在數組給定下標為i的位置插入e*/ St alist_insert(ALIST *Lpointer, const int i, const MY_TYPE e); /*增加一個元素在表尾*/ St alist_add(ALIST *Lpointer, MY_TYPE e); /*在數組給定下標為i的位置刪除此元素用*P_e返回*/ St alist_delete(ALIST *Lpointer, const int i, MY_TYPE *p_e); /*修改指定位置的元素*/ St alist_mod(ALIST *Lpointer, const int i, MY_TYPE mod_e); /*遍歷線性表*/ St alist_visit(const ALIST *Lpointer); /*compare--->equal*/ St cmp_equal(MY_TYPE e1, MY_TYPE e2); /*compare--->less*/ St cmp_less(MY_TYPE e1, MY_TYPE e2); /*compare--->more*/ St cmp_more(MY_TYPE e1, MY_TYPE e2); #endif /* _LIST_H */

順序表基本操作

初始化

/*初始化*/
St init_alist(ALIST *Lpointer){
    Lpointer->head_pointer = (MY_TYPE *)calloc(LIST_INIT_SIZE, sizeof(MY_TYPE));
    if(!Lpointer->head_pointer){
        return ERROR;
    }
    Lpointer->length = 0;
    Lpointer->listsize = LIST_INIT_SIZE;
    return OK;
}

銷毀

/*銷毀*/
St destroy_alist(ALIST *Lpointer){
    Lpointer->length = 0;
    Lpointer->listsize = 0;
    free(Lpointer->head_pointer);
    Lpointer->head_pointer = NULL;
    return OK;
}

清空

/*清空*/
St clear_alist(ALIST *Lpointer){
    Lpointer->length = 0;
    memset(Lpointer->head_pointer, 0, Lpointer->listsize);
    return OK;
}

判斷線性表是否為空

/*判斷線性表是否為空*/
St alist_empty(const ALIST *Lpointer){
    if(Lpointer->length)
        return ERROR;
    else
        return OK;
}

表長

/*表長*/
int alist_length(ALIST *Lpointer){
    return (Lpointer->length);
}

取給定位置的元素用*pe返回

/*取給定位置的元素用*pe返回*/
St get_elem(const ALIST *Lpointer, const int i, MY_TYPE *pe){
    if(i > -1 && i < Lpointer->length){
        *pe = *(Lpointer->head_pointer + i);
        return OK;
    }else{
        return ERROR;
    }
}

查看滿足compare關系的元素的位置

/*查看滿足compare關系的元素的位置*/                      /*pc簡單設定為大於小於等於*/
int locate_elem(const ALIST *Lpointer, const MY_TYPE e, compare pc){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if((*pc)(*(Lpointer->head_pointer + i), e))
            break;
    }
    if(i < Lpointer->length)
        return i;
    else
        return -1;
}

取某個元素的前驅

/*取某個元素的前驅*/
St prior_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if(cur_e == *(Lpointer->head_pointer + i)){
            break;
        }
    }
    if(i > 0 && i < Lpointer->length){
        *ppre_e = *(Lpointer->head_pointer + i - 1);
        return OK;
    }else{
        return ERROR;
    }
}

取某個元素的後繼

St next_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if(cur_e == *(Lpointer->head_pointer + i)){
            break;
        }
    }
    if(i > -1 && i < (Lpointer->length - 1)){
        *ppre_e = *(Lpointer->head_pointer + i + 1);
        return OK;
    }else{
        return ERROR;
    }
}

在數組給定下標為i的位置插入e

/*在數組給定下標為i的位置插入e*/
St alist_insert(ALIST *Lpointer, const int i, const MY_TYPE e){
    if(!(i > -1 && i <= Lpointer->length))
        return ERROR;
    if(Lpointer->length >= Lpointer->listsize){
        MY_TYPE *newpointer = (MY_TYPE *)realloc(Lpointer->head_pointer,        (Lpointer->listsize + LIST_INCRE_SIZE) * sizeof(MY_TYPE));
        if(!newpointer)
            return ERROR;
        Lpointer->head_pointer = newpointer;
        Lpointer->listsize += LIST_INCRE_SIZE;
    }
    for(int j = (Lpointer->length - 1); j > i; --j)
        *(Lpointer->head_pointer + j + 1) = *(Lpointer->head_pointer + j);
    *(Lpointer->head_pointer + i) = e;
    ++Lpointer->length;
    return OK;
}

增加一個元素在表尾

/*增加一個元素在表尾*/
St alist_add(ALIST *Lpointer, MY_TYPE e){
    if(!e)
        return ERROR;
    if(Lpointer->length >= Lpointer->listsize){
        MY_TYPE *newpointer = (MY_TYPE *)realloc(Lpointer->head_pointer,        (Lpointer->listsize + LIST_INCRE_SIZE) * sizeof(MY_TYPE));
        if(!newpointer)
            return ERROR;
        Lpointer->head_pointer = newpointer;
        Lpointer->listsize += LIST_INCRE_SIZE;
    }
    *(Lpointer->head_pointer + Lpointer->length) = e;
    ++Lpointer->length;
    return OK;
}

在數組給定下標為i的位置刪除此元素用*P_e返回

/*在數組給定下標為i的位置刪除此元素用*P_e返回*/
St alist_delete(ALIST *Lpointer, const int i, MY_TYPE *p_e){
    if(!(i > -1 && i < Lpointer->length))
        return ERROR;
    *p_e = *(Lpointer->head_pointer + i);
    for(int j = (Lpointer->length - 1); j > i; --j)
        *(Lpointer->head_pointer + j - 1) = *(Lpointer->head_pointer + j);
    --Lpointer->length;
    return OK;
}

修改指定位置的元素

/*修改指定位置的元素*/
St alist_mod(ALIST *Lpointer, const int i, MY_TYPE mod_e){
    if(!(i > -1 && i < Lpointer->length))
        return ERROR;
    *(Lpointer->head_pointer + i) = mod_e;
    return OK;
}

遍歷線性表

/*遍歷線性表*/
St alist_visit(const ALIST *Lpointer){
    switch(_MY_TYPE_){
    case 0:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d個元素是:%d\n", i, (int)*p++);
            break;
        }
    case 1:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d個元素是:%lf\n", i, (double)*p++);
            break;
        }
    case 2:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d個元素是:%f\n", i, (float)*p++);
            break;
        }
    case 3:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d個元素是:%c\n", i, (char)*p++);
            break;
        }
    default:
        printf("屬下無能\n");
        return ERROR;
    }
    return OK;
}

簡單的比較函數

/*compare--->equal*/
St cmp_equal(MY_TYPE e1, MY_TYPE e2){
    if(e1 == e2)
        return OK;
    else
        return ERROR;
}
/*compare--->less*/
St cmp_less(MY_TYPE e1, MY_TYPE e2){
    if(e1 < e2)
        return OK;
    else
        return ERROR;
}
/*compare--->more*/
St cmp_more(MY_TYPE e1, MY_TYPE e2){
    if(e1 > e2)
        return OK;
    else
        return ERROR;
}

測試操作順序表的基本功能

在main.中測試

#include "header.h"

int main()
{
    ALIST L = {0};
    init_alist(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n長度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    if(alist_empty(&L))
        printf("是否為空:OK\n");
    else
        printf("是否為空:NO\n");
    printf("------------------------------------------------------------------\n");
    printf("當前長度:%d\n", alist_length(&L));
    printf("------------------------------------------------------------------\n");
    alist_add(&L, 1);
    alist_add(&L, 2);
    alist_add(&L, 200);
    alist_add(&L, 34);
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    MY_TYPE e1;
    alist_delete(&L, 2, &e1);
    printf("被刪除的元素:%d\n", e1);
    printf("------------------------------------------------------------------\n");
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n長度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    alist_insert(&L, 2, 200);
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n長度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    MY_TYPE e2;
    if(next_elem(&L, 200, &e2))
        printf("200的後繼:%d\n", e2);
    else
        printf("後繼訪問失敗啦\n");
    printf("------------------------------------------------------------------\n");
    MY_TYPE e3;
    if(prior_elem(&L, 200, &e3))
        printf("200的前驅:%d\n", e3);
    else
        printf("前驅訪問失敗啦\n");
    printf("------------------------------------------------------------------\n");
    printf("%d\n", locate_elem(&L, 200, cmp_equal));
    printf("------------------------------------------------------------------\n");
    MY_TYPE e4;
    if(get_elem(&L, 4, &e4))
        printf("index=2:%d\n", e4);
    else
        printf("獲取元素失敗啦\n");
    printf("------------------------------------------------------------------\n");
    if(alist_mod(&L, 2, 300))
        alist_visit(&L);
    else
        printf("修改元素失敗啦\n");
    printf("------------------------------------------------------------------\n");
    if(alist_empty(&L))
        printf("是否為空:OK\n");
    else
        printf("是否為空:NO\n");
    printf("------------------------------------------------------------------\n");
    if(clear_alist(&L)){
        printf("清空成功\n");
        alist_visit(&L);
    }
    else
        printf("清空失敗\n");
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n長度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    if(destroy_alist(&L))
        printf("地址:%d\n長度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    else
        printf("銷毀失敗啦\n");
    printf("------------------------------------------------------------------\n");

    return 0;
}

測試結果

------------------------------------------------------------------
地址:6893504
長度:0
容量:3
------------------------------------------------------------------
是否為空:OK
------------------------------------------------------------------
當前長度:0
------------------------------------------------------------------
第1個元素是:1
第2個元素是:2
第3個元素是:200
第4個元素是:34
------------------------------------------------------------------
被刪除的元素:200
------------------------------------------------------------------
第1個元素是:1
第2個元素是:2
第3個元素是:34
------------------------------------------------------------------
地址:6893408
長度:3
容量:5
------------------------------------------------------------------
第1個元素是:1
第2個元素是:2
第3個元素是:200
第4個元素是:34
------------------------------------------------------------------
地址:6893408
長度:4
容量:5
------------------------------------------------------------------
200的後繼:34
------------------------------------------------------------------
200的前驅:2
------------------------------------------------------------------
200的位置:2
------------------------------------------------------------------
獲取元素失敗啦
------------------------------------------------------------------
第1個元素是:1
第2個元素是:2
第3個元素是:300
第4個元素是:34
------------------------------------------------------------------
是否為空:NO
------------------------------------------------------------------
清空成功
------------------------------------------------------------------
地址:6893408
長度:0
容量:5
------------------------------------------------------------------
地址:0
長度:0
容量:0
------------------------------------------------------------------

數據結構---線性表---順序存儲結構