#include < stdio.h >
#include < stdlib.h >
#include "myconst.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct {
ElemType * elem;
int length;
int listsize;
}
SqList;
Status 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 * p,
*q,
*newbase;
if (i < 1 || i > L.length + 1) return ERROR;
if (L.length >= L.listsize) {
newbase = (ElemType * ) realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = &(L.elem);
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);
e = *p;
q = L.elem + L.length - 1;
for (++p; p <= q; ++p) * (p - 1) = *p; --L.length;
return OK;
}
void printSq(SqList L) {
int i,
j;
ElemType * p;
j = L.length;
p = L.elem;
printf("\n");
for (i = 0; i < j; i++) {
printf("%5d", p);
if ((i + 1) % 10 == 0) printf("\n");
}
printf("\n");
}
void main() {
int i;
ElemType e;
SqList L;
InitList_Sq(L);
printf("\n");
for (i = 1; i <= 11; i++) {
printf("Enter L[%d]:", i);
scanf("%d", &e);
ListInsert_Sq(L, i, e);
}
printSq(L);
ListDelete_Sq(L, 7, e);
printSq(L);
printf("e=%d", e);
}
标签:int,ElemType,listsize,elem,length,XXSXB,SqList From: https://blog.51cto.com/u_16076050/6195967