SHARE
TWEET

Untitled

a guest May 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct nodo {
  5.   int chiave;
  6.   nodo* next;
  7.   nodo(int a = 0, nodo* b = NULL){chiave = a; next = b;}
  8. };
  9.  
  10. nodo* inserisci_in_ordine(int k, nodo* L) {
  11.   if(L == NULL || L->chiave >= k) {
  12.     nodo* newN = new nodo();
  13.     newN->chiave = k;
  14.     newN->next = L;
  15.     return newN;
  16.   }
  17.   else {
  18.     L->next = inserisci_in_ordine(k, L->next);
  19.     return L;
  20.   }
  21. }
  22.  
  23. void stampa(nodo* L) {
  24.   if(L) {
  25.     cout << L->chiave << " ";
  26.     stampa(L->next);
  27.    }
  28. }
  29.  
  30. int main() {
  31.   nodo* L = NULL;
  32.   int k;
  33.   bool exit = false;
  34.  
  35.   cout << "start" << endl;
  36.  
  37.   while(!exit) {
  38.     cin >> k;
  39.     if(k == -1)
  40.       exit = true;
  41.     else
  42.       L = inserisci_in_ordine(k, L);
  43.   }
  44.  
  45.   stampa(L);
  46.  
  47.   cout << "end" << endl;
  48. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Top