-
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
13 changed files
with
585 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
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,35 @@ | ||
/* | ||
* Scrivere un programma che acquisisca 5 numeri interi da tastiera e li inserisce in una lista. | ||
* Il programma chiede un ulteriore numero N, se questo è presente nella lista, lo elimina e aggiunge alla fine il suo quadrato | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "liste_int.h" | ||
|
||
#define NUM 5 | ||
|
||
int main (int argc, char *argv[]) { | ||
listi_t *head = NULL; | ||
int i, n; | ||
|
||
for (i = 0; i < NUM; i++) { | ||
scanf("%d", &n); | ||
head = append(head, n); | ||
} | ||
|
||
scanf("%d", &n); | ||
|
||
if (search(head, n)) { | ||
head = delete(head, n); | ||
head = append(head, n*n); | ||
} else { | ||
printf("%d not in list\n", n); | ||
} | ||
|
||
view(head); | ||
|
||
freeList(head); | ||
|
||
return 0; | ||
} |
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,113 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "liste_int.h" | ||
|
||
listi_t *push(listi_t *head, int val) { | ||
listi_t *newNode; | ||
|
||
/* Creo un nuovo nodo */ | ||
newNode = malloc(sizeof(listi_t)); | ||
|
||
if (newNode) { | ||
/* Lo metto prima della head */ | ||
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; | ||
|
||
/* Creo un nuovo nodo */ | ||
newNode = malloc(sizeof(listi_t)); | ||
|
||
if (newNode) { | ||
/* Gli assegno il valore */ | ||
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; | ||
|
||
/* Scorro tutta la lista e elimino ogni elemento */ | ||
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; | ||
|
||
/* Stampo ogni elemento della lista */ | ||
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,20 @@ | ||
#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); | ||
|
||
void view(listi_t *head); | ||
|
||
listi_t *freeList(listi_t *head); | ||
|
||
listi_t *search(listi_t *head, int val); | ||
|
||
listi_t *delete(listi_t *head, int val); | ||
|
||
#endif |
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,69 @@ | ||
void view(listc_t *head) { | ||
listc_t *p; | ||
|
||
for (p = head; p != NULL; p = p->next) { | ||
printf("%d -> ", p->num); | ||
} | ||
printf("|\n"); | ||
} | ||
|
||
listc_t *push(listc_t *head, char val) { | ||
listc_t *newNode; | ||
|
||
newNode = malloc(sizeof(listc_t)); | ||
|
||
if (newNode) { | ||
newNode->num = val; | ||
newNode->next = head; | ||
|
||
head = newNode; | ||
} else { | ||
printf("push: failed allocating memory\n"); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listc_t *append(listc_t *head, char val) { | ||
listc_t *newNode; | ||
listc_t *p; | ||
|
||
newNode = malloc(sizeof(listc_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; | ||
} | ||
|
||
listc_t *freeList(listc_t *head) { | ||
listc_t *tmp; | ||
|
||
while (head != NULL) { | ||
tmp = head; | ||
head = head->next; | ||
free(tmp); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listc_t *search(listc_t *head, char val) { | ||
return head; | ||
} | ||
|
||
listc_t *delete(listc_t *head, char val) { | ||
return head; | ||
} |
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,23 @@ | ||
#ifndef LISTE_CHAR_H | ||
#define LISTE_CHAR_H | ||
|
||
typedef struct listc_s { | ||
char num; | ||
struct listc_s *next; | ||
} listc_t; | ||
|
||
/* Anche la append può modificare l'inizio della lista (quando la lista è vuota), quindi restituisce la head */ | ||
listc_t *append(listc_t *head, char newval); | ||
listc_t *push(listc_t *head, char newval); | ||
|
||
/* Stampa tutti i valori della lista */ | ||
void view(listc_t *head); | ||
|
||
/* Libera tutta la lista */ | ||
listc_t *freeList(listc_t *head); | ||
|
||
listc_t *search(listc_t *head, char val); | ||
|
||
listc_t *delete(listc_t *head, char val); | ||
|
||
|
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,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "liste_float.h" | ||
|
||
void view(listf_t *head) { | ||
listf_t *p; | ||
|
||
for (p = head; p != NULL; p = p->next) { | ||
printf("%d -> ", p->num); | ||
} | ||
printf("|\n"); | ||
} | ||
|
||
listf_t *push(listf_t *head, float val) { | ||
listf_t *newNode; | ||
|
||
newNode = malloc(sizeof(listf_t)); | ||
|
||
if (newNode) { | ||
newNode->num = val; | ||
newNode->next = head; | ||
|
||
head = newNode; | ||
} else { | ||
printf("push: failed allocating memory\n"); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listf_t *append(listf_t *head, float val) { | ||
listf_t *newNode; | ||
listf_t *p; | ||
|
||
newNode = malloc(sizeof(listf_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; | ||
} | ||
|
||
listf_t *freeList(listf_t *head) { | ||
listf_t *tmp; | ||
|
||
while (head != NULL) { | ||
tmp = head; | ||
head = head->next; | ||
free(tmp); | ||
} | ||
|
||
return head; | ||
} | ||
|
||
listf_t *search(listf_t *head, float val) { | ||
return head; | ||
} | ||
|
||
listf_t *delete(listf_t *head, float val) { | ||
return head; | ||
} |
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,20 @@ | ||
#ifndef LISTE_FLOAT_H | ||
#define LISTE_FLOAT_H | ||
|
||
typedef struct listf_s { | ||
float num; | ||
struct listf_s *next; | ||
} listf_t; | ||
|
||
listf_t *append(listf_t *head, float newval); | ||
listf_t *push(listf_t *head, float newval); | ||
|
||
void view(listf_t *head); | ||
|
||
listf_t *freeList(listf_t *head); | ||
|
||
listf_t *search(listf_t *head, float val); | ||
|
||
listf_t *delete(listf_t *head, float val); | ||
|
||
#endif |
Oops, something went wrong.