数据结构线性表顺序存储C++实现

#include
#include
using namespace std;


#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define List_Init_Size 20
#define List_IncreMent 10 


typedef int ElemType;
typedef int Status;
typedef struct{
  ElemType *elems;//数据元素组成的数组
  int length;//元素个数
  int listsize;//线性表长度
}SqList;


Status compare(ElemType e1,ElemType e2);
Status add(ElemType &e);
Status InitSqList(SqList &list);
Status DestorySqList(SqList *list_p);
Status ClearSqList(SqList &list);
Status ListEmpty(SqList list);
int LIstLength(SqList list);
Status getElem(SqList list,ElemType &e,int i);
int LocateElem(SqList list,ElemType e,Status (*compare)(ElemType e1,ElemType e2));
Status PriorElem(SqList list,ElemType cur_e,ElemType &pre_e);
Status NextElem(SqList list,ElemType cur_e,ElemType &pre_e);
Status ListInsert(SqList &list,ElemType e,int i);
Status ListDelete(SqList &list,int i,ElemType &e);
Status ListTraverse(SqList &list,Status (*add)(ElemType &e));
Status CoutSqList(SqList list);


int main(){
  SqList list;
  cout<<"初始化顺序线性表"<   InitSqList(list);
  cout<<"顺序线性表初始化完成"<   cout<<"线性表是否为空:"<   cout<<"放置数据元素1,2,3,4"<   ListInsert(list,1,0);
  ListInsert(list,2,1);
  ListInsert(list,3,2);
  ListInsert(list,4,3);
  cout<<"线性表是否为空:"<   CoutSqList(list);
  cout<<"线性表长度为:"<   ElemType e;
  getElem(list,e,1);
  cout<<"获取线性表第二个元素为"<   cout<<"元素1在表中下标:"<   cout<<"元素3在表中下标:"<   ListDelete(list,2,e);
  cout<<"删除表中第三个元素,此值为"<   cout<<"线性表长度为:"<   CoutSqList(list);
  cout<<"自增1"<   ListTraverse(list,add);
  CoutSqList(list);
  Status status;
  status=PriorElem(list,1,e);
  if(status)
  cout<<"找到元素1前面的值为:"<   else
  cout<<"找不到元素1前面的值"<   status=NextElem(list,2,e);
  if(status)
  cout<<"找到元素2后面的值为:"<   else
  cout<<"找不到元素2后面的值"<   return OK;
}


Status InitSqList(SqList &list){
  list.elems=(ElemType *)malloc(List_Init_Size*sizeof(ElemType));
  if(!list.elems)
  exit(OVERFLOW);
  list.length=0;
  list.listsize=List_Init_Size;
  return OK;
}




Status DestorySqList(SqList *list_p){
  free(list_p);
  return OK;
}


Status ClearSqList(SqList &list){
  list.length=0;
  return OK;
}


Status ListEmpty(SqList list){
return list.length==0?TRUE:FALSE;
}


int LIstLength(SqList list){
return list.length;
}


Status getElem(SqList list,ElemType &e,int i){
    if(i<0 || i>=list.length)
return ERROR;
e=list.elems[i];
return OK;
}


int LocateElem(SqList list,ElemType e,Status (*compare)(ElemType e1,ElemType e2)){
    for(int i=0;i if(compare(e,list.elems[i]))
return i;
}
return -1;
}


Status PriorElem(SqList list,ElemType cur_e,ElemType &pre_e){
    for(int i=1;i if(compare(cur_e,list.elems[i])){
pre_e=list.elems[i-1];
    return OK;
}
}
return ERROR;
}


Status NextElem(SqList list,ElemType cur_e,ElemType &pre_e){
    for(int i=0;i if(compare(cur_e,list.elems[i])){
  pre_e=list.elems[i+1];
  return OK;
}
}
return ERROR;
}


Status ListInsert(SqList &list,ElemType e,int i){
    if(i<0 || i>list.length){
  return ERROR;
}
if(list.length==list.listsize)
  list.elems=(ElemType *)realloc(list.elems,List_IncreMent*sizeof(ElemType));//扩大内存
if(!list.elems)
exit(OVERFLOW);
for(int j=list.length-1;j>i;j--){
  list.elems[j+1]=list.elems[j];
}
list.elems[i]=e;
list.length++;
return OK;
}


Status ListDelete(SqList &list,int i,ElemType &e){
    if(i<0 || i>list.length-1)
return ERROR;
e=list.elems[i];
for(int j=i+1;j   list.elems[j-1]=list.elems[j];
}
list.length--;
return OK;
}


Status ListTraverse(SqList &list,Status (*visit)(ElemType &e)){
    for(int i=0;i    if(!visit(list.elems[i]))
   return ERROR;
}
return OK;
}


Status compare(ElemType e1,ElemType e2){
return e1==e2?TRUE:FALSE;
}


Status add(ElemType &e){
e++;
return OK;
}


Status CoutSqList(SqList list){
cout<<"输出线性表"<     if(list.length==0)
cout<<"线性表为空"< for(int i=0;i cout< return OK;
}

你可能感兴趣的:(数据结构)