Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 2, 2017 20:32
Show Gist options
  • Select an option

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

Select an option

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

Esercizi della piattaforma del 28 Novembre 2017

#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int info;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void print_lista(Lista *lista) {
Elemento *el = *lista;
while (el != NULL) {
printf("%d\n", el->info);
el = el->next;
}
}
/*
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
*/
void push_ordered(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
if (*lista == NULL) {
*lista = el;
}
else if ((*lista)->info < value) {
el->next = *lista;
*lista = el;
}
else {
Elemento *prev = *lista;
while (prev->next != NULL && prev->next->info > value) {
prev = prev->next;
}
el->next = prev->next;
prev->next = el;
}
}
void add(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
el->next = *lista;
*lista = el;
}
void intersection_list(Elemento *a, Elemento *b, Lista *out) {
if (a != NULL && b != NULL) {
if (a->info == b->info && (*out == NULL || (*out)->info != a->info)) {
add(out, a->info);
intersection_list(a->next, b->next, out);
}
else if (a->info > b->info) {
intersection_list(a->next, b, out);
}
else {
intersection_list(a, b->next, out);
}
}
}
int main() {
Lista lista1 = NULL;
Lista lista2 = NULL;
Lista lista3 = NULL;
int value;
do {
scanf("%d", &value);
if (value >= 0) push_ordered(&lista1, value);
} while (value >= 0);
do {
scanf("%d", &value);
if (value >= 0) push_ordered(&lista2, value);
} while (value >= 0);
intersection_list(lista1, lista2, &lista3);
print_lista(&lista3);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int info;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void print_lista(Lista *lista) {
Elemento *el = *lista;
while (el != NULL) {
printf("%d\n", el->info);
el = el->next;
}
}
/*
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
*/
void push_ordered(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
if (*lista == NULL) {
*lista = el;
}
else if ((*lista)->info > value) {
el->next = *lista;
*lista = el;
}
else {
Elemento *prev = *lista;
while (prev->next != NULL && prev->next->info < value) {
prev = prev->next;
}
el->next = prev->next;
prev->next = el;
}
}
int main() {
Lista l = NULL;
int value;
do {
scanf("%d", &value);
if (value < 0) {
print_lista(&l);
}
else {
push_ordered(&l, value);
}
} while (value >= 0);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct elemento {
int info;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void push(Lista *lista, int info) {
Elemento *el = malloc(sizeof(Elemento));
el->info = info;
el->next = *lista;
*lista = el;
}
void pop(Lista *lista) {
if (*lista != NULL) {
Elemento *el = *lista;
*lista = (*lista)->next;
free(el);
}
}
int main() {
int value;
Lista lista = NULL;
do {
scanf("%d", &value);
if (value > 0) {
push(&lista, value);
}
else if (value == 0) {
pop(&lista);
}
else {
for (Lista el = lista; el != NULL; el = el->next) {
printf("%d\n", el->info);
}
}
} while(value >= 0);
while (lista != NULL) {
pop(&lista);
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct elemento {
int info;
struct elemento *next;
} Elemento;
typedef Elemento* Lista;
void print_lista(Lista *lista) {
for (Elemento *el = *lista; el != NULL; el = el->next) {
printf("%d\n", el->info);
}
}
void push_front(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
el->next = *lista;
*lista = el;
}
void push_back(Lista *lista, int value) {
Elemento *el = malloc(sizeof(Elemento));
el->info = value;
el->next = NULL;
if (*lista == NULL) {
*lista = el;
}
else {
Elemento *last = *lista;
while (last->next != NULL) {
last = last->next;
}
last->next = el;
}
}
void remove_first(Lista *lista, int value) {
if (*lista != NULL) {
Elemento *el = *lista;
if (el->info == value) {
*lista = el->next;
free(el);
}
else {
while (el != NULL && el->next != NULL && el->next->info != value) {
el = el->next;
}
if (el != NULL && el->next != NULL) {
Elemento *newnext = el->next->next;
free(el->next);
el->next = newnext;
}
}
}
}
int main() {
Lista l = NULL;
int value;
do {
scanf("%d", &value);
if (value < 0) {
remove_first(&l, -value);
}
else if (value > 0) {
if (value % 2 == 0) {
push_front(&l, value);
}
else {
push_back(&l, value);
}
}
else if (value == 0) {
print_lista(&l);
}
} while (value != 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment