C++链表3

此博客围绕C++和链表展开,虽未给出具体内容,但可知涉及C++语言中链表的相关信息技术知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
const int n=4;
struct node
{
    string note;
    string text;
    node* next;
};
class notebook
{
private:
    node* head,* p,* end;
public:
    void init()
    {
        head=new node;
       end=head;
       for(int i=0;i<n;++i)
       {
           p=new node;
           cin>>p->note;
           cin>>p->text;
           end->next=p;
           end=p;
       }
       end->next=0;
    }
    void add()
    {
        p=new node;
        cin>>p->note;
        cin>>p->text;
        end->next=p;
        end=p;
    }
    int erase(string s)
    {
        int i=0;
        p=head;
        while(p->next)
        {
        while(p->next->note==s&&p->next!=NULL)
            {
                node* p1=p->next;
                p->next=p1->next;
                delete p1;
                i++;
            }
            p=p->next;
        }
        return i;
    }
    void show()
    {
        p=head;
        while(p->next)
        {
            p=p->next;
            cout<<p->note<<endl;
            cout<<p->text<<endl<<endl<<endl;
        }
    }
    void sort()
    {
        p=head;
        int m=0;
        while(p->next)
        {
           m++;
           p=p->next;
        }
        p=head;
        string s;
        for(int i=0;i<m-1;++i)
        {
            p=head;
           for(int j=0;j<m-i-1;++j)
           {
               p=p->next;
               if(p->next->note < p->note)
               {
                    s = p->next->note;
                    p->next->note = p->note;
                    p->note = s;
                    s = p->next->text;
                    p->next->text = p->text;
                    p->text=s;
               }
           }
        }
    }
};
int main()
{
    notebook t;
    t.init();
    t.show();
    cout<<"----------------------"<<endl;
    t.add();
    t.show();
    cout<<"----------------------"<<endl;
    t.erase("s");
    t.show();
    cout<<"----------------------"<<endl;
    t.sort();
    t.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值