循序表的(SeqList.c)(函数实现)

#include "SeqList.h"
#include 
#include 

SeqList *Create_List()
{
    // 创建一个顺序表
    SeqList* s = (SeqList*)malloc(sizeof(SeqList)/sizeof(char));
    if (s == NULL)
    {
        return NULL;
    }

    // 初始化顺序的成员
    // 为顺序表分配存储空间
    s->list = (SeqData *)malloc(sizeof(SeqData)/sizeof(char)*INIT_SIZE);
    if (s->list == NULL)
    {
        free(s);  // 创建存储空间失败,先释放顺序表结点
        return NULL;
    }
    s->max_len = INIT_SIZE;
    s->len = 0;

    return s;
}

void Destroy(SeqList *s)
{
    if (s == NULL)
        return;

    // 先释放存储空间
    free (s->list);

    // 释放顺序表
    free (s);
}

int AgainMalloc(SeqList *s)
{
    if (s == NULL)
        return FALSE;

    // 新申请空间的字节数
    int new_size = sizeof(SeqData)/sizeof(char)*(INIT_SIZE + INCRESS_SIZE);
    SeqData* tmp = (SeqData*)realloc(s->list, new_size);
    if (tmp == NULL)
    {
        return FALSE;
    }
    s->list = tmp;
    s->max_len += INCRESS_SIZE;


    return TRUE;
}

int Insert_Last(SeqList *s, SeqData data)
{
    if (s == NULL)
    {
        return FALSE;
    }

    // 检测顺序表是否存满
    if (s->len == s->max_len)
    {
        // 扩展空间
        if (AgainMalloc(s) != TRUE)
        {
            return FALSE;
        }
    }

    // 插入数据
    s->list[s->len] = data;
    s->len++;

    return TRUE;
}

int Insert_Head(SeqList *s, SeqData data)
{
    if (s == NULL)
        return FALSE;

    // 检测顺序表是否存满
    if (s->len == s->max_len)
    {
        if (AgainMalloc(s) != TRUE)
        {
            return FALSE;
        }
    }

    // 进行移位
    int i;
    for (i = s->len-1; i >= 0; i--)
    {
        s->list[i+1] = s->list[i];
    }

    s->list[0] = data;
    s->len++;

    return TRUE;
}

int Insert_Pos(SeqList *s, int pos, SeqData data)
{
    if (s == NULL || pos < 0 || pos > s->len)
        return FALSE;

    // 检测顺序表是否存满
    if (s->len == s->max_len)
    {
        if (AgainMalloc(s) != TRUE)
        {
            return FALSE;
        }
    }

    // 进行移位
    int i;
    for (i = s->len-1; i >= pos; i--)
    {
        s->list[i+1] = s->list[i];
    }

    s->list[pos] = data;
    s->len++;

    return TRUE;
}

int GetData(SeqList *s, int pos, SeqData *x)
{
    if (s == NULL || pos < 0 || pos >= s->len || x == NULL)
        return FALSE;

    *x = s->list[pos];

    return TRUE;
}

int FindData(SeqList *s, SeqData data, SeqData *x)
{
    if (s == NULL || x == NULL)
        return FALSE;

    int i;
    int flag = 0;
    for (i = 0; i < s->len; i++)
    {
        if (s->list[i] == data)
        {
            *x = i;
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        return FALSE;

    return TRUE;
}

int Delete_Pos(SeqList *s, int pos)
{
    if (s == NULL || pos < 0 || pos >= s->len)
    {
        return FALSE;
    }

    // 移动元素
    int i;
    for (i = pos; i < s->len-1; i++)
    {
        s->list[i] = s->list[i+1];
    }

    s->len--;

    return TRUE;
}


int Delete_Data(SeqList *s, SeqData data)
{
    if (s == NULL)
        return FALSE;

    int index = -1;
    if (FindData(s, data, &index) == FALSE)
        return FALSE;

    return Delete_Pos(s, index);
}

int Reverse_List(SeqList *s)
{
    if (s == NULL)
        return FALSE;

    int min = 0;
    int max = s->len - 1;
    SeqData tmp;
    while (min < max)
    {
        tmp = s->list[min];
        s->list[min++] = s->list[max];
        s->list[max--] = tmp;
    }

    return TRUE;
}

SeqList * Merge_List(SeqList *s1, SeqList *s2)
{
    if (s1 == NULL || s2 == NULL)
        return NULL;

    SeqList* s3 = (SeqList*)malloc(sizeof(SeqList)/sizeof(char));
    if (s3 == NULL)
        return NULL;

    // 为s3分配存储空间
    int size = sizeof(SeqData)/sizeof(char)*(s1->len + s2->len);
    s3->list = (SeqData*)malloc(size);
    s3->len  = s1->len + s2->len;
    s3->max_len = s3->len;

    int i = 0;     // 遍历 s1
    int j = 0;     // 遍历 s2
    int k = 0;     // 遍历 s3

    while (i < s1->len && j < s2->len)
    {
        if (s1->list[i] < s2->list[j])
        {
            s3->list[k++] = s1->list[i++];
        }
        else
        {
            s3->list[k++] = s2->list[j++];
        }
    }

    while (i < s1->len)
    {
        s3->list[k++] = s1->list[i++];
    }

    while (j < s2->len)
    {
        s3->list[k++] = s2->list[j++];
    }

    return s3;
}

void Display(SeqList *s)
{
    if (s == NULL)
        return;

    int i;
    int count = 0;
    for (i = 0; i < s->len; i++)
    {
        if (i % 4 == 0)
            printf ("\n");

        printf ("%8d", s->list[i]);
    }

    printf ("\n");
}

你可能感兴趣的:(循序表的(SeqList.c)(函数实现))