Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int info;
- struct node* next;
- } ElementoDiLista;
- void addlast (ElementoDiLista** l, int n) {
- ElementoDiLista* new = malloc (sizeof (ElementoDiLista));
- new -> info = n;
- new -> next = NULL;
- if (*l == NULL)
- *l = new;
- else
- {
- ElementoDiLista* corr = *l;
- while (corr != NULL){
- corr = corr -> next;
- }
- corr -> next = new;
- }
- }
- void add (ElementoDiLista** l, int n) {
- ElementoDiLista* new = malloc (sizeof (ElementoDiLista));
- new -> info = n;
- new -> next = *l;
- *l = new;
- }
- void canc (ElementoDiLista** l, int n) {
- ElementoDiLista* prec = NULL;
- ElementoDiLista* corr = *l; //corr = *l;
- int trovato = 0;
- while (corr -> next != NULL && !trovato){
- if (corr -> info == n) trovato =1;
- else {
- prec = corr;
- corr = corr-> next;
- }
- }
- if (trovato)
- if (prec == NULL){
- *l = (*l)-> next;
- free (corr);
- }
- else {
- prec -> next = corr -> next;
- free (corr);
- }
- }
- void lettura (ElementoDiLista* l){
- ElementoDiLista* corr = l;
- while (corr != NULL){
- printf ("%d\n", corr -> info);
- corr = corr -> next;
- }
- }
- int main () {
- ElementoDiLista* list = NULL;
- int n;
- while (n != 0){
- scanf ("%d", &n);
- }
- if (n < 0)
- canc (&list,n);
- else if (n > 0 && n %2==0)
- add (&list, n);
- else if (n > 0 && n%2==1)
- addlast (&list, n);
- else if (n == 0)
- lettura (list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement