-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
726 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "liste_int.h" | ||
|
||
listi_t *push(listi_t *head, int val) { | ||
listi_t *newNode; | ||
|
||
newNode = malloc(sizeof(listi_t)); | ||
|
||
if (newNode) { | ||
newNode->num = val; | ||
newNode->next = head; | ||
|
||
head = newNode; | ||
} else { | ||
printf("push: failed allocating memory\n"); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listi_t *append(listi_t *head, int val) { | ||
listi_t *newNode; | ||
listi_t *p; | ||
|
||
newNode = malloc(sizeof(listi_t)); | ||
|
||
if (newNode) { | ||
newNode->num = val; | ||
newNode->next = NULL; | ||
|
||
if (head == NULL) { // Se la lista è vuota | ||
head = newNode; | ||
} else { | ||
// Arrivo in fondo alla lista (*p ultimo elemento) | ||
for (p = head; p->next != NULL; p = p->next); | ||
p->next = newNode; | ||
} | ||
} else { | ||
printf("append: failed allocating memory\n"); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listi_t *freeList(listi_t *head) { | ||
listi_t *tmp; | ||
|
||
while (head != NULL) { | ||
tmp = head; | ||
head = head->next; | ||
free(tmp); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listi_t *search(listi_t *head, int val) { | ||
listi_t *p; | ||
|
||
/* Scorro tutta la lista */ | ||
for (p = head; p != NULL; p = p->next) { | ||
/* Se trovo l'elemento cercato ritorno il suo indirizzo */ | ||
if (p->num == val) { | ||
return p; | ||
} | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
listi_t *delete(listi_t *head, int val) { | ||
listi_t *p, *del; | ||
|
||
/* Se l'elemento cercato è il primo */ | ||
if (head->num == val) { | ||
del = head; | ||
head = head->next; | ||
free(del); | ||
|
||
return head; | ||
} else { | ||
for (p = head; p->next != NULL; p = p->next) { | ||
/* Mi fermo all'elemento precedente a quello con il valore richiesto */ | ||
if (p->next->num == val) { | ||
/* Cambio il puntatore dell'elemento precedente a quello successivo a quello cercato */ | ||
del = p->next; | ||
p->next = p->next->next; | ||
/* Elimino l'elemento cercato */ | ||
free(del); | ||
|
||
return head; | ||
} | ||
} | ||
} | ||
|
||
return head; | ||
} | ||
|
||
void view(listi_t *head) { | ||
listi_t *p; | ||
|
||
for (p = head; p != NULL; p = p->next) { | ||
printf("%d -> ", p->num); | ||
} | ||
printf("|\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef LISTE_INT_H | ||
#define LISTE_INT_H | ||
|
||
typedef struct listint_s { | ||
int num; | ||
struct listint_s *next; | ||
} listi_t; | ||
|
||
listi_t *append(listi_t *head, int newval); | ||
listi_t *push(listi_t *head, int newval); | ||
|
||
/* Libera tutta la lista */ | ||
listi_t *freeList(listi_t *head); | ||
|
||
listi_t *search(listi_t *head, int val); | ||
|
||
listi_t *delete(listi_t *head, int val); | ||
|
||
/* Stampa tutti i valori della lista */ | ||
void view(listf_t *head); | ||
|
||
#endif |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.