zoukankan      html  css  js  c++  java
  • c语言实现基本的数据结构(一) 线性表

    #include <stdio.h>
    #include <tchar.h>
    #include <stdlib.h>
    
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10
    
    
    // TODO: 在此处引用程序需要的其他头文件
    //
    typedef struct{
    int *elem;
    int length;//当前长度
    int listsize;//当前分配的存储容量
    }SqList;
    
    //新建线性表
    bool Init_List(SqList* L){
    L->elem = (int*)malloc(LIST_INIT_SIZE*sizeof(int));
    if (!L->elem) return false;
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return true;
    }
    //销毁线性表
    bool Destroy_List(SqList* L){
    if (L){
    free(L);
    L = NULL;
    return true;
    }
    else
    return false;
    }
    //表末新增一个元素
    bool Append_List(SqList* L,int value){
    if (L->length >= L->listsize){
    L->elem = (int*)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(int));
    L->listsize += LISTINCREMENT;
    }
    L->length++;
    L->elem[L->length] = value;
    return true;
    }
    //打印线性表
    void Print_List(SqList L){
    for (int i = 1; i<=L.length;i++){
    printf("%d", L.elem[i]);
    }
    }
    //指定位置locate插入值valu
    bool Insert_List(SqList* L, int locate, int value){
    if (L->length >= L->listsize){
    L->elem = (int*)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(int));
    L->listsize += LISTINCREMENT;
    }
    for (int i = L->length; i >= locate; i--){
    L->elem[i + 1] = L->elem[i];
    }
    L->elem[locate] = value;
    L->length++;
    return true;
    }
    //删除指定位置locate元素
    bool Delete_List(SqList* L, int locate){
    for (int i = locate; i<L->length; i++){
    L->elem[i] = L->elem[i + 1];
    }
    L->elem[L->length] = NULL;
    L->length--;
    return true;
    }
    //清空线性表
    bool Clear_List(SqList* L){
    while (L->length){
    L->elem[L->length] = NULL;
    L->length--;
    }
    return true;
    }
  • 相关阅读:
    postgresql批量插入copy_from()的使用
    Fabric SSH链接时关于找不到主机的问题
    Python多线程获取返回值
    网页正文提取,降噪的实现(readability/Document)
    HTML标签参考手册
    javascript获取当前日期和时间
    2017易观OLAP算法大赛
    Apache DolphinScheduler 诞生记
    Apache DolphinScheduler(海豚调度)
    开源分布式工作流任务调度系统Easy Scheduler Release 1.0.2发布
  • 原文地址:https://www.cnblogs.com/xin1998/p/7739484.html
Copyright © 2011-2022 走看看