lab 2

#include <iostream>
#include <malloc.h>
using namespace std;
struct celltype
{
    int   element ;
    struct celltype  *next ;/*结点型*/
};


typedef  struct celltype *LIST; /*线性表的型*/
typedef  struct celltype  *position; /*位置型*/


void Insert(int x,position p,LIST &L)
{
    position  q;
    q=new celltype;
    q->element = x;
    q->next = p->next;
    p->next = q;
}


void Delete (position p,LIST &L)
{
    position q;
    if(p->next!=NULL)
    {
        q=p->next;
        p->next = q->next;
        delete q;
    }
}


position Locate(int x,LIST &L)
{
    position  p ;
    p = L ;
    while ( p->next != NULL )
        if  ( p->next->element == x )
            return  p ;
        else
            p = p->next ;
    return  p ;
}


int Retrieve(position p,LIST &L)
{
    return p->next->element;
}


position Previous(position p,LIST &L)
{
    position  q ;
    if  ( p ==L->next )
        cout<<"不存在前驱位置";
    else
    {
        q = L ;
        while (q->next!=p)
            q =q->next;
        return  q ;
    }
}


position Next(position p,LIST &L)
{
    position  q ;
    if  (p->next == NULL)
        cout<<"不存在后继位置";
    else
    {
        q =  p->next;
        return  q ;
    }
}


position MakeNull(LIST &L)
{
    L = new celltype;
    L->next= NULL;
    return L ;
}


position First(LIST L)
{
    return L;
}


position End(LIST L)
{
    position q;
    q = L;
    while(q->next!=NULL)
        q=q->next;
    return q;
}


int main()
{
    struct celltype *L;
    struct celltype *Head;
    L = (celltype *)malloc(sizeof(celltype));
    Head = (celltype *)malloc(sizeof(celltype));
    position p;
    Head->next = L;
    L->next = NULL;
    p = L;
    Insert(2,p,L);
    p = Locate(2,L);
    p = p->next;
    Insert(3,p,L);
    Insert(4,Locate(2,L),L);
    Insert(5,Locate(2,L),L);
    Insert(6,Locate(4,L),L);
    p = Locate(3,L);
    p = p->next;
    Insert(9,p,L);
    position a;
    a = L;
    cout<<"插入结果为:"<<endl;
    while(a->next!=NULL)
    {
        cout<<Retrieve(a,L)<<" ";
        a = a->next;
    }
    cout<<endl;
    position b;
    b = L;
    cout<<"返回L的第一个元素:"<<endl;
    cout<<Retrieve(b,L)<<endl;
    cout<<"从L中删除4:"<<endl;
    Delete(Locate(4,L),L);
    a = L;
    while(a->next!=NULL)
    {
        cout<<Retrieve(a,L)<<" ";
        a = a->next;
    }
    cout<<endl;
    cout<<"返回L中3的位置:"<<endl;
    cout<<Locate(3,L)<<endl;
    cout<<"返回L中3的下一个元素:"<<endl;
    a = Locate(3,L)->next;
    cout<<Retrieve(a,L)<<endl;
    cout<<"返回L的最后一个元素:"<<endl;
    cout<<Retrieve(a,L)<<endl;
    cout<<"删除L中的最后一个元素:"<<endl;
    Delete(a,L);
    a = L;
    while(a->next!=NULL)
    {
        cout<<Retrieve(a,L)<<" ";
        a = a->next;
    }
    cout<<endl;
    cout<<"删除L中的第一个元素:"<<endl;
    a = L;
    Delete(a,L);
    while(a->next!=NULL)
    {
        cout<<Retrieve(a,L)<<" ";
        a = a->next;
    }
    cout<<endl;
    cout<<"返回L中的第2个位置对应的元素:"<<endl;
    a = L;
    a = a->next;
    cout<<Retrieve(a,L)<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值