codice:
#include <iostream>
using namespace std;
struct lista{
int info;
lista*next;
};
typedef lista* punt;
punt head;
punt insert(int, punt);
punt append(int, punt);
void insdopovalore(int, int, punt);
void insinpos(int, int, punt);
punt creanodo(int, punt);
punt del(int, punt);
void dellist(punt);
bool cerca(int, punt);
int conta(punt);
void leggi(punt);
int main(){
int a=1;
while(a>0){
punt tmp=head;
cout<<" 1)Inserisci"<<endl<<" 2)Appendi"<<endl<<" 3)Inserisci dopo valore"<<endl<<" 4)Inserisci dopo puntatore"<<endl<<" 5)Cancella"<<endl<<" 6)Cancella lista"<<endl<<" 7)Cerca"<<endl<<" 8)Conta nodi"<<endl<<" 9)Visualizza"<<endl<<" 0)Esci"<<endl;
int n, val, x;
cin>>n;
switch(n){
case 1:
cout<<"Inserire valore"<<endl;
cin>>val;
insert(val, head);
system("PAUSE");
break;
case 2:
cout<<"Inserire valore"<<endl;
cin>>val;
append(val, tmp);
system("PAUSE");
break;
case 3:
cout<<"Inserire valore e info dopo la quale inserire"<<endl;
cin>>val;
cin>>x;
insdopovalore(val, x, tmp);
system("PAUSE");
break;
case 4:
cout<<"Inserire valore e numero posizione"<<endl;
cin>>val;
cin>>x;
insinpos(val, x, tmp);
system("PAUSE");
break;
case 5:
cout<<"Inserire valore"<<endl;
cin>>val;
del(val, tmp);
system("PAUSE");
break;
case 6:
dellist(head);
system("PAUSE");
break;
case 7:
if(cerca(val, tmp)){
cout<<"Elemento presente"<<endl;
}else{
cout<<"Elemento assente"<<endl;
}
system("PAUSE");
break;
case 8:
x=conta(tmp);
cout<<x<<endl;
system("PAUSE");
break;
case 9:
leggi(head);
system("PAUSE");
break;
default:
a--;
break;
}
system("cls");
}
system("PAUSE");
return 0;
}
punt creanodo(int val, punt tmp){
tmp=new(lista);
tmp->info=val;
tmp->next=NULL;
}
punt insert(int val, punt head){
if(head==NULL){
head=creanodo(val, head);
}else{
punt tmp;
creanodo(val, tmp);
tmp->next=head;
head=tmp;
}
}
punt append(int val, punt head){
if(head==NULL){
head=creanodo(val, head);
}else{
append(val, head->next);
}
}
void insdopovalore(int val, int x, punt head){
punt aux;
if(head!=NULL){
if(head->info==x){
creanodo(val, aux);
aux->next=head->next->next;
head->next=aux;
}else{
insdopovalore(val, x, head->next);
}
}else{
cout<<"Lista vuota o valore x non trovato"<<endl;
}
}
void insinpos(int val, int x, punt head){
if(x==1){
insert(val, head);
}
for(int i=1;i<x;i++){
if(head!=NULL){
head=head->next;
}
}
if(head->next==NULL){
append(val, head);
}else{
insdopovalore(val, head->info, head);
}
}
punt del(int val, punt head){
punt aux;
if(head==NULL){
cout<<"Lista vuota o elemento non trovato"<<endl;
}else{
punt prev=NULL;
if(head->info==val){
aux=head;
head=head->next;
free(aux);
}else{
del(val, head->next);
}
}
}
void dellist(punt head){
punt tmp;
while(head!=NULL){
tmp=head;
head=head->next;
free(tmp);
}
}
bool cerca(int val, punt head){
if(head==NULL){
return false;
}else if(head->info==val){
return true;
}else{
cerca(val, head->next);
}
}
int conta(punt head){
int a=0;
if(head!=NULL){
a=conta(head->next)+1;
}
return a;
}
void leggi(punt head){
if(head!=NULL){
cout<<head->info<<endl;
leggi(head->next);
}
}