¿Por qué el siguiente código no escanea todas las entradas correctamente?

Este código es una solución al problema dado: https://www.hackerearth.com/international-women-hackathon-2016/algorithm/jp-and-rotations/

El problema es después de que doy m operaciones como R 1, L 2, L 1 una debajo de otra como se especifica en la entrada de muestra, no toma ninguna entrada adicional e imprime directamente la salida como 2, no puedo obtener el error que soy haciendo aquí, por favor ayudame

#include  #include struct node { unsigned long int data ; struct node *next,*prev; }; int main() { int n ,m,a,count=0,i,j; char rot; scanf("%d %d ", &n ,&m); struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2; if(head1==NULL) { temp=(struct node*)malloc(sizeof(struct node)); scanf("%ld ",&temp->data); temp->next=NULL; head1=temp; ptr=head1; head1->prev=NULL; } for(i=1;idata); ptr->next=temp; temp->prev=ptr; ptr=temp; temp->next=NULL; } rear1=ptr; temp=NULL; ptr=NULL; fflush(stdout); if(head2==NULL) { temp=(struct node*)malloc(sizeof(struct node)); scanf("%ld ",&temp->data); temp->next=NULL; head2=temp; ptr=head2; } for(i=1;idata); ptr->next=temp; ptr=temp; ptr->next=NULL; } rear2=ptr; ptr=NULL;temp=NULL; fflush(stdout); for(i=0;i<m;i++) { scanf("%c %d",&rot,&a); fflush(stdout); if(rot=='L') { ptr=head1; for(j=0;jnext; rear1->next=ptr; ptr->prev=rear1; rear1=ptr; head1=temp; ptr=head1; } count++; ptr=NULL; temp=NULL; if(head1->data==head2->data && rear1->data==rear2->data) { break; } } else if(rot=='R') { temp=head1; ptr=rear1->prev; for(j=0;jprev=rear1; rear1->prev->next=NULL; rear1->next=temp; head1=rear1; temp=head1; rear1=ptr; ptr=rear1->prev; } count++; temp=NULL; ptr=NULL; if(head1->data==head2->data && rear1->data==rear2->data) { break; } } } printf("%d",count); return 0; } 

Intente eliminar el espacio después del especificador de formato en la instrucción scanf. No le dará el resultado final correcto. Pero, creo que eso es lo que necesita saber.

Prueba este código:

 #include  #include struct node { unsigned long int data; struct node *next, *prev; }; int main() { int n, m, a, count = 0, i, j; char rot; scanf("%d %d", &n, &m); struct node *head1 = NULL, *rear1 = NULL, *ptr = NULL, *temp = NULL, *head2 = NULL, *rear2 = NULL; if (head1 == NULL) { temp = (struct node*)malloc(sizeof(struct node)); scanf("%ld", &temp->data); temp->next = NULL; head1 = temp; ptr = head1; head1->prev = NULL; } for (i = 1; i <= n - 1; i++) { temp = (struct node*)malloc(sizeof(struct node)); scanf("%ld", &temp->data); ptr->next = temp; temp->prev = ptr; ptr = temp; temp->next = NULL; } ptr->next = head1; head1->prev = ptr; rear1 = ptr; temp = NULL; ptr = NULL; fflush(stdout); fflush(stdin); if (head2 == NULL) { temp = (struct node*)malloc(sizeof(struct node)); scanf("%ld", &temp->data); temp->next = NULL; head2 = temp; ptr = head2; head2->prev = NULL; } for (i = 1; i <= n - 1; i++) { temp = (struct node*)malloc(sizeof(struct node)); scanf("%ld", &temp->data); ptr->next = temp; temp->prev = ptr; ptr = temp; ptr->next = NULL; } ptr->next = head2; head2->prev = ptr; rear2 = ptr; ptr = NULL; temp = NULL; fflush(stdout); fflush(stdin); for (i = 0; inext; rear1->next = ptr; ptr->prev = rear1; rear1 = ptr; head1 = temp; ptr = head1; } count++; ptr = NULL; temp = NULL; if (head1->data == head2->data && rear1->data == rear2->data) { break; } } else if (rot == 'R') { temp = head1; ptr = rear1->prev; for (j = 0; jprev = rear1; rear1->prev->next = NULL; rear1->next = temp; head1 = rear1; temp = head1; rear1 = ptr; ptr = rear1->prev; } count++; temp = NULL; ptr = NULL; if (head1->data == head2->data && rear1->data == rear2->data) { break; } } } printf("%d", count); return 0; }