顺序表SqList的C++代码实现

C++实现

#include <iostream>
using namespace  std;

//结构的定义部分

const int INITSIZE=3 ;
const int INCREMENT=1
;
typedef 
int
 ElemType;      
typedef 
struct
{
  ElemType  
*
elem;
  
int
       length;
  
int
       listsize;
} SqList;

//结构的生成操作

void InitList(SqList &L,int maxsize=0 )
{
  
if(maxsize==0) maxsize=
INITSIZE;
  L.elem
=new
 ElemType[maxsize];
  
if(!L.elem) exit(1
);
  L.length
=0
;
  L.listsize
=
maxsize;
}

//结构的销毁操作

void DestroyList(SqList & L)
{
  delete [] L.elem;
  L.elem
=
NULL;
  L.length
=0
;
  L.listsize
=0
;
}

//结构的清理操作

void ClearList(SqList & L)
{
  L.length
=0
;
}

//结构的状态查看

bool  ListEmpty(SqList L)
{
  
return L.length==0
;
}

int
 ListLength(SqList L)
{
  
return
 L.length;
}

void
 ListOutput(SqList L)
{
  cout
<<"("
;
  
for(int i=0;i<L.length-1;i++) cout<<L.elem[i]<<","
;
  
if(L.length-1>=0) cout<<L.elem[L.length-1
];
  cout
<<")"<<
endl;
}

void
 ListInfo(SqList L)
{
  cout
<<"  "
;
  ListOutput(L);
  
if(ListEmpty(L)) cout<<"  Empty:Yes"<<
endl;
  
else cout<<"  Empty:No"<<
endl;
  cout
<<"  Length="<<ListLength(L)<<",Size="<<L.listsize<<
endl;
}

//结构的查改增删

bool GetElem(SqList L,int pos,ElemType & e)
{
  
if(pos<1||pos>L.length) return false
;
  e
=L.elem[pos-1
];
  
return true
;
}

bool
 ElemEqual(ElemType e1,ElemType e2)
{
  
return e1==
e2;
}

bool
 ElemPlus(ElemType e1,ElemType e2)
{
  
return e1>
e2;
}

bool
 ElemMinus(ElemType e1,ElemType e2)
{
  
return e1<
e2;
}

int LocateElem(SqList L,ElemType e,bool (*
compare)(ElemType,ElemType))
{
  
for(int i=1;i<=L.length;i++
)
    
if((*compare)(L.elem[i-1],e)) return
 i;
  
return 0
;
}

bool PutElem(SqList &L,int
 pos,ElemType e)
{
  
if(pos<1||pos>L.length) return false
;
  L.elem[pos
-1]=
e;
  
return true
;
}

bool ListInsert(SqList &L,int
 pos,ElemType e)
{
  
if(pos<1||pos>L.length+1return false
;
  
if(L.length>=
L.listsize)
  {
    ElemType 
*newbase=new ElemType[L.listsize+
INCREMENT];
    
if(!newbase) exit(1
);
    
for(int i=0;i<L.length;i++) newbase[i]=
L.elem[i];
    delete [] L.elem;
    L.elem
=
newbase;
    L.listsize
+=
INCREMENT;
  }
  
for(int i=L.length-1;i>=pos-1;i--
)
    L.elem[i
+1]=
L.elem[i];
  L.elem[pos
-1]=
e;
  L.length
++
;
  
return true
;
}

bool ListDelete(SqList &L,int pos,ElemType &
e)
{
  
if(pos<1||pos>L.length) return false
;
  
for(int i=pos;i<L.length;i++
)
    L.elem[i
-1]=
L.elem[i];
  e
=L.elem[pos-1
];
  L.length
--
;
  
return true
;
}

//结构的排序操作

void ListSort(SqList & L)
{
  
for(int i=0;i<L.length-1;i++
)
  
for(int j=0;j<L.length-1-i;j++
)
    
if(L.elem[j]>L.elem[j+1
])
    {
      
int tmp=
L.elem[j];
      L.elem[j]
=L.elem[j+1
];
      L.elem[j
+1]=
tmp;
    }
}

void MergeList(SqList La,SqList Lb,SqList &
Lc)
{
  ElemType 
*pa,*pb,*pc,*pa_last,*
pb_last;
  pa 
= La.elem;  pb =
 Lb.elem;
  Lc.listsize 
= Lc.length = La.length+
Lb.length;
  pc 
= Lc.elem = new
 ElemType[Lc.listsize];
  
if (!Lc.elem) exit(1);

  pa_last = La.elem+La.length-1 ;
  pb_last 
= Lb.elem+Lb.length-1
;
  
while(pa<=pa_last&&pb<=pb_last){

    if (*pa<=*pb) *pc++=*pa++ ;
    
else *pc++=*pb++
;
  }
  
while (pa<=pa_last) *pc++=*pa++;

  while (pb<=pb_last) *pc++=*pb++;
}

//结构的功能测试

int  main()
{
  cout
<<"顺序表结构的测试:"<<
endl;

  SqList A;

  cout
<<"***************************************"<<
endl;
  cout
<<"生成操作:InitList(A)"<<
endl;
  InitList(A);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"插入操作:ListInsert(A,1,2)"<<
endl;
  ListInsert(A,
1,2
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"插入操作:ListInsert(A,2,7)"<<
endl;
  ListInsert(A,
2,7
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"插入操作:ListInsert(A,2,8)"<<
endl;
  ListInsert(A,
2,8
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"排序操作:ListSort(A)"<<
endl;
  ListSort(A);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"插入操作:ListInsert(A,2,21)"<<
endl;
  ListInsert(A,
2,21
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"插入操作:ListInsert(A,4,15)"<<
endl;
  ListInsert(A,
4,15
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"删除操作:ListDelete(A,5,e)"<<
endl;
  ElemType e;
  ListDelete(A,
5
,e);
  ListInfo(A);
  cout
<<"  e="<<e<<
endl;

  cout
<<"***************************************"<<
endl;
  cout
<<"排序操作:ListSort(A)"<<
endl;
  ListSort(A);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"更新操作:PutElem(A,4,39)"<<
endl;
  PutElem(A,
4,39
);
  ListInfo(A);

  cout
<<"***************************************"<<
endl;
  cout
<<"查找操作:GetElem(A,2,e)"<<
endl;
  GetElem(A,
2
,e);
  ListInfo(A);
  cout
<<"  e="<<e<<
endl;

  cout
<<"***************************************"<<
endl;
  cout
<<"定位操作:LocateElem(A,15,ElemEqual)"<<
endl;
  
int Pos=LocateElem(A,15
,ElemEqual);
  ListInfo(A);
  cout
<<"  Pos="<<Pos<<
endl;

  cout
<<"***************************************"<<
endl;
  cout
<<"定位操作:LocateElem(A,6,ElemPlus)"<<
endl;
  Pos
=LocateElem(A,6
,ElemPlus);
  ListInfo(A);
  cout
<<"  Pos="<<Pos<<
endl;

  cout
<<"***************************************"<<
endl;
  cout
<<"定位操作:LocateElem(A,8,ElemMinus)"<<
endl;
  Pos
=LocateElem(A,8
,ElemMinus);
  ListInfo(A);
  cout
<<"  Pos="<<Pos<<
endl;
  
  cout
<<"***************************************"<<
endl;
  SqList B;
  InitList(B);
  ListInsert(B,
1,9
);
  ListInfo(B);

  cout
<<"***************************************"<<
endl;
  SqList C;
  InitList(C);
  MergeList(A,B,C);
  ListOutput(A);
  ListOutput(B);
  ListOutput(C);

  cout
<<"按任意键,结束..."
;
  cin.
get
();

  
return 0
;
}
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

learnlife

向这个世界输出我独有的声音。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值