Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 5, 2017 08:56
Show Gist options
  • Select an option

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

Select an option

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

Esercizi dell'esercitazione del 5 Dicembre 2017

#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_multiples(Lista* lista, int n) {
Elemento *el = *lista;
if (el != NULL) {
if (el->value % n == 0) {
*lista = el->next;
free(el);
remove_multiples(lista, n);
}
else {
remove_multiples(&(el->next), n);
}
}
}
void push(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = *lista;
*lista = el;
}
int main() {
int n;
scanf("%d", &n);
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push(&l, value);
scanf("%d", &value);
}
remove_multiples(&l, n);
print_lista(l);
}
#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");
}
}
void push(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
if (*lista == NULL) {
el->value = value;
el->next = NULL;
*lista = el;
}
else {
el->value = (*lista)->value;
el->next = (*lista)->next;
(*lista)->value = value;
(*lista)->next = el;
}
}
int insert_minusone(Lista* lista) {
Elemento *el = *lista;
if (el != NULL) {
if (el->value % 2 == 0) {
push(lista, -1);
insert_minusone(&((*lista)->next->next));
}
else {
insert_minusone(&((*lista)->next));
}
}
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push(&l, value);
scanf("%d", &value);
}
insert_minusone(&l);
print_lista(l);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void push_back(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
if (*lista == NULL) {
*lista = el;
}
else {
Elemento *last = *lista;
while (last->next != NULL) {
last = last->next;
}
last->next = el;
}
}
int check_lista(Elemento *elemento) {
if (elemento != NULL && elemento->next != NULL && elemento->value < elemento->next->value) {
return check_lista(elemento->next);
}
else {
return elemento->next == NULL;
}
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push_back(&l, value);
scanf("%d", &value);
}
if (check_lista(l)) {
printf("True");
}
else {
printf("False");
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
int RecLength(Elemento* elemento) {
if (elemento != NULL) {
return 1 + RecLength(elemento->next);
}
else {
return 0;
}
}
void push(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = *lista;
*lista = el;
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push(&l, value);
scanf("%d", &value);
}
printf("%d\n", RecLength(l));
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void print_lista(Elemento* elemento, int i) {
if (elemento != NULL) {
print_lista(elemento->next, i + 1);
printf("%d -> ", elemento->value);
}
if (i == 0) {
printf("NULL");
}
}
void push(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
if (*lista == NULL) {
el->value = value;
el->next = NULL;
*lista = el;
}
else {
el->value = (*lista)->value;
el->next = (*lista)->next;
(*lista)->value = value;
(*lista)->next = el;
}
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push(&l, value);
scanf("%d", &value);
}
print_lista(l, 0);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int value;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void RecStampa(Elemento* elemento) {
if (elemento != NULL) {
printf("%d -> ", elemento->value);
RecStampa(elemento->next);
}
else {
printf("NULL");
}
}
void push(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->value = value;
el->next = *lista;
*lista = el;
}
int main() {
Lista l = NULL;
int value;
scanf("%d", &value);
while (value >= 0) {
push(&l, value);
scanf("%d", &value);
}
RecStampa(l);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment