線性表的順序存儲

/*
C++學習筆記07——線性表的順序存儲
2017.07.03
*/

  • seqlist.h
#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif  //__MY_SEQLIST_H__
  • seqlist.c
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"

typedef struct _tag_SeqList
{
    int capacity;
    int length;
    unsigned int *node;
}TSeqList;

//typedef void SeqList;//把void 重命名爲  SeqList
//typedef void SeqListNode;

/*
void* SeqList_Create(int capacity)
{
    TSeqList *ret = NULL;
    ret = (TSeqList *)malloc(sizeof(TSeqList));
    if (ret == NULL)
    {
        return NULL;  
    }
    ret->capacity = capacity;
    ret->node = (unsigned int *)malloc(sizeof(int *)*capacity);
    if (ret->node == NULL)
    {
        return NULL;
    }
    ret->length = 0;


    return ret;
}
*/

void* SeqList_Create(int capacity)
{
    TSeqList *ret = NULL;
    if (capacity <= 0)
    {
        return NULL;

    }
    ret = (TSeqList *)malloc(sizeof(TSeqList) + sizeof(unsigned int )*capacity);
    if (ret == NULL)
    {
        return NULL;
    }
    ret->capacity = capacity;
    ret->node =(unsigned int *) (ret + 1);//ret+1表示下一個數組
    if (ret->node == NULL)
    {
        return NULL;
    }
    ret->length = 0;


    return ret;
}

void SeqList_Destroy(SeqList* list)
{
    if (list == NULL)
    {
        return;
    }
    free(list);
    return ;
}

void SeqList_Clear(SeqList* list)
{
    TSeqList *tlist = (TSeqList *)list;
    if (list == NULL)
    {
        return;
    }
    tlist->length = 0;

    return ;
}

int SeqList_Length(SeqList* list)
{
    TSeqList *tlist = list;
    if (list == NULL)
    {
        return -1;
    }
    return tlist->length;
}

int SeqList_Capacity(SeqList* list)
{
    TSeqList *tlist = list;
    if (list == NULL)
    {
        return -1;
    }
    return tlist->capacity;

}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    int i = 0;
    TSeqList *tlist = list;

    if (list == NULL || node == NULL)
    {
        return -1;
    }
    if (pos<0 || pos >tlist->capacity)
    {
        return -2;
    }

    if (tlist->length >= tlist->capacity)
    {
        return -3;
    }
    //容錯
    if (pos > tlist->length)
    {
        pos = tlist->length;
    }
    //插入算法
    //從插入的位置後移元素
    //注意length 能表示出現在數組的最後元素位置
    //最後元素的下標爲tlise->node[length-1];
    for (i = tlist->length; i > pos; i--)
    {
        tlist->node[i] = tlist->node[i - 1];
    }
    //在pos位置插入元素
    tlist->node[pos] = (unsigned int)node;
    tlist->length ++;
    return 0;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{
    int i = 0;
    TSeqList *tlist = list;
    /*if (list == NULL || pos<0 || pos>=tlist->length)*/
    if (list == NULL || pos<0 || pos > tlist->length)
    {
        return NULL;
    }

    return (SeqListNode *)tlist->node[pos];
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
    int i = 0;
    TSeqList *tlist = list;
    SeqListNode * ret = NULL;
    if (list == NULL || pos<0 || pos > tlist->length)
    {
        return NULL;
    }
    ret = (SeqListNode *)tlist->node[pos];
    for (i = pos + 1; i < tlist->length;i++)
    {
        tlist->node[i - 1] = tlist->node[i];
    }
    tlist->length --;
    return ret;
}
  • 測試:
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"


typedef struct _Teacher
{
    char name[64];
    int age;
    int buf;
}Teacher;

void main()
{   
    int ret = 0, i = 0;
    Teacher t1, t2, t3;
    SeqList* list = NULL;

    t1.age = 10;
    t2.age = 20;
    t3.age = 30;

    list = SeqList_Create(10);


    ////仔細思考:業務數據 和 鏈表算法(底層庫)是如何分離的。。。。。。
    ////業務數據結點的管理(內存的生命週期)甩給了上層應用(業務模型)
    ret = SeqList_Insert(list, (SeqListNode*)&t1, 0);
    ret = SeqList_Insert(list, (SeqListNode*)&t2, 0);
    ret = SeqList_Insert(list, (SeqListNode*)&t3, 0);


    //循環遍歷
    for (i = 0; i<SeqList_Length(list); i++)
    {
        Teacher *tmp = (Teacher *)SeqList_Get(list, i);
        printf("age:%d \n", tmp->age);
    }


    //循環刪除
    for (i = 0; i<SeqList_Length(list); i++)
    {
        SeqList_Delete(list, 0);
    }

    SeqList_Destroy(list);
    system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章