#include<iostream.h>
#define MAX 1024
typedef int Elemtype;
typedef struct
{
int *elem;
int length;
}Sqlist;
void Initlist(Sqlist &L)
{
L.elem=new int[MAX];
if(L.elem==NULL)
return;
else
L.length=0;
}
void createlist(Sqlist &L)
{
cin>>L.length;
for(int i=0;i<L.length;i++)
cin>>L.elem[i];
}
void printlist(Sqlist L)
{
for(int i=0;i<L.length;i++)
cout<<L.elem[i]<<'\t';
}
int emptylist(Sqlist L)
{
if(L.length==0)
return 0;
else
return 1;
}
int queryelem(Sqlist L,Elemtype e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e)
return i+1;
return 0;
}
void getelem(Sqlist L,int i,int &e)
{
if(i<1 || i>L.length)
return;
else
e=L.elem[i-1];
}
void insertelem(Sqlist &L,int i,int e)
{
if(i<1 || i>L.length+1)
return;
else
for(int k=L.length-1;k>i-1;k--)
L.elem[k+1]=L.elem[k];
L.elem[i-1]=e;
L.length ++;
}
void main()
{
Sqlist L;
Initlist(L);
createlist(L);
printlist(L);
int null;
null=emptylist(L);
if(null==0)
cout<<"it is nulllist";
else
cout<<" \nit is not nulllist";
int e;
cout<<"\nput on query elem";
cin>>e;
int i;
i=queryelem(L,e);
if(i!=0)
cout<<i;
cout<<"\n put on di ji ge elem";
cin>>i;
getelem(L,i,e);
cout<<e;
cout<<"put on insert elem and locate";
cin>>i>>e;
insertelem(L,i,e);
printlist(L);
}
|