Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream f("date.in");
- ofstream g("date.out");
- struct node{
- int inf;
- node *urm, *ant;
- };
- node *prim, *ultim;
- void creare(int x){
- prim = new node;
- prim -> inf = x;
- prim -> urm = NULL;
- prim -> ant = NULL;
- ultim = prim;
- }
- void afisare(){
- node *p = prim;
- while(p != NULL){
- g << p -> inf << " ";
- p = p -> urm;
- }
- }
- void inseraref(int x){
- node *p = new node;
- p -> inf = x;
- p -> ant = NULL;
- p -> urm = prim;
- prim -> ant = p;
- prim = p;
- }
- void inserareb(int x){
- node *p = new node;
- p -> inf = x;
- p -> ant = ultim;
- p -> urm = NULL;
- ultim -> urm = p;
- ultim = p;
- }
- void inserare(int x){
- node *p = prim;
- while(p != NULL){
- if(p -> inf <= x && p -> urm -> inf >= x){
- node *t = new node;
- t -> inf = x;
- t -> ant = p;
- t -> urm = p -> urm;
- p -> urm -> ant = t;
- p -> urm = t;
- break;
- }
- p = p -> urm;
- }
- }
- void afisare2(){
- node *p = ultim;
- while(p != NULL){
- g << p -> inf << " ";
- p = p -> ant;
- }
- }
- int main(){
- int x;
- f >> x;
- creare(x);
- while(f >> x){
- if(x < prim -> inf)
- inseraref(x);
- else
- if(x > ultim -> inf)
- inserareb(x);
- else
- inserare(x);
- }
- afisare2();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement