Advertisement
Guest User

esercizio1_15_5_2017.cpp

a guest
Jun 23rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct nodo{int info; nodo* next; nodo(int a=0, nodo* b=0){info=a; next=b;}};
  5.  
  6. nodo* build(nodo* L) {
  7. int x;
  8. cin>>x;
  9. if(x!=-1) {
  10. return new nodo(x,build(L));
  11. }
  12. else
  13. return 0;
  14. }
  15.  
  16. //PRE=(L = L_0, L_1, ... è una lista di interi)
  17. int cerca(int k, nodo* L) {
  18. if(L==0)
  19. return -1;
  20. if(L->info==k)
  21. return 0;
  22. else {
  23. int x=1+cerca(k,L->next);
  24. if(x==0)
  25. return -1;
  26. else
  27. return x;
  28. }
  29. }
  30. /*POST=(ritorna il più piccolo indice i tale che L_i.chiave == k,
  31. oppure -1 se non esiste)*/
  32.  
  33. int main() {
  34. cout<<"start"<<endl;
  35. nodo* L=build(L);
  36. int key;
  37. cin>>key;
  38. int pos=cerca(key,L);
  39. if(pos!=-1)
  40. cout<<"Il valore "<<key<<" si trova in posizione "<<pos<<endl;
  41. else
  42. cout<<"Il valore "<<key<<" non si trova nella lista"<<endl;
  43. cout<<"end"<<endl;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement