Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node{
- int data;
- struct node *next;
- };
- typedef struct node Tnode;
- typedef Tnode* Tlist;
- int leggidato()
- {
- int key;
- puts("inserisci dato\n");
- scanf("%d",&key);
- return key;
- }
- ///inserimento in fondo
- void inserimento(Tlist lista,int data)
- {
- Tlist start;
- start=lista;
- while(lista->next!=start)
- {
- lista=lista->next;
- }
- lista->next=(Tnode*)malloc(sizeof(Tnode));
- lista=lista->next;
- if(lista==NULL){puts("errore allocazione\n");exit(1);}
- lista->data=data;
- lista->next=start; ///aggiusto collegamento
- }
- void inseriscitesta(Tlist lista,int key)
- {
- Tlist nn;
- nn=(Tnode*)malloc(sizeof(Tnode));
- nn->data=key;
- nn->next=lista->next;
- lista->next=nn;
- }
- void stampa(Tlist start,Tlist list)
- {
- if(list==start)
- return;
- printf("%d->",list->data);
- stampa(start,list->next);
- }
- int find(Tlist list,int key)
- {
- Tlist start;
- start=list;
- list=list->next;
- while(list!=start){
- if(list->data==key)
- return 1;
- list=list->next;
- }return 0;
- }
- void deletenode(Tlist list,int key)
- {
- Tlist start;
- start=list;
- Tlist temp;
- while(list->next!=start && (list->next)->data!=key)
- {
- list=list->next;
- }
- if(list->next==start)
- {
- puts("nodo non trovato\n");
- return;
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- }
- void cont(Tlist list)
- {
- Tlist start;
- int c=0;
- for(start=list; start->next!=list; start=start->next){
- c=c+1;}
- printf("ci sono %d nodi\n",c);
- }
- int main()
- {
- Tlist lista;
- lista=(Tnode*)malloc(sizeof(Tnode));
- lista->next=lista;
- int flag=0;
- int value;short scelta;
- while(1)
- {
- puts("digita 1 per inserire un elemento dal fondo\nDigita 2 per inserire un elemento in testa\ndigita 3 per stampare");
- puts("digita 4 per cercare un nodo\ndigita 5 per eliminare un nodo\nDigita 6 per contare i nodi presenti\nPremi 0 per uscire\n ");
- scanf("%d",&scelta);
- switch(scelta)
- {
- case 1: value=leggidato(); inserimento(lista,value);break;
- case 2: value=leggidato(); inseriscitesta(lista,value); break;
- case 3:puts("\n");stampa(lista,lista->next);puts("\n"); break;
- case 4: value=leggidato();flag=find(lista,value);if(flag)puts("nodo presente\n");else puts("nodo non presente"); break;
- case 5: value=leggidato(); deletenode(lista,value);break;
- case 6: cont(lista);break;
- case 0: return 0;
- }
- system("pause");
- system("cls");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement