C++实验15

一、顺序插入链表

#include<iostream>
using namespace std;
class node
{
public:
    float data;
    node *link;
};
node *insert(node *head,float n)
{
    node *ptem=new node;
    ptem->data=n;
    if(n==-1)      //最后-1作为结束符也申请了堆空间,释放掉
    {
        delete ptem;
        return head;
    }
    if(!head)       //链表未存在时
    {
        head=ptem;
        head->link=0;
        return head;
    }
    node *p1,*p2;    //定义p1=head用来while(p1),p2用于断开链表中间时保存。

    p1=head;

    if(n<head->data)     //插入元素在列表头的情况,因head一直指向链头,故可以在循环外直接比较
    {
        ptem->link=head;
        return ptem;
    }

    while(p1)      //循环在表中找到位置
    {
        if(p1->link==0&&p1->data<=n)   //插入元素在列表尾的情况
        {
                p1->link=ptem;
                ptem->link=0;
                return head;
        }           
        if(n<=p1->link->data&&n>=p1->data)  //置于表中
        {
            p2=p1->link;                   //断开链表前用p2保存断开位置后面的指针
            p1->link=ptem;
            ptem->link=p2;
            return head;
        }
        p1=p1->link;
    }

}
void print(node *head)   //输出链表
{
    while(head)
    {
        cout<<head->data<<'\t';
        head=head->link;
    }
}
void del(node *head)    //释放链表空间
{
    node *p;
    while(head)
    {
        p=head->link;
        delete head;
        head=p;
    }

}
void main()
{
    node *head=0;
    int len;
    float n;
    cout<<"请输入要穿件链表的值,以-1结束\n";
    while(true)
    {
        cin>>n;
        if(n==-1)
            break;
        head=insert(head,n);
    }
    print(head);
    del(head);
}

二、输入数据并处理

#include<iostream>
using namespace std;
struct stunode
{
    int num;
    float score;
    stunode *link;
};
stunode *insert(stunode *head,int id,float n)//按score从大到小排列
{
    stunode *ptem=new stunode;
    ptem->num=id;
    ptem->score=n;
    if(n==-1)      //最后-1作为结束符也申请了堆空间,释放掉
    {
        delete ptem;
        return head;
    }
    if(!head)       //链表未存在时
    {
        head=ptem;
        head->link=0;
        return head;
    }
    stunode *p1,*p2;    //定义p1=head用来while(p1),p2用于断开链表中间时保存。

    p1=head;

    if(n>head->score)     //插入元素在列表头的情况,因head一直指向链头,故可以在循环外直接比较
    {
        ptem->link=head;
        return ptem;
    }

    while(p1)      //循环在表中找到位置
    {
        if(p1->link==0&&p1->score>=n)   //插入元素在列表尾的情况
        {
            p1->link=ptem;
            ptem->link=0;
            return head;
        }           
        if(n>=p1->link->score&&n<=p1->score)  //置于表中
        {
            p2=p1->link;                   //断开链表前用p2保存断开位置后面的指针
            p1->link=ptem;
            ptem->link=p2;
            return head;
        }
        p1=p1->link;
    }

}
void print(stunode *head)   //输出链表
{
    cout<<"ID\t"<<"Score"<<endl;
    while(head)
    {
        cout<<head->num<<'\t'<<head->score<<endl;
        head=head->link;
    }
}
void del(stunode *head);
void del_fail(stunode *head,float n)   //按照输入的达标线删除达标线以下的链表数据
{
    while(head)
    {
        if(head->score>=n&&head->link->score<n)
        {
            head->link=0;
            del(head->link);
            break;
        }
        head=head->link;
    }
}
void del(stunode *head)    //释放链表空间
{
    stunode *p;
    while(head)
    {
        p=head->link;
        delete head;
        head=p;
    }

}
void main()
{
    stunode *head=0;
    int num;
    float n;
    cout<<"请输入编号、总分,以-1 -1结束\n";
    while(true)
    {
        cin>>num;
        cin>>n;
        if(n==-1)
            break;
        head=insert(head,num,n);
    }
    print(head);
    float fail_n;
    cout<<"请输入达标线:\n";
    cin>>fail_n;
    del_fail(head,fail_n);
    cout<<"删除不达标的数据后为:\n";
    print(head);
    del(head);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值