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;
}
原文地址:https://www.cnblogs.com/xin1998/p/7739484.html