在C语言中用动态分配的一维数组描述。
//---线性表的动态分配顺序存储结构---//
#include<math.h>
#define LIST_INIT_SIZE 100 //初始大小
#define LISTINCREMENT 10 //分配增量
typedef struct
{ char name[20];
char stuno[10];
int age;
int score;
}ElemType;
typedef struct{
ElemType *elem; //首地址
int length; //长度
int listsize; //当前最多可存放的元素个数
}SqList;
typedef int status;
SqList InitList_Sq(SqList &L){
//构造一个空的线性表,返回构造结果:成功或失败。
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*Sizeof(ElemType));
if(!L.elem ) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
Return OK;
}
status ListInsert_Sq(SqList &L, int i, ElemType e){
ElemType *newbase, *q, *p;
if(i<=0 || i>L.length +1) return ERROR;
if(L.length>=L.listsize){
newbase=(ElemType*)realloc(L.elem.(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(newbase==NULL)
return OVERFLOW;
L.elem=newbase;
L.listsize=L.listsize+LISTINCERMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]); p>=q; --p)
*(p+1)= *p;
*q=e;
L.length++;
return OK;
}*/
Status ListDelete_Sq(SqList &L, int i, ElemType &e){
ElemType *p, *q;
if((i<1) || (i>L.length)) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
p++;
for(p; p<=q; p++) *(p-1)= *p;
L.length--;
return OK;
}
main()
{ ElemType *p,*q;
int i,e;
scanf("%d%d",&i,&e);
status InitList_Sq(p);
status ListInsert_Sq(p,i,e);
status ListDelete_Sq(SqList &L, int i, ElemType &e);
}
并附上关键的说明~!~
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课