C++实验15

来源:互联网 发布:如何用matlab预测数据 编辑:程序博客网 时间:2024/06/06 01:56

一、顺序插入链表

#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);}