#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;
class list
{
private :
int*array_pointer;
int arraysize;
int length;
int increment;
public:
list(int*list=NULL,int n=0,int len=0,int inc=0):array_pointer(list),arraysize(n),length(len),increment(inc){}
void initlist();
void listinsert();
void listdelete();
void clearlist();
void destorylist();
void display();
void ListLength();
void listempty();
void LocatElem();
void GetElem();
void MaxElem();
void average();
};
//*****************************************************************
//******************initlist************************************
void list::initlist()
{
array_pointer=new int[arraysize];
cout<<"please input the inital value of arraysize\n";
cin>>arraysize;
cout<<endl;
cout<<"please input the increment value\n";
cin>>increment;
cout<<endl;
cout<<"please input the array's length \n";
cin>>length;
while(length>arraysize)
{
cout<<"the number you want is invalid please input again\n";
cin>>length;
}
cout<<endl;
cout<<"please input "<<length<<" numbers\n";
for(int i=0;i<length;i++)
{
cin>>array_pointer[i];
}
cout<<endl;
}
//***********************************************
//******************************listinsert*******************
void list::listinsert()
{
int i,e;
cout<<"what number do you want to insert\n";
cin>>e;
cout<<"where do you want to insert after\n ";
cin>>i;
while(i<1||i>length)
{cout<<"the order you insert is error\n";
cout<<"where do you want to insert after\n ";
cin>>i;
}
length++;
if(length>arraysize)
{
int *newbase=new int[arraysize+increment];
memmove(newbase,array_pointer,sizeof(int)*arraysize);
array_pointer=newbase;
arraysize+=increment;
}
for(int j=0;j<=length-i;j++)
array_pointer[length-j]=array_pointer[length-j-1];
array_pointer[i]=e;
}
//***********************************************************
//******************listdelete**************************
void list::listdelete()
{int i,e;
cout<<"which number you delete \n ";
cin>>i;
while(i<1||i>length)
{cout<<"the order you insert is error\n";
cout<<"where do you want to delete\n ";
cin>>i;
}
e=array_pointer[i-1];
cout<<"the number you delete is "<<e<<endl;
for(int j=i-1;j<length;j++)
array_pointer[j]=array_pointer[j+1];
length--;
}
***********************************************************
//********************************************listclear
void list::clearlist()
{
length=0;
}
***********************************************************
//********************************************void MaxElem()
void list::MaxElem()
{
int max=0;
for(int i=0;i<length;i++)
{
if(array_pointer[i]>max)
max=array_pointer[i];
}
cout<<"the maxinum number in array is "<<max<<endl;
}
***********************************************************
//*******************************************destorylist
void list::destorylist()
{
delete []array_pointer;
}
***********************************************************
//*******************************************LocatElem()
void list::LocatElem()
{
int e,j;
cout<<"please input the number you want to search\n";
cin>>e;
for( j=0;j<length;j++)
if(array_pointer[j]==e)
{cout<<"the number you want is in "<<j<<"th \n";
break;
}
if(j>=(length-1))
cout<<"the number you want wae not in list\n";
}
***********************************************************
//*******************************************GetElem()
void list::GetElem()
{
int n;
cout<<" which number you want to get\n";
cin>>n;
if(n<=length-1&&0<=n)
cout<<"the number you want is "<<array_pointer[n]<<endl;
while(n>=length)
{
cout<<"the number you want is invalid please input again\n";
cin>>n;
if(n<=length-1)
cout<<"the number you want is "<<array_pointer[n]<<endl;
}
}
***********************************************************
//*******************************************listempty
void list::listempty()
{
if(length==0)
cout<<"the list is empty\n";
else
cout<<"the list is not empty\n";
}
***********************************************************
//*******************************************list_length
void list::ListLength()
{
cout<<"the length of list is "<<length<<endl;
}
***********************************************************
//*******************************************listdisplay
void list::display()
{
for(int i=0;i<length;i++)
cout<<array_pointer[i]<<" ";
cout<<endl;
}
***********************************************************
//******************************************* average
void list::average()
{
int sum=0;
float average;
for(int i=0;i<length;i++)
{
sum+=array_pointer[i];
}
average=float(sum)/length;
cout<<"the average of array is "<<average<<endl;
}
//******************************
//***************menu***********
void menu(void){
printf("\n武汉大学\n");
printf(" Mune for Linear Table On Sequence Structure \n");
printf("------------------------------------------------------\n");
printf(" 1. IntiaList 7. LocatElem\n");
printf(" 2. DestroyList 8. MaxElem\n");
printf(" 3. ClearList 9. Average \n");
printf(" 4. ListEmpty 10. ListInsert\n");
printf(" 5. ListLength 11. ListDelete\n");
printf(" 6. GetElem 12. ListTrabverse\n");
printf(" 0. Exit\n");
printf("------------------------------------------------------\n");
}
//**********************main***************************
int main()
{int a[5]={1,2,3,4,5};
list list(a,5,5,1);
/*list.listinsert();
list.display();
list.listdelete();
list.display();
list.max_and_average();*/
int op;
do{
//clrscr();
menu();
cout<<" Please input your option[0-12]:";
cin>>op;
switch(op){
case 0: break;
case 1: cout<<"\n here is IntiaList()\n";
list.initlist();
break;
case 2: cout<<"\n here is DestroyList()\n";
list.destorylist();
break;
case 3: cout<<"\n here is ClearList()\n";
list.clearlist();
break;
case 4: cout<<"\n here is ListEmpty()\n";
list.listempty();
break;
case 5: cout<<"\n here is ListLength() \n";
list.ListLength();
break;
case 6: cout<<"\n here is GetElem()\n";
list.GetElem();
break;
case 7: cout<<"\n here is LocatElem()\n";
list.LocatElem();
break;
case 8: cout<<"\n here is MaxElem()\n";
list.MaxElem();
break;
case 9: cout<<"\n here is average()\n";
list.average();
break;
case 10: cout<<"\n here is ListInsert()\n";
list.listinsert();
break;
case 11: cout<<"\n here is ListDelete()\n";
list.listdelete();
break;
case 12: cout<<"\n here is ListTrabverse()\n";
list.display();
default: ;
}
}while(op);
return 0;
}
顺序链表的一些操作
最新推荐文章于 2021-12-06 19:29:43 发布