Advertisement
darkjessy94

circular list - gabbo

Sep 2nd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct node{
  5. int data;
  6. struct node *next;
  7. };
  8. typedef struct node Tnode;
  9. typedef Tnode* Tlist;
  10.  
  11.  
  12. int leggidato()
  13. {
  14. int key;
  15. puts("inserisci dato\n");
  16. scanf("%d",&key);
  17. return key;
  18. }
  19. ///inserimento in fondo
  20. void inserimento(Tlist lista,int data)
  21. {
  22. Tlist start;
  23. start=lista;
  24. while(lista->next!=start)
  25. {
  26. lista=lista->next;
  27. }
  28. lista->next=(Tnode*)malloc(sizeof(Tnode));
  29. lista=lista->next;
  30. if(lista==NULL){puts("errore allocazione\n");exit(1);}
  31. lista->data=data;
  32. lista->next=start; ///aggiusto collegamento
  33. }
  34.  
  35. void inseriscitesta(Tlist lista,int key)
  36. {
  37. Tlist nn;
  38. nn=(Tnode*)malloc(sizeof(Tnode));
  39. nn->data=key;
  40. nn->next=lista->next;
  41. lista->next=nn;
  42. }
  43.  
  44. void stampa(Tlist start,Tlist list)
  45. {
  46. if(list==start)
  47. return;
  48. printf("%d->",list->data);
  49. stampa(start,list->next);
  50. }
  51.  
  52. int find(Tlist list,int key)
  53. {
  54. Tlist start;
  55. start=list;
  56. list=list->next;
  57. while(list!=start){
  58. if(list->data==key)
  59. return 1;
  60. list=list->next;
  61. }return 0;
  62.  
  63. }
  64.  
  65.  
  66. void deletenode(Tlist list,int key)
  67. {
  68. Tlist start;
  69. start=list;
  70. Tlist temp;
  71.  
  72. while(list->next!=start && (list->next)->data!=key)
  73. {
  74. list=list->next;
  75. }
  76. if(list->next==start)
  77. {
  78. puts("nodo non trovato\n");
  79. return;
  80. }
  81. temp=list->next;
  82. list->next=temp->next;
  83. free(temp);
  84. }
  85.  
  86. void cont(Tlist list)
  87. {
  88. Tlist start;
  89. int c=0;
  90. for(start=list; start->next!=list; start=start->next){
  91. c=c+1;}
  92. printf("ci sono %d nodi\n",c);
  93. }
  94.  
  95.  
  96.  
  97. int main()
  98. {
  99. Tlist lista;
  100. lista=(Tnode*)malloc(sizeof(Tnode));
  101. lista->next=lista;
  102. int flag=0;
  103. int value;short scelta;
  104. while(1)
  105. {
  106. puts("digita 1 per inserire un elemento dal fondo\nDigita 2 per inserire un elemento in testa\ndigita 3 per stampare");
  107. 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 ");
  108. scanf("%d",&scelta);
  109.  
  110. switch(scelta)
  111. {
  112. case 1: value=leggidato(); inserimento(lista,value);break;
  113. case 2: value=leggidato(); inseriscitesta(lista,value); break;
  114. case 3:puts("\n");stampa(lista,lista->next);puts("\n"); break;
  115. case 4: value=leggidato();flag=find(lista,value);if(flag)puts("nodo presente\n");else puts("nodo non presente"); break;
  116.  
  117. case 5: value=leggidato(); deletenode(lista,value);break;
  118. case 6: cont(lista);break;
  119. case 0: return 0;
  120.  
  121. }
  122. system("pause");
  123. system("cls");
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement