Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. struct lista{
  6. int numero;
  7. lista* next;
  8. };
  9. typedef lista* dio;
  10.  
  11. dio crea(dio h,int n){
  12. int a=n;
  13. while(a>0){
  14. dio p=new lista;
  15. p->numero=a;
  16. p->next=h;
  17. h=p;
  18. a=a-1;
  19. }
  20. return h;
  21. }
  22.  
  23. int somma(dio h ){
  24. int s=0;
  25. dio z=h;
  26. while (z!=NULL){
  27. s=s+z->numero;
  28. z=z->next;
  29. }
  30. //cout<<s<<endl;
  31. return s;
  32. }
  33.  
  34. void stampa(dio h){
  35. dio c=h;
  36. while(c!=NULL){
  37. cout<<c->numero;
  38. c=c->next;
  39. }
  40.  
  41. }
  42.  
  43.  
  44. dio elimina(dio h){
  45. dio e=h; dio p=h; dio d=h;
  46. while(e!=NULL){
  47. if ((e->numero%2)==0){
  48. if (e==h){
  49. h=h->next;
  50. delete e;
  51. e=h;
  52.  
  53. }else{
  54.  
  55. while (p->next!=e){p=p->next;}
  56. d=e->next;
  57. p->next=d;
  58. delete e;
  59. e=NULL;
  60. e=d;}}
  61. if (e!=NULL){
  62. e=e->next;
  63. }
  64. }
  65. return h;
  66. }
  67.  
  68.  
  69.  
  70. int main(){
  71. dio h=new lista;
  72. h=NULL;
  73.  
  74. h=crea(h,6);
  75. //stampa(h);
  76. //cout<<somma(h);
  77. elimina(h);
  78. stampa(h);
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement