// 线性表.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include
using namespace std;
//头文件
//=====================================================================================
//=====================================================================================
//=====================================================================================
#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();
};
//=====================================================================================
//=====================================================================================
//=====================================================================================
//=====================================================================================
//方法实现
//构造
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's value is illegal"<
length+1||i<1)
{
cout<<"i's value is illegal"<
=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结束"<
>elem;
if(elem!=0)
{
insert(elem);
}
else
{
cout<<"表创建完成,它的大小是"<
<<",表中有"<
<<"元素,它们是:"<
length||i<1) { cout<<"i's value is illegal"<
>str; while(toupper(str)!='Q') { switch(toupper(str)) { case 'A': sqc.displayElem(); break; case 'C': cout<<"请输入要插入的位置:"; int i; cin>>i; cout<<"表中第"<
<<"个元素为"<
<
>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<<"请输入正确的选项!"<
>str; } cout<<"操作结束!"<