#include <iostream>
#include <cstdlib>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 10
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct _SqList{
ElemType* elem;
int length;
int listsize;
}SqList;
bool InitList(SqList* List)
{
if(NULL == List->elem){
//List->elem = new (ElemType)[LIST_INIT_SIZE * (ElemType)];
List->elem = (ElemType*) malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if(NULL == List->elem){
return false;
}
List->length = 0;
List->listsize = LIST_INIT_SIZE;
}else{
return false;
}
return true;
}
ElemType ListInsert(SqList* List, int i, ElemType e)
{
if(NULL == List->elem){
std::cout << "表为空,创建表!" << std::endl;
InitList(List);
}
if(i < 1 || i > List->length){
std::cout << "插入位置出错!" << std::endl;
return ERROR;
}
if(List->length >= List->listsize){
ElemType* Newq = NULL;
Newq = (ElemType*) realloc(List->elem, sizeof(ElemType) * LIST_INCREMENT);
if(NULL == Newq){
std::cout << "插入失败,表满,扩充表失败!" << std::endl;
return ERROR;
}
List->elem = Newq;
List->listsize += LIST_INCREMENT;
}
int* p = NULL;
int* q = NULL;
q = &(List->elem[i - 1]);
for(p = &(List->elem[List->length - 1]); p >= q; --p){
*(p + 1) = *p;
}
*q = e;
++(List->length);
return OK;
}
ElemType DleteList(SqList* List, int i)
{
if(NULL == List){
std::cout << "表为空,删除失败!" << std::endl;
return ERROR;
}
if(i < 1 || i > List->length){
std::cout << "删除位置无效,删除失败!" <<std::endl;
return ERROR;
}
ElemType* q = NULL;
ElemType* p = NULL;
ElemType e = List->elem[i - 1];
p = &(List->elem[List->length - 1]);
for(q = &(List->elem[i - 1]); q > p; ++q){
*q = *(q + 1);
}
--(List->length);
return e;
}
int main(int argc, char* argv[])
{
return 0;
}