网站建设 招标书,慈溪机械加工网,网站时间轴,大兴网站制作今天学习了思成老师的数据结构实战教程 写了一个顺序表 插入和删除的操作 把源码共享给大家 一共包括list.c stu.h main.c list.h .h文件是头文件 需要引入 具体的功能我都已经在代码中写明了list.h代码如下#xff1a;//线性表的定义在头文件中实现#ifndef _LIST_H#define …今天学习了思成老师的数据结构实战教程 写了一个顺序表 插入和删除的操作 把源码共享给大家 一共包括list.c stu.h main.c list.h .h文件是头文件 需要引入 具体的功能我都已经在代码中写明了list.h代码如下//线性表的定义在头文件中实现#ifndef _LIST_H#define _LIST_H#define _LIST_INIT_SIZE 10#define _LIST_INCREME 10typedef struct{ElemType * elem;//首地址int length;int size;}LIST;LIST *InitList();void FreeList(LIST *l);int InsertList(LIST *l,int i,ElemType *e);int DeleteList(LIST *l,int i);#endiflist.c代码如下#include #include #include stu.h#include list.h//线性表的初始化,此时数据存储的大小的内存还未开辟 开辟的是list.h的大小LIST *InitList(){LIST *l(LIST *)malloc(sizeof(LIST));//判断开辟是否成功if(lNULL)exit(0);//开辟存储数据的内存的区域l-elem(ElemType *)malloc(_LIST_INIT_SIZE *sizeof(ElemType));//如果开辟成功的话就释放掉内存if(l-elemNULL){free(l);exit(0);}//有效长度赋初值为0l-length0;l-size_LIST_INIT_SIZE;return l;}//释放对区内存的函数void FreeList(LIST *l){//要先释放成员的空间free(l-elem);free(l);}//第一个参数要传得是插入哪一个线性表中去 i指位置int InsertList(LIST *l,int i,ElemType *e){//定义一些指针 指向相应的位置ElemType *pNULL,*qNULL,*newElemNULL;if(lNULL || eNULL)return 0;//i指的是第几个位置 不是下标if(i1||il-length1)return 0;//if有效长度大于最大的长度的时候 重新开辟一块内存if(l-lengthl-size){newElemrealloc(l-elem,(l-size_LIST_INCREME)*sizeof(ElemType));if(newElemNULL)return 0;l-elemnewElem;l-size_LIST_INCREME;}//q指向插入的位置 i-1代表下标ql-elem[i-1];//指向最后一个有效的数据元素for(p(l-elem[l-length-1]);pq;p--)*(p1)*p;*q*e;l-length;return 1;}int DeleteList(LIST *l,int i){ElemType *pNULL,*qNULL;if(lNULL)return 0;if(i1|| il-length)return 0;pl-elem[i-1];ql-elem[l-length-1];for(;p*p*(p1);--l-length;return 1;}stu.h代码如下#ifndef _STU_H#define _STU_H//定义一个学生的结构体typedef struct{char sno[4];char name[21];char sex[3];int score;}ElemType;#endifmain.c代码如下#include #include stu.h#include list.hElemType stu[3]{{S101,张三,男,80},{S102,小红,女,75},{S103,王五,男,90},};void main(){int i;LIST *listNULL;//通过函数初始化空间listInitList();for(i0;i3;i)InsertList(list,1,stu[i]);DeleteList(list,2);FreeList(list);}