lab1

#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);
    */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值