Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct nodo{
- int codice;
- int quantita;
- struct nodo* link;
- };
- struct nodo *head=NULL;
- struct nodo *nuovaLista=NULL;
- /* FASE 0: impostare head come var globale = NULL
- FASE 1: creazione del mio nodo
- FASE 2: verifica esistenza head
- FASE 3: in assenza di head-> head := nuovo
- FASE 4: altrimenti mi scorro la lista
- fino a trovare l'ultimo elemento
- FASE 5: ultimoElemento->link := nuovo
- FASE 6: ritorno head
- FASE 7: nella call: head=addNodo(head,...);
- */
- struct nodo *addNode(struct node *head, int codice, int quantita) {
- //F1 creo il nuovo nodo
- struct nodo* nuovo = (struct nodo*)malloc(sizeof(struct nodo));
- nuovo->codice = codice;
- nuovo->quantita = quantita;
- nuovo->link = NULL;
- //F2 verico se non già esiste una testa
- if (head == NULL) {
- head = nuovo; //F3
- } else { //F4 se già esiste vado alla ricerca della coda
- struct nodo* current = head;
- while (current->link != NULL) {
- current = current->link;
- };
- //F5 accodo il mio nodo
- current->link = nuovo;
- }
- //F6 ritorno la testa
- return head;
- }
- void display(struct nodo* head){
- if(head){
- printf("%d %d\n",head->codice,head->quantita);
- display(head->link);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement