0

Let's suppose I write a program that simulates the Dominoes game, so I'd like to define a struct the following way:

typedef struct nodo { 
    int casella1;
    int casella2;
    struct nodo *next;
} Tessera;
typedef Tessera *Lista;

And then after some input in casual order that ends when a number outside the range 0 <= x <= 6 is entered, I'd like to delete the possible duplicates that don't respect the domino rules. The number casella2 should always be succeeded by the same number in ->next->casella1, using a recursive function, that goes like this:

void legale(Lista *head) {

    Lista aux,aus = *head;

    if(aus->next == NULL) {
      return;
    }

    else if(aus->casella2 == aus->next->casella1)   {
      legale(&(aus->next));
    }
    else {
      aux = aus->next;
      aus->next = aus->next->next;  
      free(aux);
    }
}

But for example the sequence " 1 2, 2 3, 3 4, 4 5, 5 4, 6 2, 7" gives "1 2, 2 3, 3 4, 4 5,6 2 " So it doesn't delete 6,2 as it should.

I think the way I deleted the pointer is correct, so why is the function wrong?

The code goes like this :

#include<stdio.h>
#include<stdlib.h>
typedef struct nodo { 
    int casella1;
    int casella2;
    struct nodo *next;
    }Tessera;
typedef Tessera *Lista;

void stampa(Lista *head) {

    Lista ausil = *head;

    while(ausil != NULL) {
    printf("%d\t%d\n",ausil->casella1,ausil->casella2);
    ausil = ausil->next;
    }
}

void legale(Lista *head) {

    Lista aux,aus = *head;

    if(aus->next == NULL) {
    return;
}

    else if(aus->casella2 == aus->next->casella1)   {
    legale(&(aus->next));
}
    else {
    aux = aus->next;
    aus->next = aus->next->next;    
    free(aux);
}


}

void write (Lista *head) {
    Lista corr,aux,aus;
    int x,y;
    scanf("%d%d",&x,&y);
    aus = *head;

    while((x >= 0 && x <= 6) && (y >= 0 && y <= 6)) {

    if(aus == NULL) {

    aus = malloc(sizeof(Tessera));
    aus->casella1 = x;  
    aus->casella2 = y;
    aus->next = NULL;
    *head = aus;
}
    else {
    aux = *head;

    corr = malloc(sizeof(Tessera));
    corr->casella1 = x;
    corr->casella2 = y;
    corr->next = aux;
    *head = corr;
}
    scanf("%d%d",&x,&y);
    }

}

int main() {
    Lista Lista1 = NULL;
    write(&Lista1);
    legale(&Lista1);
    stampa(&Lista1);
return 0;
}

1 Answer 1

1

You are at least missing a recursive call after you remove a duplicate,

else {
  aux = aus->next;
  aus->next = aus->next->next;  
  free(aux);
}

If you don't recurse you stop after the first removal.

Also by precaution, before checking whether aus->next == NULL you should check whether aus == NULL so it doesn't break if you pass it an empty list.


EDIT

You are constructing your linked list backwards when you read it.

You insert each pair at the head, so in the end you have your sequence backwards. It's always a good idea to print out your list after you read it to make sure it's OK ;)

Sign up to request clarification or add additional context in comments.

3 Comments

I agree,but it should at least delete the last one and so, if just one is wrong the sequence should be right
Indeed. I don't get your example though, you wrote: "1 2, 2 3, 3 4, 4 5, 5 4, 6 2, 7" gives "1 2, 2 3, 3 4, 4 5,6 2". Here it did remove 5 4 (which it shouldn't have). Also what is that 7 by itself? It would be best if you provided a minimal compilable, testable example
It should't have removed 5 4, it should have removed 6 2, 7 is there just to stop the sequence as wrote "that ends when a number outside the range 0 <= x <= 6 is entered", could be omitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.