首页 > 其他 > 详细

线性表的表示及实现

时间:2015-11-18 12:10:35      阅读:265      评论:0      收藏:0      [点我收藏+]

表示

typedef int data_t;

#define MAX 100

typedef struct {
    data_t    data[MAX];
    int    last;     /* pointer to the position 
             * in the array ‘data‘ where 
             * stores the last element of
             * the list
             */
} seqlist_t;

实现

//创建
seqlist_t *CreateEmptySqlist()
{
    seqlist_t *list;
    list = (seqlist_t *)malloc(sizeof(seqlist_t));
    if (NULL == list)
        return NULL;
    list->last = -1;
    return list;
}
//销毁
int DestroySqlist(seqlist_t *list)
{
    if (NULL != list) {
        free(list);
        return 0;
    } else {
        return -1;
    }
}
//清空
int ClearSqlist(seqlist_t *list)
{
    if (NULL != list) {
        list->last = -1;
        return 0;
    } else {
        return -1;
    }
}
//测试是否为空
int EmptySqlist(seqlist_t *list)
{
    if (NULL != list) {
        if (list->last == -1)
            return 0;
        else
            return 1;
    } else {
        return -1;
    }
}
//测试是否为满
int FullSqlist(seqlist_t *list)
{
    if (NULL != list) {
        if (list->last == (MAX - 1))
            return 0;
        else
            return 1;
    } else {
        return -1;
    }
}
//当前长度
int LengthSqlist(seqlist_t *list)
{
    if (NULL != list)
        return (list->last + 1);
    else
        return -1;
    
}
//
int GetSqlist(seqlist_t *list, int at, data_t *x)
{
    if (NULL != list) {
        if ((at < 0) || (at > list->last))
            return -2;
        else {
            *x = list->data[at];
            return 0;
        }
            
    } else {
        return -1;
    }
}
//
int SetSqlist(seqlist_t *list, int at, data_t x)
{
    if (NULL != list) {
        if ((at < 0) || (at > list->last))
            return -2;
        else {
            list->data[at] = x;
            return 0;
        }
    } else {
        return -1;
    }
}
//
int InsertSqlist(seqlist_t *list, int at, data_t x)
{
    if (NULL != list) {
        if (at < 0) return -1;
        if (FullSqlist) return -2;
        if (at > list->last)
            list->data[list->last + 1] = x;
        else {
            int i = 0;
            for (i = list->last + 1; i >= at; i--) {
                list->data[i] = list->data[i - 1];
            }
            list->data[at] = x;
            list->last++;
            return 0;
        }

    } else {
        return -1;
    }
}
//
int DeleteSqlist(seqlist_t *list, int at)
{
    if (NULL != list) {
        if ((at < 0) || (at > list->last))
            return 1;
        else {
            int i = 0;
            for (i = at; i < list->last;) {
                list->data[i] = list->data[i++];
            }
            list->last--;
            return 0;
        }
    } else {
        return -1;
    }
}

 测试代码

void iterate_list(seqlist_t *list)
{
    int i;
    
    printf("list.last = %d, list = {", list->last);
    
    for (i = -1; i < list->last;) {
        printf("%d,", list->data[++i]);
    }
        
    if (LengthSqlist(list) > 0)
        printf("\b}\n");
    else
        printf("}\n");
}

int main(int argc, char *argv[])
{
    int i;
    data_t a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    data_t x;
    
    seqlist_t *list;
    
    list = CreateEmptySqlist();
    
    if (NULL == list) return -1;
        
    for (i = 0; i < 10; i++) {
        if (InsertSqlist(list, i, a[i]) < 0)
            break;
    }
    iterate_list(list);
    
    GetSqlist(list, 4, &x);
    printf("list[4] = %d\n", x);
    printf("updated list[4] to 100\n");
    SetSqlist(list, 4, 100);
    GetSqlist(list, 4, &x);
    printf("now list[4] = %d\n", x);
    iterate_list(list);
        
    printf("removed list[4]\n");
    DeleteSqlist(list, 4);
    GetSqlist(list, 4, &x);
    printf("now list[4] = %d\n", x);
    printf("and total number of list is %d\n", LengthSqlist(list));
    
    iterate_list(list);
    
    ClearSqlist(list);
    printf("after clear, total number of list is %d\n", LengthSqlist(list));

    iterate_list(list);
        
    DestroySqlist(list);
    
    return 0;
}

结果

list.last = 9, list = {2,4,6,8,10,12,14,16,18,20}
list[4] = 10
updated list[4] to 100
now list[4] = 100
list.last = 9, list = {2,4,6,8,100,12,14,16,18,20}
removed list[4]
now list[4] = 12
and total number of list is 9
list.last = 8, list = {2,4,6,8,12,14,16,18,20}
after clear, total number of list is 0
list.last = -1, list = {}

 

线性表的表示及实现

原文:http://www.cnblogs.com/vsyf/p/4912162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!