//Written by LiuZhe(liuzhedash@gmail.com)
//Welcome to my blog:http://blog.csdn.net/liuzhedash
class sqlist
{
private :
int *elem;//pointer of elem
int length;//amount of elem
int listsize;//capacity of list
public :
void sqlistinitialize();//initialize list
bool isempty();//emptiness judgement
bool isfull();//same above
void newspace();//need new memory
int getelem(int);//locate elem
bool insert(int,int);//insert a elem to determind location
void fill();//fill up a list
int del(int);//delete a elem by determind location,return the deleted elem
void display();//display the features of list
void listdelete();
};
void sqlist::sqlistinitialize()
{
elem=new int[initsize];//
length=0;
listsize=initsize;
}
bool sqlist::isempty()
{
return length==0;
}
bool sqlist::isfull()
{
return length==listsize;
}
void sqlist::newspace()
{
int j;//subscript
int* tempelem;//temp list
tempelem=new int[listsize+increment];//apply for new memory
for(j=0;j<=length;j++)
tempelem[j]=elem[j];//copy elem to tempelem
delete [] elem;//free elem space
elem=tempelem;//redirect elem
listsize+=increment;
}
int sqlist::getelem(int i)
{
if ((i<0)||(i>length))
{cout<<"Illegal input location";return 0;}
else
return(elem[i]);
}
bool sqlist::insert(int i,int n)//i means subscript,n means the insert elem
{
int j;//temp subscript
if (i<0||i>length)
{
cout<<"Illegal input location";
return false;
}
else
if (isfull()) newspace(); //if list has been full,occupy new space
for (j=length;j>=i+1;j--)
elem[j]=elem[j-1];//elem next to i arrange new subscript
length+=1;
elem[i]=n;//= =、
return true;
}
void sqlist::fill()
{
int j,e;
cout<<"current list"<
display();
cout<<"input elem of the list,over by 0(integer request)"<
j=length-1;//j subscript last elem
if (isfull()) newspace();
while (true)
{
cin>>e;
if ((e!=0)&&(j<(listsize-1)))
{
j+=1;
elem[j]=e;
length+=1;
}
else break;
}
}
int sqlist::del(int p)//p means subscript
{
int j,temp;
temp=elem[p];
for(j=p;j<=length-2;j++) elem[j]=elem[j+1];
length-=1;
return temp;
}
void sqlist::display()
{
int j;
cout<
<<"DISPLY LIST";
cout<
<<"list length/listsize/frist elem address:"<
<<"/"<
<<"/"<
<
cout<<"list elems"<
for (j=0;j<=length-1;j++) cout<<"NUMBER"<
<<":["<
<<"] ";
}
void sqlist::listdelete()
{
delete []elem;
}