#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 80
#define LISTINCREMENT 10
#define Status int
#define ElemType int
#define OK 1
#define OVERFLOW -2
#define ERROR 0
#define TRUE 1
#define FALSE 0
struct List{
ElemType *elem;
int length;
int listsize;
};
typedef struct List SqList;
Status InitList(SqList &L);
Status DestroyList(SqList &L);
Status ListEmpty(SqList L);
int ListLength(SqList L);
Status PriorElem(SqList L,ElemType cur_e,ElemType &pre_e);
Status NextElem(SqList L,ElemType cur_e,ElemType &next_e);
Status GetElem(SqList L,int i,ElemType &e);
int LocateElem(SqList L,ElemType e,Status (*compare)(ElemType,ElemType));
Status ListTraverse(SqList L,Status (*visit)(SqList &L));
Status ClearList(SqList &L);
Status PutElem(SqList L,int i,ElemType e);
Status ListInsert(SqList &L,int pos,ElemType e);
Status ListDelete(SqList &L,int i,ElemType &e);
Status (*compare)(ElemType,ElemType);
Status equal(ElemType,ElemType);
Status ListPrint(SqList &L);
int main()
{
SqList L;
int i,j;
ElemType e;
printf("InitList%d\n",InitList(L));//init
printf("listempty%d\n",ListEmpty(L));//listempty
for(i = 0;i < 11;i++){
L.elem[i] = i + 1;
L.length++;
}
printf("Insert before\n");//print
for(j = 0;j < L.length;j++){
printf("%d ",L.elem[j]);
}
printf("ListInsert%d\n",ListInsert(L,3,45));//insert
printf("Insert later\n");//print
for(j = 0;j < L.length;j++){
printf("%d ",L.elem[j]);
}
printf("ListDelete%d\n",ListDelete(L,3,e));//delete
printf("delete later\n");//print
for(j = 0;j < L.length;j++){
printf("%d ",L.elem[j]);
}
PutElem(L,5,100);//put
printf("putelem%d\n",L.elem[4]);
GetElem(L,5,e);//get
printf("getelem%d\n",e);
PriorElem(L,5,e);//prior
printf("priorelem%d\n",e);
NextElem(L,5,e);//next
printf("nextelem%d\n",e);
printf("length%d\n",ListLength(L));//Listlength
compare = equal;
printf("locateelem%d",LocateElem(L,6,compare));//LocateElem
printf("destroy%d\n",DestroyList(L));//destroylist
printf("listempty%d\n",ListEmpty(L));
//union
/*List La,Lb;
int i;
ElemType e;
InitList(La);
InitList(Lb);
for(i = 0;i < 10;i++){
La.elem[i] = i + 1;
La.length++;
}
for(i = 0;i < 15;i++){//∏≥÷µ
Lb.elem[i] = i;
Lb.length++;
}
printf("union before\n");//print
for(i = 0;i < La.length;i++){
printf("%d ",La.elem[i]);
}
int La_len = ListLength(La);
int Lb_len = ListLength(Lb);
compare = equal;
for(i = 1;i <= Lb_len;i++){
GetElem(Lb,i,e);
if(!LocateElem(La,e,compare)){
ListInsert(La,++La_len,e);
}
}
printf("union later\n");//print
for(i = 0;i < 15;i++){
printf("%d ",La.elem[i]);
}
DestroyList(L);
return 0;
}
Status InitList(SqList &L){
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status DestroyList(SqList &L){
if(L.elem){
free(L.elem);
}
L.elem = NULL;
return OK;
}
Status ListEmpty(SqList L){
if(!L.length){
return TRUE;
}else
return FALSE;
}
int ListLength(SqList L){
return L.length;
}
Status PriorElem(SqList L,ElemType cur_e,ElemType &pre_e){
int i = 2;
if(L.elem[0]==cur_e)
return ERROR;
while(i <= L.length&&L.elem[i-1] != cur_e)
++i;
if(i>L.length){
return ERROR;
}else
pre_e = L.elem[i-2];
return OK;
}
Status NextElem(SqList L,ElemType cur_e,ElemType &next_e){
int i = 1;
while(i < L.length&&L.elem[i-1] != cur_e)
++i;
if(i == L.length){
return ERROR;
}else
next_e = L.elem[i];
return OK;
}
Status GetElem(SqList L,int i,ElemType &e){
if(i > L.length || i < 1)
return ERROR;
e = L.elem[i -1];
return OK;
}
int LocateElem(SqList L,ElemType e,Status (*compare)(ElemType,ElemType)){
int i = 1;
ElemType *p = L.elem;
while(i <= L.length && !(*compare)(*p++,e))
++i;
if(i <= L.length)
return i;
else
return 0;
}
Status ListTraverse(SqList L,Status (*visit)(ElemType,ElemType)){
return 0;
}
Status ClearList(SqList &L){
L.length = 0;
return OK;
}
Status PutElem(SqList L,int i,ElemType e){
if(i > L.length || i < 1)
return ERROR;
L.elem[i -1] = e;
return OK;
}
Status ListInsert(SqList &L,int pos,ElemType e){
if(pos > L.length+1 || pos < 1)
return ERROR;
if(L.length >= L.listsize){
/*ElemType * newbase = (ElemType *)malloc((L.listsize + LISTINCREMENT)*sizeof(ElemType));
for(int i = 0;i < L.length;i++){
newbase[i] = L.elem[i];
}
free(L.elem);
*/
ElemType * newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT)*sizeof(ElemType));
L.elem = newbase;
L.listsize = LIST_INIT_SIZE+ LISTINCREMENT;
//printf("newbase\n");
if(!newbase)
exit(OVERFLOW);
}
ElemType *q = &L.elem[pos - 1];
for(ElemType *p = &L.elem[L.length - 1];p >= q;--p){
*(p + 1) = *p;
}
*q = e;
++L.length;
return OK;
}
Status ListDelete(SqList &L,int pos,ElemType &e){
if(pos > L.length || pos < 1)
return ERROR;
ElemType *q = &L.elem[L.length - 1];
for(ElemType *p = &L.elem[pos - 1];p <= q;p++){
*p = *(p + 1);
}
e = *q;
--L.length;
return OK;
}
Status ListPrint(SqList &L)
{
if (ListEmpty(L))
{
return ERROR;
}
for (int i=0; i<L.length; ++i)
{
printf("%-4d ", L.elem[i]);
}
printf("\n");
return OK;
}
Status equal(ElemType a,ElemType b){
if(a == b)
return true;
else
return false;
}