#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;
}
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;
}