实现部分
#include "SeqList.h"
void seqlist::setlist(seqlist *L,int n)
{
int *p=new int[n];
if(p==NULL)
cout<<"locate memory fail!"<<endl;
L->data=p;
L->max=n;
L->size=0;
}
void seqlist::freelist(seqlist *L)
{
delete [] L->data;
L->size=0;
L->max=0;
}
int seqlist::listsize(seqlist *L)
{
return L->size;
}
bool seqlist::listempty(seqlist *L)
{
if(L->size==0)
return true;
else
return false;
}
bool seqlist::listfull(seqlist *L)
{
if(L->size==L->max)
return true;
else
return false;
}
int seqlist::getdata(seqlist *L,int pos)
{
if(pos>=L->size&&pos<0)// the range is in 0 ~~ L->size-1 ;
{
cout<<"the fetch is out of reach!"<<endl;
exit(1);
}
else
return L->data[pos];
}
int seqlist::locate(seqlist *L,int item)
{
int i=0;
while(i<=L->size-1&&item!=L->data[i])
{
i++;
}
if(i>=0&&i<=L->size-1)
return i;
else
{
cout<<"can't find any!"<<endl;
exit(1);
}
}
void seqlist::setdata(seqlist *L,int item,int pos)
{
if(pos>=0&&pos<=L->size-1)
{
L->data[pos]=item;
}
else
{
cout<<"can't set data!";
exit(1);
}
}
void seqlist::insert(seqlist *L,int item,int pos)
{
if(L->size==L->max)
{
cout<<"can't insert,cause it's full already!"<<endl;
exit(1);
}
if(pos<0||pos>L->size)
{
cout<<"can't insert,cause it's in a wrong position!"<<endl;
exit(1);
}
for(int i=L->size;i>pos;i--)
{
L->data[i]=L->data[i-1];
}
L->data[pos]=item;
L->size++;
}
void seqlist::insertrear(seqlist *L,int item)
{
if(L->size==L->max)
{
cout<<"can't insert from the rear,cause it's full already!"<<endl;
exit(1);
}
else
{
L->data[L->size]=item;
}
L->size++;
}
void seqlist::deletedata(seqlist *L,int pos)
{
if(pos<0||pos>L->size-1)
{
cout<<"can't delete,cause it doesn't exist!"<<endl;
exit(1);
}
if(L->size==0)
{
cout<<"no element can be delete,cause nobody inside!"<<endl;
exit(1);
}
for(int i=pos+1;i<L->size;i++)
{
L->data[i-1]=L->data[i];
}
L->size--;
}
void seqlist::clearlist(seqlist *L)
{
L->size=0;
}
void seqlist::bubble(seqlist *L)
{
if(L->size==0)
{
cout<<"there is no element inside,so no bubble!"<<endl;
exit(1);
}
else
{
for(int i=0;i<L->size-1;i++)
for(int j=i+1;j<L->size;j++)
{
if(L->data[i]>L->data[j])
{
int temp=L->data[i];
L->data[i]=L->data[j];
L->data[j]=temp;
}
}
}
}