这段时间在啃数据结构,就想着将这些数据结构自己编写实现一下。
自身水平有限,如有错误,希望大家指教。
/* 内容:线性表的顺序存储
/ 作者:phlixce
/ 日期:2017.5.23
*/
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define MaxSize 20
typedef int ElemType;
class SeqList
{
public:
bool InitList();//初始化线性表
bool DestroyList();//销毁线性表
bool ClearList();//清空线性表
bool ListEmpty();//判断线性表是否为空
ElemType ListLength();//获取线性表长度
ElemType GetElem(ElemType i);//获取线性表的第i个元素
ElemType LocateElem(ElemType data);//判断data是否在顺序表中
bool ListInsert(ElemType i, ElemType data);//在线性表的第i个位置插入data
bool ListDelete(ElemType i);//删除线性表的第i个元素
void DisList();//输出线性表
private://私有数据,通过函数访问
ElemType *p;
ElemType Length;
};
//初始化线性表
bool SeqList::InitList()
{
p = new ElemType[MaxSize];
if(p==NULL)
return false;
Length=0;
return true;
}
//销毁线性表
bool SeqList::DestroyList()
{
delete []p;
/*
注意区别delete p与delete []p
*/
p=NULL;//防止野指针的产生
return true;
}
//清空线性表
bool SeqList::ClearList()
{
SeqList::DestroyList();
Length=0;
return true;
}
//判断线性表是否为空
bool SeqList::ListEmpty()
{
if(Length)
return true;
else
return false;
}
//获取线性表的长度
ElemType SeqList::ListLength()
{
return Length;
}
//获取线性表的第i个元素
ElemType SeqList::GetElem(ElemType i)
{
if(i<0||i>=Length)
cout<<"Invalid num input!"<<endl;
else
return p[i];
}
//判断data是否在顺序表中
ElemType SeqList::LocateElem(ElemType data)
{
for(int i=0; i<Length; i++)
{
if(data == p[i])
return i;
}
return -1;
}
//在线性表的第i个位置插入data
bool SeqList::ListInsert(ElemType i, ElemType data)
{
if(Length+1>MaxSize)
cout<<"线性表数据已满!"<<endl;
else if(i<0)
return false;
else
{
for(int j=Length-1;j>=i;j--)
{
p[j+1]=p[j];
}
p[i]=data;
}
Length+=1;
return true;
}
//删除i位置上的元素
bool SeqList::ListDelete(ElemType i)
{
if(i<0||i>Length-1)
return false;
else
{
for(int j=i+1;j<Length;j++)
{
p[j-1]=p[j];
}
}
Length-=1;
return true;
}
//输出线性表
void SeqList::DisList()
{
for(int i=0; i<Length; i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}
int main()
{
SeqList list;
list.InitList();
cout<<"线性表初始信息:"<<endl;
cout<<"list初始容量为"<<MaxSize<<endl;
cout<<"list初始长度为"<<list.ListLength()<<endl;
cout<<endl;
srand(time(0));
int i;
int tmp;
cout<<"随机生成10个数的线性表!"<<endl;
cout<<endl;
for(i=0; i<10; i++)
{
tmp=rand()%100+1;
list.ListInsert(i,tmp);
}
cout<<"原始线性表为:"<<endl;
list.DisList();
cout<<endl;
cout<<"在元素3和25分别插入第0个位置和最后一个位置"<<endl;
list.ListInsert(0,3);
list.ListInsert(list.ListLength(),25);
cout<<"插入数据后的线性表为:"<<endl;
list.DisList();
cout<<endl;
cout<<"线性表索引测试:"<<endl;
cout<<"线性表第5个数为:"<<list.GetElem(5)<<endl;
cout<<endl;
cout<<"线性表数据查询:"<<endl;
cout<<"查找25在线性表的位置:"<<list.LocateElem(25)<<endl;
cout<<endl;
cout<<"删除线性表的第5个数:"<<list.GetElem(5)<<"和第8个数:"<<list.GetElem(8)<<endl;
list.ListDelete(5);
list.ListDelete(8);
cout<<"删除数据后的线性表为:"<<endl;
list.DisList();
cout<<endl;
cout<<"清空线性表:"<<endl;
list.ClearList();
cout<<"清空后的线性表长度为:"<<list.ListLength()<<endl;
cout<<endl;
cout<<"判断清空后是否为线性表是否为空(0表示空,1表示非空):"<<list.ListEmpty()<<endl;
cout<<endl;
cout<<"销毁线性表:"<<endl;
list.DestroyList();
system("pause");
}
相关参考:
https://www.anotherhome.net/1553
http://blog.youkuaiyun.com/lllcsdn/article/details/50236763
http://www.cnblogs.com/zfc-java/p/6659639.html