#ifndef LIST_H
#define LIST_H
class List
{
public:
List(int size);
~List();
void ClearList();
bool ListEmpty();
int ListLength();
bool GetElem(int i,Elem *e);
int LocateElem(Elem *e);
bool PriorElem(Elem *currentElem,Elem *preElem);
bool NextElem(Elem *currentElem,Elem *nextElem);
void ListTraverse();
bool ListInsert(int i,Elem *e);
bool ListDelete(int i,Elem *e);
private:
int *m_plist;
int m_size;
int m_length;
}
#endif
#include "list.h"
#include <iostream>
using namespace std;
List::list(int size)
{
m_size=size;
m_plist=new int[m_size];
m_length=0;
}
List::~List()
{
delete []m_plist;
m_plist=NULL;
}
void List::ClearList()
{
m_length=0;
}
bool List::ListEmpty()
{
return m_length==0?true:false;
}
int List::ListLength()
{
return m_length;
}
bool List::GetElem(int i,Elem *e)
{
if(i<0||i>=m_size)
{
return false;
}
*e=m_plist[i];
return true;
}
int List::LocateElem(Elem *e)
{
for(int i=0;i<m_length;i++)
{
if(m_plist[i]==*e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(Elem *currentElem,Elem *preElem)
{
int temp=locateElem(currentElem);
if(temp==-1||temp==0)
{
return false;
}
else
{
preElem=m_plist[temp-1];
return true;
}
}
bool List::NextElem(Elem *currentElem,Elem *nextElem)
{
int temp=locateElem(currentElem);
if(temp==-1||temp==m_length-1)
{
return false;
}
else
{
nextElem=m_plist[temp+1];
return true;
}
}
void List::ListTraverse()
{
for(int i=0;i<m_length;i++)
{
cout<<m_plist[i]<<endl;
}
}
bool List::ListInsert(int i,Elem *e)
{
if(i<0||i<=m_length)
{
return false;
}
if(m_iLength+1>m_iSize)
{
return false;
}
for(int k=m_length-1;k>=i;k--)
{
m_plist[k+1]=m_plist[k];
}
m_plist[i]=*e;
return true;
}
bool List::ListDelete(int i,Elem *e)
{
if(i<0||i>=m_length)
{
return false;
}
*e=m_plist[i];
for(int k=i+1;k<m_length;k++)
{
m_plist[k-1]=m_plist[k];
}
m_length--;
return true;
}