Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct LIST{
LIST *previous;
int data;
};
void add(LIST **top, int data){
int minmax = 32767;
LIST *temptop = new LIST;
LIST *temp = new LIST;
LIST *temp2 = new LIST;
LIST *newelem = new LIST;
temptop = *top;
while(temptop){
if((temptop->data>data)&&(minmax>data)){
minmax = temptop->data;
temp = temptop;
}
temptop = temptop->previous;
}
temp2 = temp->previous;
newelem->previous = temp2;
newelem->data = data;
temp->previous = newelem;
}
void init(LIST **top){
LIST *last = new LIST;
(*top)->data = 1;
(*top)->previous = NULL;
last->data = 32767;
last->previous = *top;
*top = last;
}
void print(LIST *top){
top = top->previous;
while(top){
cout<<top->data<<endl;
top = top->previous;
}
}
void main(){
clrscr();
LIST *top = new LIST;
init(&top);
for(int i = 0; i++<20; add(&top, random(40)+5) );
int newelem;
cout<<"\nEnter new element : ";
cin>>newelem;
add(&top, newelem);
print(top);
}