C++实现线性表

-------------------------SqList.h------------------------------------
/*构造线性表*/
#include<iostream>
#ifndef SqList_H_
#define SqList_H_//定义头文件
#define initSize 100//初始化大小
#define increment 10//增长步长
typedef int ElemType;//定义元素类型
class SqList
{
private:
 ElemType* elem; //声明元素指针
    int length;//元素个数
 int listSize;//表长度
public:
 SqList();//初始化表
 bool isFull()const;//查看表是否为空
 bool isEmpty()const;//查看表是否满
 ElemType getElem(int)const;//查看特定位置元素
 bool insert(int,ElemType);//插入特定位置元素
 bool insert(ElemType);//插入末尾元素
 void creat();//创建表
 void merge(SqList&,SqList&);//合并表并排序
 bool del(int);//删除表
 void displayElem();//显示表情况
 ~SqList();//析构
};
#endif
-------------------------SqList.cpp------------------------------------
#include"SqList.h"
using namespace std;
SqList::SqList(){
 elem=new ElemType[initSize];
 length=0;
 listSize=initSize;
}
bool SqList::isEmpty() const{
 return length==0;
}
bool SqList::isFull() const{
 return length==listSize;
}
ElemType SqList::getElem(int i) const{
 if(i>length||i<1)
 {
  cout<<"i-value is illegal!";
  return 0;
 }
 else
  return elem[i-1];
}
bool SqList::insert( int i, ElemType e){
 if(i>length+1||i<1)
 {
  cout<<"i-value is illegal!";
  return false;
 }
   if(isFull())//表满重新分配内存
 {
  ElemType* newBase=new ElemType[listSize+increment];
  for(int i=0;i<=listSize;i++)
   newBase[i]=elem[i];
  delete[] elem;
  elem=newBase;
  listSize+=increment;
 }
 ElemType* p=&(elem[i-1]);
 ElemType* q=&(elem[length-1]);
 for(;q>=p;--q)
  *(q+1)=*q;
 *p=e;
 ++length;
 return true;
}
bool SqList::insert(ElemType e){
   if(isFull())//表满重新分配内存
 {
  ElemType* newBase=new ElemType[listSize+increment];
  for(int i=0;i<=listSize;i++)
   newBase[i]=elem[i];
  delete[] elem;
  elem=newBase;
  listSize+=increment;
 }
 ElemType* p=&(elem[length]);
 *p=e;
 ++length;
 return true;
}
void SqList::creat(){
 cout<<"为表添加元素(输入0退出!)"<<endl;
 while(true)
 {
  int elem;
  cin>>elem;
  if(elem!=0)
  {
   insert(elem);
  }
  else
  {
   cout<<"表创建完成,大小为"<<listSize<<"."<<endl;
   cout<<"表现有"<<length<<"个元素:"<<endl;
   for(int i=1;i<=length;i++)
    cout<<getElem(i)<<",";
   cout<<endl;
   break;
  }
 }
}
void SqList::merge(SqList &La,SqList &Lb){
 int i=1,j=1,k=0;
 while(i<=La.length&&j<=Lb.length)
 {
  if(La.getElem(i)<=Lb.getElem(j))
  {
   insert(++k,La.getElem(i));
   ++i;
  }
  else
  {
            insert(++k,Lb.getElem(j));
   ++j;
  }
 }
 while(i<=La.length)
 {
        insert(++k,La.getElem(i));
  ++i;
 }
 while(j<=Lb.length)
 {
        insert(++k,Lb.getElem(j));
  ++j;
 }
 cout<<"合并后元素为:"<<endl;
 for(int i=1;i<=length;i++)
  cout<<getElem(i)<<endl;
}
bool SqList::del( int i){
 if(i>length||i<1)
 {
  cout<<"i-value is illegal!";
  return false;
 }
 ElemType* p=&(elem[i-1]);
 ElemType* q=&(elem[length-1]);
 for(++p;p<=q;++p)
  *(p-1)=*p;
 --length;
 return true;
}
void SqList::displayElem(){
 cout<<"表的大小为"<<listSize<<"."<<endl;
 cout<<"表现有"<<length<<"个元素:"<<endl;
 for(int i=1;i<=length;i++)
  cout<<getElem(i)<<",";
 cout<<endl;
}
SqList::~SqList(){
delete[] elem;
}
-------------------------useSqList.cpp------------------------------------
#include"SqList.h"
using namespace std;
int main()
{
 cout<<"创建表A:"<<endl;
 SqList sqa;
 sqa.creat();
 cout<<"创建表B:"<<endl;
 SqList sqb;
 sqb.creat();
 cout<<"合并表A、B的元素,并按从小到大的顺序放入表C!"<<endl;
 SqList sqc;
 sqc.merge(sqa,sqb);
 cout<<"操作表C:"<<endl;
 cout<<"选择操作(Q退出):"<<endl;
 cout<<"1.查看表情况(A):"<<endl;
 cout<<"2.查看元素特定元素(C):"<<endl;
 cout<<"3.插入表尾元素(I):"<<endl;
 cout<<"4.插入特定位置元素(L):"<<endl;
 cout<<"5.删除元素(D):"<<endl;
 char str;
 cin>>str;
 while(toupper(str)!='Q')
 {
  switch(toupper(str))
  {
      case 'A':
   sqc.displayElem();
   break;
   case 'C':
    cout<<"请输入要查看元素位置:";
    int i;
    cin>>i;
    cout<<"表中第"<<i<<"个元素为"<<sqc.getElem(i)<<endl;
    break;
   case 'I':
    cout<<"请输入要插入元素:";
    int elem;
    cin>>elem;
    sqc.insert(elem);
    sqc.displayElem();
    break;
   case 'L':
    cout<<"请输入插入元素位置:";
    int j;
    cin>>j;
    cout<<"请输入要插入元素:";
                int jelem;
    cin>>jelem;
    sqc.insert(j,jelem);
    sqc.displayElem();
    break;
   case 'D':
    cout<<"请输入要删除元素位置:";
    int k;
    cin>>k;
    sqc.del(k);
    sqc.displayElem();
    break;
   default:
    cout<<"请输入正确选项!"<<endl;
  }
  cout<<"输入选项继续操作:";
  cin>>str;
 }
 cout<<"操作结束!"<<endl;
 cin.get();
 cin.get();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值