线性表的顺序存储实现c++

#include <iostream>
using namespace std;
class List
{
private:
int *m_pList;
int m_iSize;
int m_iLength;
public:
List(int size)
{
m_iSize=size;
m_pList=new int[m_iSize];
m_iLength=0;
}
List()
{
delete []m_pList;
m_pList=NULL;
}
void InitList()
{
m_iLength=0;
}
bool EmptyList()
{
if(m_iLength==0) return true;
else return false;
// return m_iLength==0?true:false;
}
int GetLength()
{
return m_iLength;
}
bool GetElem(int i,int *e)
{
if(i<0||i>m_iSize+1)
{
return false;
}
else
{
*e=m_pList[i];
return true;
}
}
int Locate(int e)
{
int i=0;
while(m_pList[i]!=e) i++;
return(i+1);
if(i>m_iLength) return(0);
// for(int i=0;i<m_iLength;i++) { if(m_pList[i]=*e) return i;else return(0); }
 
}
bool Prior(int *x,int *pr)
{
int temp=Locate(*x);
if(temp==0)
{
return false;
}
else
{
*pr=m_pList[temp-1];
return true;
}
}
bool Next(int *x,int *ne)
{
    int temp=Locate(*x);
if(temp==0)
{
return false;
}
else
{
if(temp==m_iLength)
{
return false;
}
else
{
*ne=m_pList[temp+1];
    return true;
}  
}
}
void InsertList(int i,int *e)
{
for(int j=m_iLength;j>=i;j--)
{
m_pList[j+1]=m_pList[j];
}
m_pList[i]=*e;
m_iLength++;
}
void DeleteList(int i,int *e)
{
*e=m_pList[i];
for(int j=i;j<=m_iLength;j++)
{
m_pList[j]=m_pList[j+1];
}
m_iLength--;
}
void ListTraverse()
{
for(int i=0;i<m_iLength;i++)
{
cout<<m_pList[i]<<" ";
}
cout<<endl;


}
};
int main()
{
int e1=3,e2=6,e3=2,e4=3,e5=2,e6=1,e7=0,temp=0;
List *list1=new List(10);
list1->InsertList(0,&e1);
list1->InsertList(1,&e2);
list1->InsertList(2,&e3);
list1->InsertList(3,&e4);
list1->InsertList(4,&e5);
list1->InsertList(5,&e6);
list1->InsertList(6,&e7);
    cout<<"表长:"<<list1->GetLength();
cout<<"表的顺序:";
list1->ListTraverse();
list1->DeleteList(0,&temp);
cout<<"删除表中的数:"<<temp<<endl;
cout<<"表长:"<<list1->GetLength();
cout<<"表的顺序:";
list1->ListTraverse();
list1->GetElem(1,&temp);
cout<<"得到的值:"<<temp<<endl;
cout<<list1->Locate(temp)<<endl;
    list1->Prior(&e2,&temp);
cout<<"前驱:"<<temp<<endl;
list1->Next(&e2,&temp);
cout<<"后继:"<<temp<<endl;
delete list1; 
return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值