Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct lista{
- int numero;
- lista* next;
- };
- typedef lista* dio;
- dio crea(dio h,int n){
- int a=n;
- while(a>0){
- dio p=new lista;
- p->numero=a;
- p->next=h;
- h=p;
- a=a-1;
- }
- return h;
- }
- int somma(dio h ){
- int s=0;
- dio z=h;
- while (z!=NULL){
- s=s+z->numero;
- z=z->next;
- }
- //cout<<s<<endl;
- return s;
- }
- void stampa(dio h){
- dio c=h;
- while(c!=NULL){
- cout<<c->numero;
- c=c->next;
- }
- }
- dio elimina(dio h){
- dio e=h; dio p=h; dio d=h;
- while(e!=NULL){
- if ((e->numero%2)==0){
- if (e==h){
- h=h->next;
- delete e;
- e=h;
- }else{
- while (p->next!=e){p=p->next;}
- d=e->next;
- p->next=d;
- delete e;
- e=NULL;
- e=d;}}
- if (e!=NULL){
- e=e->next;
- }
- }
- return h;
- }
- int main(){
- dio h=new lista;
- h=NULL;
- h=crea(h,6);
- //stampa(h);
- //cout<<somma(h);
- elimina(h);
- stampa(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement