正在进行安全检测...
发布时间:1714415460 来源:文档文库
小
中
大
字号:
数据结构
第一、二次上机: #include #include #include
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2
#define list_init_size 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
typedef int Status; typedef int ElemType;
typedef struct{ ElemType *elem; //存储空间基址 int length; //当前长度 int listsize; //当前分配的存储容量(以sizeof(ElemType为单位) }SqList;
Status InitList_Sq(SqList &L{ //构造一个空的线性表L L.elem =(ElemType * malloc(list_init_size*sizeof(ElemType; if(!L.elem exit(OVERFLOW;//存储分配失败 L.length =0; //空表长度为0 L.listsize =list_init_size;//初始存储容量 return OK; }//Initlist_Sq
Status ListInsert_Sq(SqList &L,int i,ElemType e{ //在顺序线性表L中第i个位置之前插入新的元素e, //i的合法值为1<=i<=ListLength_Sq(L+1 ElemType *p,*q,*newbase; //定义指针 if(i<1||i>L.length +1 return ERROR; //i值不合法 if(L.length >=L.listsize { //当前存储空间已满,增加分配 newbase=(ElemType * realloc(L.elem ,(L.listsize +LISTINCREMENT*sizeof(ElemType;
if(!newbaseexit(OVERFLOW; //存储分配失败 L.elem =newbase; //新基址 L.listsize +=LISTINCREMENT; //增加存储容量 } q=&(L.elem [i-1]; //q为插入位置 for(p=&(L.elem [L.length -1];p>=q;--p *(p+1=*p; //插入位置及之后的元素右移 *q=e;