Advertisement
Denny707

addNodo

Jun 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct nodo{
  4. int codice;
  5. int quantita;
  6. struct nodo* link;
  7. };
  8. struct nodo *head=NULL;
  9. struct nodo *nuovaLista=NULL;
  10.  
  11. /* FASE 0: impostare head come var globale = NULL
  12. FASE 1: creazione del mio nodo
  13. FASE 2: verifica esistenza head
  14. FASE 3: in assenza di head-> head := nuovo
  15. FASE 4: altrimenti mi scorro la lista
  16. fino a trovare l'ultimo elemento
  17. FASE 5: ultimoElemento->link := nuovo
  18. FASE 6: ritorno head
  19. FASE 7: nella call: head=addNodo(head,...);
  20. */
  21.  
  22. struct nodo *addNode(struct node *head, int codice, int quantita) {
  23. //F1 creo il nuovo nodo
  24. struct nodo* nuovo = (struct nodo*)malloc(sizeof(struct nodo));
  25. nuovo->codice = codice;
  26. nuovo->quantita = quantita;
  27. nuovo->link = NULL;
  28. //F2 verico se non già esiste una testa
  29. if (head == NULL) {
  30. head = nuovo; //F3
  31. } else { //F4 se già esiste vado alla ricerca della coda
  32. struct nodo* current = head;
  33. while (current->link != NULL) {
  34. current = current->link;
  35. };
  36. //F5 accodo il mio nodo
  37. current->link = nuovo;
  38. }
  39. //F6 ritorno la testa
  40. return head;
  41. }
  42.  
  43. void display(struct nodo* head){
  44. if(head){
  45. printf("%d %d\n",head->codice,head->quantita);
  46. display(head->link);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement