#include <iostream>
#define max 100
using namespace std;
typedef int position;
struct hang
{
int elements[max];
int last;
};
void Insert(int x, position p, struct hang &L)
{
position q;
if(L.last>=max-1)
cout<<"表满";
else if((p>L.last+1)||(p<0))
cout<<"指定位置不存在";
else
{
for(q=L.last; q>=p; q--)
L.elements[q+1] = L.elements[q];
L.elements[p] = x;
L.last += 1;
}
}
void Delete(position p, struct hang &L)
{
position q;
if((p>L.last)||(p<0))
cout<<"指定位置不存在";
else
{
L.last = L.last-1;
for(q=p; q<=L.last; q++)
L.elements[q] = L.elements[q+1];
}
}
position Locate(int x,struct hang L)
{
position q;
for(q=1; q<=L.last; q++)
if(L.elements[q]==x)
return(q);
return (L.last+1);
}
int Retrieve(position p,struct hang L)
{
if(p>L.last)
cout<<"指定位置不存在";
else
return L.elements[p];
}
position Previous(position p,struct hang L)
{
if((p<=1)||(p>L.last))
cout<<"前驱位置不存在";
else
return p-1;
}
position Next(position p,struct hang L)
{
if((p<1)||(p>=L.last))
cout<<"前驱位置不存在";
else
return p+1;
}
position MakeNull(struct hang &L)
{
L.last = 0;
return (L.last+1);
}
position End(struct hang L)
{
return L.last+1;
}
int main()
{
struct hang L;
L.last = 0;
/* int i;
for(i=0;i<max;i++)
{
L.elements[i] = i+1;
L.last++;
}
*/
Insert(2,0,L);
Insert(3,1,L);
Insert(4,0,L);
Insert(5,1,L);
Insert(6,0,L);
Insert(9,5,L);
cout<<"插入操作的结果:"<<endl;
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L的第一个元素:"<<endl;
cout<<Retrieve(0,L)<<endl;
cout<<"从L中删除4:"<<endl;
Delete(Locate(4,L),L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L中3的位置:"<<endl;
cout<<Locate(3,L)<<endl;
cout<<"返回L中3的下一个元素"<<endl;
cout<<Retrieve(Next(Locate(3,L),L),L)<<endl;
cout<<"返回L的最后一个元素:"<<endl;
cout<<Retrieve(L.last-1,L)<<endl;
cout<<"删除L中的最后一个元素:"<<endl;
Delete(L.last-1,L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"删除L中的第一个元素:"<<endl;
Delete(0,L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L中的第2个位置对应的元素:"<<endl;
cout<<Retrieve(2,L)<<endl;
cout<<"最后结果为:"<<endl;
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
/*
int a;
a=Locate(x, L);
int b;
b=Retrieve(p, L);
Delete(p, L);
int c;
c=Previous(p, L);
int d;
d=Next(p, L);
int e;
e=MakeNull(L);
int f;
f=First( L);
int g;
g=End(L);
*/
}
#define max 100
using namespace std;
typedef int position;
struct hang
{
int elements[max];
int last;
};
void Insert(int x, position p, struct hang &L)
{
position q;
if(L.last>=max-1)
cout<<"表满";
else if((p>L.last+1)||(p<0))
cout<<"指定位置不存在";
else
{
for(q=L.last; q>=p; q--)
L.elements[q+1] = L.elements[q];
L.elements[p] = x;
L.last += 1;
}
}
void Delete(position p, struct hang &L)
{
position q;
if((p>L.last)||(p<0))
cout<<"指定位置不存在";
else
{
L.last = L.last-1;
for(q=p; q<=L.last; q++)
L.elements[q] = L.elements[q+1];
}
}
position Locate(int x,struct hang L)
{
position q;
for(q=1; q<=L.last; q++)
if(L.elements[q]==x)
return(q);
return (L.last+1);
}
int Retrieve(position p,struct hang L)
{
if(p>L.last)
cout<<"指定位置不存在";
else
return L.elements[p];
}
position Previous(position p,struct hang L)
{
if((p<=1)||(p>L.last))
cout<<"前驱位置不存在";
else
return p-1;
}
position Next(position p,struct hang L)
{
if((p<1)||(p>=L.last))
cout<<"前驱位置不存在";
else
return p+1;
}
position MakeNull(struct hang &L)
{
L.last = 0;
return (L.last+1);
}
position End(struct hang L)
{
return L.last+1;
}
int main()
{
struct hang L;
L.last = 0;
/* int i;
for(i=0;i<max;i++)
{
L.elements[i] = i+1;
L.last++;
}
*/
Insert(2,0,L);
Insert(3,1,L);
Insert(4,0,L);
Insert(5,1,L);
Insert(6,0,L);
Insert(9,5,L);
cout<<"插入操作的结果:"<<endl;
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L的第一个元素:"<<endl;
cout<<Retrieve(0,L)<<endl;
cout<<"从L中删除4:"<<endl;
Delete(Locate(4,L),L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L中3的位置:"<<endl;
cout<<Locate(3,L)<<endl;
cout<<"返回L中3的下一个元素"<<endl;
cout<<Retrieve(Next(Locate(3,L),L),L)<<endl;
cout<<"返回L的最后一个元素:"<<endl;
cout<<Retrieve(L.last-1,L)<<endl;
cout<<"删除L中的最后一个元素:"<<endl;
Delete(L.last-1,L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"删除L中的第一个元素:"<<endl;
Delete(0,L);
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
cout<<"返回L中的第2个位置对应的元素:"<<endl;
cout<<Retrieve(2,L)<<endl;
cout<<"最后结果为:"<<endl;
for(int i=0;i<L.last;i++)
cout<<L.elements[i]<<" ";
cout<<endl;
/*
int a;
a=Locate(x, L);
int b;
b=Retrieve(p, L);
Delete(p, L);
int c;
c=Previous(p, L);
int d;
d=Next(p, L);
int e;
e=MakeNull(L);
int f;
f=First( L);
int g;
g=End(L);
*/
}