Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2. ************************************************************MAIN DI PROVA*******************************************************
  3. #include <stdio.h>
  4. #include "lista.h"
  5. #include "persona.h"
  6. int main(int argc, char **argv)
  7. {
  8. list_t lista=allocaLista();
  9. persona p;
  10. p=NewPersona("nino","musmeci");
  11. SortListIns(lista,p);
  12. p=NewPersona("giovanni","rossi");
  13. SortListIns(lista,p);
  14. p=NewPersona("francesco","russo");
  15. SortListIns(lista,p);
  16. p=NewPersona("mario","ancona");
  17. SortListIns(lista,p);
  18. StampaLista(lista);
  19.  
  20.  
  21. return 0;
  22. }
  23.  
  24.  
  25. *****************************************************INTERFACCIA LISTA****************************************************
  26.  
  27. #ifndef H_LISTA
  28. #define H_LISTA
  29.  
  30. #include "persona.h"
  31.  
  32. typedef struct list *list_t;
  33. list_t allocaLista();
  34. void SortListIns(list_t l,persona p);
  35. void StampaLista(list_t l);
  36. #endif
  37. ***********************************************************IMPLEMENTAZIONE*******************************************************
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "lista.h"
  42.  
  43. typedef struct node *link;
  44. // definizione nodo della lista
  45. struct node{
  46. persona p;
  47. link next;
  48. };
  49.  
  50. //definizione wrapper della lista
  51. struct list{
  52. link head; // puntatore alla testa della lista;
  53. int N; //lunghezza della lista;
  54.  
  55. };
  56. // funzione di creazione della lista
  57. list_t allocaLista(){
  58. list_t l;
  59. l=malloc(sizeof(*l));
  60. l->head=NULL;
  61. l->N=0;
  62. return l;
  63. }
  64.  
  65. // funzione di allocazione di un nodo
  66.  
  67. link NewNode(persona p,link t){
  68. link x;
  69. x=malloc(sizeof(*x));
  70. x->p=p;
  71. x->next=t;
  72. return x;
  73.  
  74. }
  75.  
  76. // funzione di inserimento in ordine
  77.  
  78. void SortListIns(list_t l,persona pe){
  79. link x,p;
  80. // lista vuota -> inserimento in testa
  81. if(l->head==NULL || ConfrontaPersona(l->head->p,pe)>0)
  82. l->head=NewNode(pe,l->head);
  83. // devo cercare la posizione dove inserire p;
  84. else{
  85. for(x=l->head->next,p=l->head; x!=NULL && ConfrontaPersona(l->head->p,pe)>0;p=x,x=x->next);
  86. p->next=NewNode(pe,x);
  87.  
  88. }
  89. }
  90.  
  91. void StampaLista(list_t l){
  92. link x;
  93. for(x=l->head;x!=NULL;x=x->next){
  94. Stampa(x->p);
  95. }
  96. }
  97. ***************************************************************PERSONA***************************************************************
  98. #ifndef H_PERSONA
  99. #define H_PERSONA
  100.  
  101. typedef struct {
  102. char *nome;
  103. char *cognome;
  104. }persona;
  105.  
  106.  
  107. int ConfrontaPersona(persona p1,persona p2);
  108. persona NewPersona(char *s1,char *s2);
  109. void Stampa(persona p);
  110. #endif
  111.  
  112. ************************************************************************************************************************************
  113. #include "persona.h"
  114. #include <stdlib.h>
  115. #include <string.h>
  116. #include <stdio.h>
  117.  
  118.  
  119. persona NewPersona(char *s1,char *s2){
  120. persona p;
  121. p.nome=strdup(s1);
  122. p.cognome=strdup(s2);
  123. return p;
  124. }
  125.  
  126. int ConfrontaPersona(persona p1,persona p2){
  127.  
  128. int confronto;
  129.  
  130. confronto=strcmp(p1.cognome,p2.cognome);
  131. if(confronto==0)
  132. return strcmp(p1.nome,p2.nome);
  133. return confronto;
  134.  
  135. }
  136.  
  137. void Stampa(persona p){
  138. printf("%s %s\n",p.nome,p.cognome);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement