Skip to content
Snippets Groups Projects
Commit 348bf6ef authored by 20041679's avatar 20041679
Browse files

added Esercizi n. 2

parent fe511cc4
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
typedef int Data;
typedef struct Node {
Data data;
struct Node *next;
} Node;
typedef Node *List;
List _node_alloc(void) {
return malloc(sizeof(Node));
}
List list_new(Data data[], size_t n) {
List new;
if (n == 0) {
new = NULL;
} else {
new = _node_alloc();
new->data = data[0];
new->next = list_new(&data[1], n - 1);
}
return new;
}
List list_input(void) {
// stop at -1?
int data;
puts("inserire dato");
scanf("%d", &data);
List new;
if (data == -1) {
new = NULL;
} else {
new = list_new(&data, 1);
new->data = data;
new->next = list_input();
}
return new;
}
void list_print(List list) {
while (list) {
printf("%d ", list->data);
list = list->next;
}
puts("");
}
int main(void) {
list_print(list_input());
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char first_name[12];
char last_name[10];
int age;
} Person;
typedef Person Data;
typedef struct Node {
Data data;
struct Node *next;
} Node;
typedef Node *List;
List _node_alloc(void) {
return malloc(sizeof(Node));
}
List list_new(Data data[], size_t n) {
List new;
if (n == 0) {
new = NULL;
} else {
new = _node_alloc();
new->data = data[0];
new->next = list_new(&data[1], n - 1);
}
return new;
}
List list_input(void) {
Person data;
puts("nome");
// FIXME: buffer overflow
scanf("%s", data.first_name);
if (strcmp(data.first_name, "stop") == 0) {
return NULL;
}
puts("cognome");
// FIXME: buffer overflow
scanf("%s", data.last_name);
puts("anni");
scanf("%d", &data.age);
List new;
new = list_new(&data, 1);
new->data = data;
new->next = list_input();
return new;
}
void list_print(List list, size_t n) {
while (list && n > 0) {
printf(
"nome: %s, cognome: %s, anni: %d\n",
list->data.first_name,
list->data.last_name,
list->data.age
);
list = list->next;
--n;
}
}
int main(void) {
list_print(list_input(), 5);
}
nome0 cognome0 0
nome1 cognome1 1
nome2 cognome2 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char first_name[12];
char last_name[10];
int age;
} Person;
typedef Person Data;
typedef struct Node {
Data data;
struct Node *next;
} Node;
typedef Node *List;
List _node_alloc(void) {
return malloc(sizeof(Node));
}
List list_new(Data data[], size_t n) {
List new;
if (n == 0) {
new = NULL;
} else {
new = _node_alloc();
new->data = data[0];
new->next = list_new(&data[1], n - 1);
}
return new;
}
List list_input(FILE *f) {
Person data;
// FIXME: buffer overflow
int ret = fscanf(f, "%s %s %d", data.first_name, data.last_name, &data.age);
if (ret < 3 || ret == EOF) {
return NULL;
}
List new = list_new(&data, 1);
new->data = data;
new->next = list_input(f);
return new;
}
void list_print(List list, size_t n) {
while (list && n > 0) {
printf(
"nome: %s, cognome: %s, anni: %d\n",
list->data.first_name,
list->data.last_name,
list->data.age
);
list = list->next;
--n;
}
}
int main(void) {
FILE *f = fopen("input.txt", "r");
if (!f) { return 1; }
list_print(list_input(f), 5);
fclose(f);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment