1. 程式人生 > >【資料結構】靜態、動態順序表

【資料結構】靜態、動態順序表

靜態順序表

資料型別的定義

#define MAX_SIZE 100
typedef int DataType;
typedef struct SeqList
{
    DataType data[MAX_SIZE];
    int size;
}SeqList;
順序表的初始化和銷燬
//初始化
void SeqInit(SeqList *pSeq)
{
    assert(pSeq != NULL);
    memset(pSeq,0,MAX_SIZE*sizeof(DataType));
    pSeq->size = 0;
}
//銷燬
void SeqDestory(SeqList *pSeq)
{
    assert(pSeq != NULL);
    memset(pSeq,0,MAX_SIZE*sizeof(DataType));
    pSeq->size = 0;
}
列印順序表
void SeqShow(SeqList *pSeq)
{
    int i = 0;
    assert(pSeq != NULL);
    for(i=0; i<pSeq->size; i++)
    {
        printf("%d ",pSeq->data[i]);
    }
    printf("\n");
}
查詢
int Find(SeqList *pSeq, DataType num)
{
    int i = 0;
    assert(pSeq != NULL);
    for(i=0; i<pSeq->size; i++)
    {
        if(pSeq->data[i]==num)
        {
            return i;
        }
    }
    return -1;
}
二分查詢
int BinarySearch(SeqList *pSeq, DataType d)
{
    int pos = 0;
    int left = 0;
    int right = pSeq->size;
    while(left<right)
    {
        int mid = (right-left)/2+left;
        if(d<pSeq->data[mid])
        {
            right = mid - 1;
        }
        else if(d>pSeq->data[mid])
        {
            left = mid+1;
        }
        else
        {
            pos = mid;
            break;
        }
    }
    if(left>right)
        return -1;
    return pos;
}
增加資料
尾插
void PushBack(SeqList *pSeq, DataType num)
{
    assert(pSeq != NULL);
    assert(pSeq->size != MAX_SIZE);
    pSeq->data[pSeq->size] = num;
    pSeq->size++;
}
頭插
void PushFront(SeqList *pSeq, DataType num)
{
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    assert(pSeq->size < MAX_SIZE);
#if 1
    //i 含義 資料下標 [size-1,0]
    for(i=pSeq->size-1; i>=0; i--)
    {
        pSeq->data[i+1] = pSeq->data[i];
    }
    pSeq->data[i+1] = num;
    pSeq->size++;
#else
    //j 含義 空間下標 [size,0)
    for(j=pSeq->size; j>0; j--)
    {
        pSeq->data[j] = pSeq->data[j-1];
    }
    pSeq->data[j] = num;
    pSeq->size++;
#endif
}
在指定位置插入
void Insert(SeqList *pSeq, DataType num, DataType d)
{
    int pos = 0;
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    assert(pSeq->size < MAX_SIZE);
    pos = Find(pSeq,num);
#if 1
    //i 含義 資料下標 [size-1,pos]
    for(i=pSeq->size-1; i>=pos; i--)
    {
        pSeq->data[i+1] = pSeq->data[i];
    }
    pSeq->data[pos+1] = d;
    pSeq->size++;
#else
    //j 含義 空間下標 [size,pos)
    for(j=pSeq->size; j>pos; j--)
    {
        pSeq->data[j] = pSeq->data[j-1];
    }
    pSeq->[pos] = d;
    pSeq->size++;
#endif
}
刪除資料
尾刪
void PopBack(SeqList *pSeq)
{
    assert(pSeq != NULL);
    assert(pSeq->size != 0);
    pSeq->size--;
}
頭刪
void PopFront(SeqList *pSeq)
{
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    assert(pSeq->size != 0);
#if 1
    //i 含義 資料下標 [0,size-2]
    for(i=0; i<=pSeq->size-2; i++)
    {
        pSeq->data[i] = pSeq->data[i+1];
    }
    pSeq->size--;
#else
    //j 含義 空間下標 [1,size)
    for(j=1; j<pSeq->size; j++)
    {
        pSeq->data[j-1] = pSeq->data[j];
    }
    pSeq->size--;
#endif
}
指定位置刪除(下標刪除)
void Erase(SeqList *pSeq, int pos)
{
    int i = 0;
    assert(pSeq != NULL);
    assert(pSeq->size != 0);
    for(i=pos+1; i<pSeq->size; i++)
    {
        pSeq->data[i-1] = pSeq->data[i];
    }
    pSeq->size--;
}
指定元素刪除(資料刪除)
void Remove(SeqList *pSeq, DataType d)
{
    int i = 0;
    int pos = 0;
    assert(pSeq != NULL);
    assert(pSeq->size != 0);
    pos = Find(pSeq, d);
    if(pos == -1)
    {
        printf("沒有找到該元素\n");
        return;
    }
    else
    {
        for(i=pos; i<pSeq->size; i++)
        {
            pSeq->data[i] = pSeq->data[i+1];
        }
        pSeq->size--;
    }
}
刪除所有指定元素
void RemoveAll(SeqList *pSeq, DataType d)
{
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    assert(pSeq->size>0 && pSeq->size<MAX_SIZE);
    for(i=0,j=0; i<pSeq->size; i++)
    {
        if(pSeq->data[i]!=d)
        {
            pSeq->data[j] = pSeq->data[i];
            j++;
        }
    }
    pSeq->size = j;
}
順序表屬性
//檢視是否為空屬性
int Empty(SeqList *pSeq)
{
    assert(pSeq != NULL);
    if(pSeq->size == 0)
        return 0;
    else
        return -1;
}
//檢視順序表是否滿了
int Full(SeqList *pSeq)
{
    assert(pSeq != NULL);
    if(pSeq->size == MAX_SIZE)
        return 0;
    else
        return -1;
}
//檢視順序表長度
int Size(SeqList *pSeq)
{
    return pSeq->size;
}
排序
氣泡排序
void BubbleSort(SeqList *pSeq)
{
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    for(i=0; i<pSeq->size-1; i++)
    {
        for(j=i+1; j<pSeq->size; j++)
        {
            if(pSeq->data[i]>pSeq->data[j])
            {
                int tmp = pSeq->data[i];
                pSeq->data[i] = pSeq->data[j];
                pSeq->data[j] = tmp;
            }
        }
    }
}
選擇排序
void SelectSort(SeqList *pSeq)
{
    int i = 0;
    int minSpace = 0;
    int maxSpace = 0;
    int max = 0;
    int min = 0;
    int ret1 = 0;
    int ret2 = 0;
    assert(pSeq != NULL);
    for(i=0; i<=pSeq->size; i++)
    {
        min = pSeq->data[minSpace];
        max = pSeq->data[maxSpace];
        for(minSpace = i,maxSpace = i; minSpace<pSeq->size; minSpace++,maxSpace++)
        {
            if(pSeq->data[minSpace]<min)
            {
                int tmp = pSeq->data[minSpace];
                pSeq->data[minSpace] = min;
                min = tmp;
            }
            if(pSeq->data[maxSpace]>max)
            {
                int tmp = pSeq->data[maxSpace];
                pSeq->data[maxSpace] = max;
                max = tmp;
            }
        }
        ret1 = max;
        ret2 = min;
        pSeq->data[minSpace] = ret2;
        pSeq->data[maxSpace] = ret1;
    }
}

動態順序表

資料型別的定義

#define CAP 5
typedef int DataType;

typedef struct SeqList
{
    DataType *array;
    int size;
    int capacity;
}SeqList;
順序表的初始化和銷燬
//初始化
void InitSeqList(SeqList *ps)
{
    assert(ps);
    ps->capacity = CAP;
    ps->size = 0;
    ps->array = (DataType *)malloc(sizeof(DataType)*CAP);
    assert(ps->array);
}

//銷燬
void DestroySeqList(SeqList *ps)
{
    assert(ps);
    free(ps->array);
    ps->capacity = 0;
    ps->size = 0;
}
順序表的列印
void PrintSeqList(const SeqList *ps)
{
    int i = 0;
    assert(ps);
    for(i=0; i<ps->size; i++)
    {
        printf("%d ",ps->array[i]);
    }
    printf("\n");
}
增加資料

因為動態順序表同靜態順序表思路一致,由於考慮到擴容問題,在此列出增加資料中的尾插:

//判斷是否需要擴容
void SeqIfExpend(SeqList *ps)
{
    int i = 0;
    DataType *new_array = NULL;
    DataType *result = NULL;
    assert(ps);
    //不需要擴容
    if(ps->size<ps->capacity)
    {
        return ;
    }
    //需要擴容
    new_array = (DataType *)malloc((ps->capacity+CAP)*sizeof(DataType));
    if(new_array != NULL)
    {
        result = new_array;
        for(i=0; i<ps->size; i++)
        {
            result[i] = ps->array[i];
        }
        ps->array = result;
        ps->capacity = (ps->capacity+CAP)*sizeof(DataType);
    }

}
//尾插
void PushBack(SeqList *ps, DataType d)
{
    assert(ps);
    SeqIfExpend(ps);
    ps->array[ps->size] = d;
    ps->size++;
}

靜態與動態的比較

這裡寫圖片描述


上述內容均為學習過程總結,如有不足之處,請指正