厳蔚敏--線形表の順序表示と実現コード

1631 ワード

#include
#include
#include
using namespace std;
//----------              -------------
#define LIST_INT_SIZE 100
#define LISTINCREATMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;

typedef struct {
  ElemType *elem;
  int length;
  int listsize;/*         */
}SqList;
int InitList(SqList &L){//   
  L.elem=(ElemType*)malloc(LIST_INT_SIZE*sizeof(ElemType));
  if(!L.elem)exit(OVERFLOW);
  L.length=0;
  L.listsize=LIST_INT_SIZE;
  return OK;
}
int ListInsert(SqList&L,int i,ElemType e){//    
  if(i<1||i>L.length+1)return ERROR;
  if(L.length>>L.listsize){
    ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREATMENT)*sizeof(ElemType));
    if(!newbase)exit(OVERFLOW);
    L.elem=newbase;
    L.listsize+=LISTINCREATMENT;
  }
  ElemType*q=&(L.elem[i-1]);
  for(ElemType*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
  *q=e;
  ++L.length;
  return OK;
}
int ListDelete(SqList&L,int i,ElemType&e){//    
  if(i<1||i>L.length)return ERROR;
  ElemType*p=&(L.elem[i-1]);
  e=*p;
  ElemType*q=&(L.elem[L.length-1]);
  for(++p;p<=q;++p)*(p-1)=*p;
  --L.length;
  return OK;
}
void PrintList(SqList L){//  
  for(int i=0;i