Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct nodo{
  8.  
  9.     int info;
  10.  
  11.     nodo* next;
  12.  
  13.     nodo(int a=0, nodo* b=0){
  14.  
  15.         info=a; next=b;
  16.     }
  17. };
  18.  
  19.  
  20.  
  21. //crea nodo
  22.  
  23. nodo* crea(int dim, ifstream & IN){
  24.  
  25.     if(dim==0){
  26.  
  27.         return NULL;
  28.     }
  29.  
  30.     nodo* temp;
  31.    
  32.     temp= new nodo;
  33.    
  34.     IN >> temp-> info;
  35.    
  36.     temp-> next = crea(dim-1, IN);
  37.    
  38.     return temp;
  39.  
  40. }
  41.  
  42. //cancella nodo
  43.  
  44. nodo* del_z(nodo* n, int z){
  45.  
  46.     nodo * temp;
  47.    
  48.     temp->info =z;
  49.    
  50.     if ( n->next != temp) {
  51.    
  52.         return NULL;
  53.     }
  54.  
  55.     while(n->next!=temp){
  56.    
  57.         n=n->next;
  58.     }
  59.    
  60.     n->next=n->next;
  61.    
  62.     delete temp;
  63.    
  64.     del_z(n, z);
  65.    
  66.     return n;
  67.  
  68. }
  69.  
  70. //stampa
  71.  
  72.  
  73. void stampa(ofstream & OUT, int dim, nodo* x){
  74.  
  75.     if (dim==0){
  76.  
  77.         OUT<<" Fine";
  78.         return;
  79.    
  80.     }
  81.  
  82.     else if(dim==1){
  83.        
  84.         x->next;
  85.         OUT<<x->info;
  86.         OUT<<endl<<" Fine";
  87.         return;
  88.     }
  89.  
  90.     OUT<< x->info;
  91.     OUT<<"->";
  92.  
  93.     stampa(OUT, dim-1, x->next);
  94.  
  95. }
  96.  
  97. //main del programma
  98.  
  99. int main(){
  100.  
  101.  ifstream IN("input");
  102.  ofstream OUT("output");
  103.  
  104.  int dim;
  105.  int z;
  106.  
  107.  IN>>dim;
  108.  IN>>z;
  109.  
  110.  nodo* n = crea(dim, IN);
  111.  
  112.  nodo* x = del_z(n, z);
  113.  
  114.  stampa(OUT, dim, x);
  115.  
  116.  
  117.  return 0;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement