#include<iostream.h>
#include<stddef.h>
#include<conio.h>
struct Node
{
int key;
Node *link;
};
class List
{
public:
List (int nn) { n=nn; MakeList(n) ;};
~List() { cleanList (head) ;};
void Out() { printList(head) ;};
void Work(int kk) { k=kk; p=searchPair(head); Paste(k) ;};
private:
void MakeList(int n);
void Paste (int k);
Node *searchPair(Node *p);
void printList(Node *p);
void cleanList(Node *p);
Node *head, *p;
int n,k;
};
void List::MakeList(int n)
{
int key;
cin>>key; head=new Node; head->key=key; p=head; n--;
while(n) {
cin>>key; p->link=new Node; p=p->link; p->key=key; n--;
}
p->link=NULL;
}
Node *List::searchPair(Node *p)
{
if (!p->link->link->link) return NULL;
else {
if ((p->link->key+p->link->link->key)>p->link->link->link->key) return p;
else return searchPair(p->link);
}
}
void List::Paste (int k)
{ Node *i;
i=new Node;
i->link=p->link;
i->key=k;
p->link=i;
}
void List::printList(Node *p)
{
if (p) {
cout<<p->key<<" ";
printList(p->link);
}
}
void List::cleanList(Node *p)
{
if (p) {
cleanList(p->link);
delete p;
}
}
void main()
{
clrscr();
int k,n;
cout <<"\nKol-vo elementov spiska: ";
cin >>n;
cout <<"\nNew Element: ";
cin >>k;
cout<<"\nVvedite " <<n<<" Elementov spiska\n";
List rt(n);
rt.Work(k);
rt.Out();
getch();
}