int fine_max()
{
int max=head.next->data;
node* p=head.next->next;
while(p)
{
if(max<p->data)
max=p->data;
p=p->next;
}
return max;
}
void erase_data(const int& max)
{
node* cur=head.next;
node* pre=&head;
while(cur)
{
if(cur->data==max)
{
node* saved=cur;
pre->next=cur->next;
cur=cur->next;
delete saved;
}
else
{
pre=cur;
cur=cur->next;
}
}
}
完整代码如下:
#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node head;
int len;
node* creat()
{
head.next=NULL;
node* r=&head;
node* p;
for( int i=0;i<len;i++)
{
int data;
cin>>data;
p=new node;
p->data=data;
p->next=r->next;
r->next=p;
r=p;
}
r->next=0;
return &head;
}
int fine_max()
{
int max=head.next->data;
node* p=head.next->next;
while(p)
{
if(max<p->data)
max=p->data;
p=p->next;
}
return max;
}
void erase_data(const int& max)
{
node* cur=head.next;
node* pre=&head;
while(cur)
{
if(cur->data==max)
{
node* saved=cur;
pre->next=cur->next;
cur=cur->next;
delete saved;
}
else
{
pre=cur;
cur=cur->next;
}
}
}
void show()
{
node* p=head.next;
while(p)
{
cout<<p->data;
if(p->next)
cout<<' ';
p=p->next;
}
cout<<endl;
}
void discard()
{
node* p=head.next;
while(p)
{
node* q=p;
p=p->next;
delete q;
}
}
int main()
{
while(cin>>len)
{
creat();
int max=fine_max();
erase_data(max);
show();
discard();
}
return 0;
}