#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;
}
C++链表3
最新推荐文章于 2025-03-29 19:07:30 发布