linear list 线性表的 顺序存储方式 C语言实现

linear list 线性表的 顺序存储方式实现 对顺序存储结构实现线性表查询、插入、删除过程时间复杂度分析
插入、删除、按元素值查询推导同理、同结果
linear list 线性表的 顺序存储方式 C语言实现
文章图片

按位查询O(1)
基于顺序存储方式实现线性表的代码实现

#include #include #define Elemtype int #define Init_num 20 #define Init_cur 10 #define Init_value 0//linear list 顺序存储体基本构造(区别于数组方式) //data区域将会获得连续的存储空间,可以直接使用下标访问(区别于链表,不要看到指针就想到链表) typedef struct { Elemtype *data; //(仅为了实现动态内存空间的分配使用了指针,而不是用指针访问) int current_max_length; int list_max_length; }linear_list; void initial_list(linear_list &L)//顺序存储结构的初始化方法 { int i = 0; L.data = https://www.it610.com/article/(Elemtype*)malloc(sizeof(Elemtype)*Init_num-1); for (i=0; i= 0) { for(temp_loc; temp_loc+1 <= L.current_max_length; temp_loc++) { L.data[temp_loc] = L.data[temp_loc+1]; } L.current_max_length = L.current_max_length-1; printf("delete operation succeed!\n"); for(int i=0 ; i <= L.current_max_length ; i++) { printf("%d ",L.data[i]); } printf("\ndone!\n"); } else { printf("delete operation failed,linear list is empty!\n"); } }//销毁操作是对结构体中的Elemtype类型指针所指向的内存空间进行释放,而不是将结构体L整个进行释放 void release_list(linear_list &L) { //free操作非空才有效 if(L.data != NULL) { free(L.data); L.data= https://www.it610.com/article/NULL; L.current_max_length = -1; L.list_max_length = -1; printf("linear list has been released!\n"); } else { printf("pointer is no defeined!\n"); }}void insert_element(linear_list &L,Elemtype new_element,int location) { //顺序表没有多余空位、或者非法输入 location = location -1; //顺序表内以0开始计数,线性表逻辑结构中以1开始 if(L.list_max_length == L.current_max_length || location < 0) { printf("insert operation fail!\nno extra space\n"); } else if (L.list_max_length > L.current_max_length) { //应对两类情况 //state_1 直接在顺序表末尾添加 if(location == L.current_max_length+1) { L.data[location] = new_element; }//state_2 在顺序表0-n以内位置插入 else if (location <= L.current_max_length) { int end_marke = L.current_max_length; for(int i=location; i

程序运行结果示例
linear list 线性表的 顺序存储方式 C语言实现
文章图片

代码地址 http://www.dooccn.com/c/#id/6... 顺序表物理存储形式示例
【linear list 线性表的 顺序存储方式 C语言实现】linear list 线性表的 顺序存储方式 C语言实现
文章图片

    推荐阅读