Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : List_Nopari_Giusè.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- using namespace std;
- struct nodo {
- int val;
- nodo* next;
- };
- nodo* ins_in_testa(nodo* root)
- {
- int n;
- nodo* tmp=root;
- cout<<"Dammi il valore da inserire: \n";
- cin>>n;
- root=new nodo;
- root->val=n;
- root->next=tmp;
- return(root);
- }
- nodo* ins_in_coda(nodo* root)
- {
- nodo* tmp=root;
- nodo* nuovonodo;
- int n;
- cout<<"Dammi il valore da inserire: \n";
- cin>>n;
- nuovonodo=new nodo;
- nuovonodo->val=n;
- nuovonodo->next=NULL;
- if (root==NULL)
- root=nuovonodo;
- else
- {
- while(tmp->next!=NULL) tmp=tmp->next;
- tmp->next=nuovonodo;
- }
- return(root);
- }
- nodo* ins_ord_c (nodo* root, int a)
- {
- if (root==NULL) { //caso lista vuota
- root=new nodo;
- root->val=a;
- root->next=NULL;
- }
- else { //ci sono altri elementi
- if (a<(root->val)) {//da inserire in cima alla lista
- nodo* tmp=root;
- root=new nodo;
- root->val=a;
- root->next=tmp;
- }
- else { //caso generale
- nodo*tmp1=root;
- nodo*tmp2=root;
- while ( a >(tmp1->val) ) tmp1=tmp1->next; //quella successiva alla nostra
- while ((tmp2->next)!=tmp1) tmp2=tmp2->next; //tmp2 punta la precendente
- tmp2->next=new nodo;
- tmp2->next->val=a;
- tmp2->next->next=tmp1;
- }
- }
- return root;
- };
- nodo* crea_lista(nodo* root)
- {
- char a;
- do
- {
- root=ins_in_coda(root);
- cout<<"Vuoi inserire un nuovo elemento?(s/n)";
- cin>>a;
- }
- while(a=='s');
- return(root);
- }
- nodo* crea_lista_ord(nodo* root)
- {
- char a;
- int b;
- do
- {
- cout<<"Dammi il valore: \n";
- cin>>b;
- root=ins_ord_c(root,b);
- cout<<"Vuoi inserire un nuovo elemento?(s/n)";
- cin>>a;
- }
- while(a=='s');
- return(root);
- }
- void stampa(nodo* root)
- {
- while(root!=NULL)
- {
- cout<<root->val<<" ";
- root=root->next;
- }
- cout<<"\n";
- }
- nodo* rim_pari(nodo* root)
- {
- nodo* curr=root;
- if (root==NULL) cout<<"Lista Vuota\n"; //Messaggio in caso lista vuota
- while (curr!=NULL)
- {
- if ( (curr->val)%2!=0 ) {(curr->val)=(curr->val)*3; curr=curr->next;} //Caso Dispari
- else
- {
- if (curr==root) //Se curr è uguale a root, allora è il primo nodo della lista
- {
- curr=curr->next;
- delete root;
- root=curr;
- }
- else
- {
- nodo* tmp1=root;
- nodo* tmp2=curr;
- while ((tmp1->next)!=curr) tmp1=tmp1->next;
- curr=curr->next;
- delete tmp2;
- tmp1->next=curr;
- }
- }
- }
- return (root);
- }
- int main()
- {
- nodo* root=NULL;
- root=crea_lista_ord(root);
- stampa(root);
- /*nodo *puntlist = NULL;
- puntlist = crea_lista (puntlist);
- stampa (puntlist);
- puntlist=rim_pari(puntlist);
- stampa(puntlist);*/
- return 0;
- }
Add Comment
Please, Sign In to add comment