zoukankan      html  css  js  c++  java
  • 线性表的表示及实现

    表示

    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("}
    ");
        else
            printf("}
    ");
    }
    
    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
    ", x);
        printf("updated list[4] to 100
    ");
        SetSqlist(list, 4, 100);
        GetSqlist(list, 4, &x);
        printf("now list[4] = %d
    ", x);
        iterate_list(list);
            
        printf("removed list[4]
    ");
        DeleteSqlist(list, 4);
        GetSqlist(list, 4, &x);
        printf("now list[4] = %d
    ", x);
        printf("and total number of list is %d
    ", LengthSqlist(list));
        
        iterate_list(list);
        
        ClearSqlist(list);
        printf("after clear, total number of list is %d
    ", 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 = {}
  • 相关阅读:
    在centos7.4上安装confluence-6.7.1
    用rpm包在centos7.4上安装mysql-5.7.29-1.el7.x86_64
    Linux系统运维笔记,CentOS 7.4防火墙配置
    Linux系统运维笔记(6),CentOS 7.6双网卡路由配置
    Linux系统运维笔记(五),CentOS 6.4安装java程序
    Java 构造 BSON 数据类型
    Linux系统运维笔记(四),CentOS 6.4安装 MongoDB
    设计模式(5)原型模式(Prototype)
    设计模式(4)建造者模式/生成器模式(Builder)
    设计模式(3)抽象工厂模式(Abstract Factory)
  • 原文地址:https://www.cnblogs.com/vsyf/p/4912162.html
Copyright © 2011-2022 走看看