데이터 구조의 순서 표 (2)

12266 단어 장쑤 성
이것 은 이전 에 남 은 부분 이다.
#include 
#include "SeqList.h"
#include 

SeqList *Create_List()
{
    //       
    SeqList* s = (SeqList*)malloc(sizeof(SeqList)/sizeof(char));
    if (NULL == s)
    {
        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 (NULL == s)
    {
        return;
    }

    //       
    free (s->list);

    //     
    free (s);   
}

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

    //          
    int new_size = sizeof(SeqData)/sizeof(char) * 
    (INIT_SIZE + INCRESS_SIZE);
    SeqData* tmp = (SeqData*)realloc(s->list, new_size);

    if (NULL == tmp)
    {
        return FALSE;
    }
    s->list = tmp;
    s->max_len += INCRESS_SIZE;

    return TRUE;    
}

int Insert_Last(SeqList* s, SeqData data)
{
    if (NULL == s)
    {
        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 (NULL == s)
    {
        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 (NULL == s || 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 (NULL == s || 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 (NULL == s)
    {
        return FALSE;
    }

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

int Reverse_List(SeqList* s)
{
    if (NULL == s)
    {
        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 (NULL == s1 || NULL == s2)
    {
        return NULL;
    }
    SeqList* s3 = (SeqList*)malloc(sizeof(SeqList)/sizeof(char));
    if (NULL == s3)
    {
        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++];
    }

    return s3;
}

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

    int i;
    int count = 0;
    for (i = 0; i < s->len; i++)
    {
        if (0 == i % 4)
        {
            printf ("
"
); } printf ("%8d", s->list[i]); } printf ("
"
); }

좋은 웹페이지 즐겨찾기