Kod: Zaznacz cały
#include <cstdlib>
#include <iostream>
using namespace std;
class Element
{
public:
Element *next_left, *next_right, *prev;
int number;
Element()
{
next_left=NULL;
next_right=NULL;
prev=NULL;
number=0;
}
};
class Head
{
public:
Element *first;
int i=0;
void add(int _number)
{
if(first==NULL)
{
first=new Element;
first->number=_number;
cout << "Element pierwszy" << endl;
}
else
{
Element *tmp=first;
Element *prev_;
cout << "tmp=first" << tmp << endl;
while(tmp!=NULL)
{
prev_=tmp;
cout << "prev_=tmp" << prev_ << endl;
if(_number <= tmp->number)
{
tmp=tmp->next_left;
cout << "tmp" << tmp << endl;
i=0;
}
else
{
tmp=tmp->next_right;
cout << "tmp" <<tmp << endl;
i=1;
}
}
cout << "prev_" << prev_ << endl;
Element *ptr=new Element;
ptr->number=_number;
ptr->prev=prev_;
cout << "prev" << ptr->prev << endl;
cout << "i" << i << endl;
if(i==1)
{
prev_->next_left=ptr;
cout << "ptr" << ptr << endl;
}
else
{
prev_->next_right=ptr;
cout << "ptr" << ptr << endl;
}
cout << "---------------------------------------------------" << endl;
}
}
void show(int _number)
{
Element *tmp=first;
cout << tmp->number << endl;
while(tmp!=NULL&&tmp->number!=_number)
{
if(_number <= tmp->number)
{
tmp=tmp->next_left;
}
else
{
tmp=tmp->next_right;
}
if(tmp!=NULL)
{
cout << tmp->number << endl;
}
}
}
Head()
{
first=NULL;
}
};
int main()
{
Head h;
h.add(10);
h.add(12);
h.add(5);
h.add(6);
h.add(15);
h.add(1);
h.add(3);
h.show(3);
return 0;
}