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;
    }
  • 相关阅读:
    一个把数据转化成Excel导出的程序 python Django
    Ubuntu常用命令
    Git 常用命令 和 安装
    strick-footer 粘边布局
    Django
    CSS基础 和 font字体、背景属性连写 与 清除浮动方法
    MySQL 40题练习题和答案
    JS(ES6)、Vue.js、node.js
    ORM框架 和 面向对象编程
    MySQL基本指令3 和 索引 、分页
  • 原文地址:https://www.cnblogs.com/xin1998/p/7739484.html
Copyright © 2011-2022 走看看