Advertisement
darkjessy94

sfcsfs

Dec 23rd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct nodo{
  6. char c;
  7. struct list *next;
  8. struct nodo *left, *right;
  9. };
  10.  
  11. struct list{
  12. char nome[50];
  13. char numero[15];
  14. struct list *next;
  15. };
  16.  
  17. void iniRubrica(struct nodo **p){
  18. *p = NULL;
  19. }
  20.  
  21. void stampaARB(struct nodo *p){
  22. if(p == NULL){
  23. printf("()");
  24. return;
  25. }else{
  26. printf("(%c",p->c);
  27. stampaARB(p->left);
  28. stampaARB(p->right);
  29. printf(")");
  30. }
  31. }
  32.  
  33. struct nodo* InserisciLettera(struct nodo *p, char k){
  34. struct nodo *nuovo;
  35. if(p == NULL){
  36. nuovo = malloc(sizeof(struct nodo));
  37. nuovo -> c = k;
  38. nuovo -> left = NULL;
  39. nuovo -> right = NULL;
  40. nuovo ->next = NULL;
  41. return nuovo;
  42. }else{
  43. if(p -> c < k){
  44. p->left = InserisciLettera(p->left,k);
  45. return p;
  46. }
  47. else
  48. {
  49. p->right = InserisciLettera(p->right,k);
  50. return p;
  51. }
  52. }
  53. }
  54.  
  55. struct nodo* ricercaBinaria(struct nodo *p,char k){
  56. int trovato;
  57. if(p -> c == k){
  58. return p;
  59. }
  60. else{
  61. if(p -> c < k)
  62. return ricercaBinaria(p->left,k);
  63. else
  64. return ricercaBinaria(p->right,k);
  65. }
  66. };
  67.  
  68. struct list* Inserisci_Elemento(struct list* lista,char nome[],char numero[])
  69. {
  70. struct list* prec,*curr,*new_node;
  71. prec=NULL;
  72. curr=lista;
  73. while(curr!=NULL && (strcmp(nome,curr->nome))>0) ///trova la posizione di inserimento in modo da insere l'elemento in ordine
  74. {
  75. prec=curr;
  76. curr=curr->next;
  77. }
  78. new_node=(struct nodo*)malloc(sizeof(struct nodo)); ///allocazione dinamica
  79. if(new_node==NULL){printf("Errore di allocazione");exit(1);}
  80. strcpy(new_node->nome,nome);
  81. strcpy(new_node->numero,numero);
  82.  
  83. if(prec==NULL)///inserimento in testa
  84. {
  85. printf("\n\nins testa\n");
  86. new_node->next=lista;
  87. lista=new_node;
  88. return lista;
  89. }
  90. else ///inserimento in mezzo o fondo
  91. {
  92. printf("\n\nins mezzo\n");
  93. prec->next=new_node;
  94. new_node->next=curr;
  95. return lista;
  96. }
  97. }
  98.  
  99. struct nodo* inserisciNomeNumero(struct nodo *p, char nome[50], char numero[15]){
  100. struct nodo *pos;
  101. struct list *j;
  102. pos = ricercaBinaria(p,nome[0]);
  103. //printf("\n\nLettera lista: %c\n",pos->c);
  104. pos->next=Inserisci_Elemento(pos->next,nome,numero);
  105. printf("\n\nLISTA TOTALE %c\n",pos->c);
  106. while(pos->next!=NULL)
  107. {
  108. printf("\n\nLista lettera %c\nNome: ",nome[0]); puts(pos->next->nome); printf("Numero: "); puts(pos->next->numero);
  109. pos->next=pos->next->next;
  110. }
  111.  
  112. return pos;
  113.  
  114. }
  115.  
  116. int main()
  117. {
  118. int i;
  119. char let[26] = "LFTBJPWADHKNQUYCEGIMORSVXZ";
  120. struct nodo *r;
  121. iniRubrica(&r);
  122. for(i=0;i<26;i++){
  123. r = InserisciLettera(r,let[i]);
  124. }
  125. stampaARB(r);
  126. r=inserisciNomeNumero(r,"Sergio","3338154382");
  127. r=inserisciNomeNumero(r,"Salvatore","3338154381");
  128. r=inserisciNomeNumero(r,"Sandro","3338154382");
  129.  
  130.  
  131. printf("\n\nLISTA TOTALE %c\n",r->c);
  132. while(r->next!=NULL)
  133. {
  134. printf("\n\nLista lettera %c\nNome: ",r->c); puts(r->next->nome); printf("Numero: "); puts(r->next->numero);
  135. r->next=r->next->next;
  136. }
  137.  
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement