C数据结构顺序表的实现。

来源:百度知道 编辑:UC知道 时间:2024/06/14 11:54:30
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LIST_INIT_SIZE 100
#define LISTCONTENTADD 10
typedef int elemType;
typedef struct{
elemType *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList *l){
printf("成功");
l->elem= (elemType *)malloc(LIST_INIT_SIZE*sizeof(elemType));
l->length=0;
l->listsize=LIST_INIT_SIZE;
}
void ListInsert_Sq(SqList *l,int locate,int e){
int *q,*p;
if(locate<1||locate>l->listsize){
printf("出现错误\n");
exit(0);
}
if(l->length>=l->listsize)
l->elem=(elemType *)realloc(l->elem,(l->listsize+LISTCONTENTADD)*sizeof(elemType));
l->listsize+=LISTCONTENTADD;
q=l->elem+locate-1;
for(p=l->elem+l->length*-1;p>=q;p--)
*(p+1)=*p;

int main(){
SqList *p,*q,*s;
p=(SqList *)malloc(sizeof(SqList));//分配空间
InitList_Sq(p);
AppendList_Sq(q);
PrintList_Sq(s);
return 0;
}
注意:结构体指针里面没有空间,所以赋值错误