Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active January 9, 2018 10:26
Show Gist options
  • Select an option

  • Save aziis98/f1c15c2b2e75747066e92a8bb6486cae to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/f1c15c2b2e75747066e92a8bb6486cae to your computer and use it in GitHub Desktop.

Esercizi della Lezione 11

  • Stampa Numero Elementi Maggiori di \ldots
  • Stampa primo pari e primo dispari
  • Cancella i primi N elementi
  • Inserisci dopo quarto
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void print_lista(Elemento* elemento) {
if (elemento != NULL) {
printf("%d -> ", elemento->value);
print_lista(elemento->next);
}
else {
printf("NULL");
}
}
int remove_firsts(Lista* lista, int n) {
if (*lista != NULL && n > 0) {
Elemento *rem = *lista;
*lista = (*lista)->next;
free(rem);
remove_firsts(lista, n - 1);
}
}
void add_last(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = NULL;
if (*lista == NULL) {
*lista = el;
}
else {
Lista last = *lista;
while (last->next != NULL) {
last = last->next;
}
last->next = el;
}
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
add_last(&l, value);
scanf("%d", &value);
}
int n;
scanf("%d", &n);
remove_firsts(&l, n);
print_lista(l);
}
#include <stdio.h>
#include <stdlib.h>
// Cose cattive
#define loop while(1)
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void add_last(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = NULL;
if (*lista == NULL) {
*lista = el;
}
else {
Lista last = *lista;
while (last->next != NULL) {
last = last->next;
}
last->next = el;
}
}
int sum_lista(Elemento *el) {
if (el == NULL)
return 0;
else {
return el->value + sum_lista(el->next);
}
}
int count_greater(Elemento *el, float minvalue) {
if (el == NULL)
return 0;
else {
return (el->value > minvalue ? 1 : 0) + count_greater(el->next, minvalue);
}
}
int main() {
Lista lista = NULL;
int count = 0;
loop {
int value;
scanf("%d", &value);
if (value < 0) break;
count++;
add_last(&lista, value);
}
int sum = sum_lista(lista);
printf("%d\n", sum);
float minimo = (float) sum / 4;
printf("%d\n", count_greater(lista, minimo));
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
Elemento *create_elemento(int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = NULL;
return el;
}
void insert_after(Elemento **lista, int value, int after) {
if (*lista == NULL) {
*lista = create_elemento(value);
}
else if (after == -1) {
Elemento *el = *lista;
while (el->next != NULL) {
el = el->next;
}
el->next = create_elemento(value);
}
else {
Elemento *el = *lista;
while (el->next != NULL && --after > 0) {
el = el->next;
}
Elemento *newel = create_elemento(value);
if (el->next != NULL) {
newel->next = el->next;
}
el->next = newel;
}
}
void print_lista(Elemento *lista) {
if (lista == NULL) {
printf("NULL\n");
}
else {
printf("%d -> ", lista->value);
print_lista(lista->next);
}
}
int main() {
Elemento *lista = NULL;
while (1) {
int n;
scanf("%d", &n);
if (n < 0) break;
insert_after(&lista, n, -1);
}
int newvalue;
scanf("%d", &newvalue);
insert_after(&lista, newvalue, 4);
print_lista(lista);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Cose cattive
#define loop while(1)
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void add_last(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = NULL;
if (*lista == NULL) {
*lista = el;
}
else {
Lista last = *lista;
while (last->next != NULL) {
last = last->next;
}
last->next = el;
}
}
void find_even_odd(Elemento *el, int index, int *even_index, int *odd_index) {
if (el != NULL) {
if (*even_index == -1 && el->value % 2 == 0) {
*even_index = el->value;
}
if (*odd_index == -1 && el->value % 2 == 1) {
*odd_index = el->value;
}
if (*even_index == -1 || *odd_index == -1)
find_even_odd(el->next, index + 1, even_index, odd_index);
}
}
int main() {
Lista lista = NULL;
int count = 0;
loop {
int value;
scanf("%d", &value);
if (value < 0) break;
count++;
add_last(&lista, value);
}
int even = -1;
int odd = -1;
find_even_odd(lista, 0, &even, &odd);
printf("%d\n%d\n", odd, even);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment