0

This code is a solution to the given problem : https://www.hackerearth.com/international-women-hackathon-2016/algorithm/jp-and-rotations/

Issue is after I give m operations as R 1 ,L 2 ,L 1 one below other as specified in the sample input , it doesn't take any further input and directly prints output as 2 , I am unable to get the mistake I am making here ,please help me out .

#include <stdio.h>
#include<malloc.h>
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;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;

    }

    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;i<=n-1;i++)
    {

        temp=(struct node*)malloc(sizeof(struct node));
        scanf("%ld ",&temp->data);

        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;j<a;j++)
        {
            temp=ptr->next;
            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;j<a;j++)
        {
            temp->prev=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;
}
user94
  • 25
  • 3
  • `` is not a standard C header. – melpomene Jul 13 '16 at 05:16
  • 1
    Did you try `scanf(" %c%d",&rot,&a);`? Note that space is placed before `"%c"`. – Mohit Jain Jul 13 '16 at 05:17
  • This is probably a duplicate of every other question about input issues/`scanf`. – melpomene Jul 13 '16 at 05:18
  • 1
    Off topic: Avoid `scanf`. If you use it anyway, remember to check the return code. And don't cast the return value from `malloc`. And please indent your code correctly – Support Ukraine Jul 13 '16 at 05:20
  • [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Jul 13 '16 at 05:23
  • @MohitJain , can you please tell why did you mentioned space before %c in scanf , When I am already using fflush so what is the mistake by not using space before %c ? – user94 Jul 13 '16 at 05:59
  • @user94 - if you want help with this question you must provide information about **all** the input. That is m, n, data (in the two loops). Currently we can only guess that m is 3 but we have know ideas about which data values you input and how many. You should also state your expected output. Currently we can't tell why `if(head1->data==head2->data && rear1->data==rear2->data)` is true. It might not be a bug.... it depends on the data you input. – Support Ukraine Jul 13 '16 at 06:27
  • Hi @user94, I found a [related question](http://stackoverflow.com/questions/6582322/what-does-space-in-scanf-mean) for you. If you still don't understand, please shoot another comment, I would love to help you understand. `fflush(stdout);` is a `stdout` thing, `scanf` is `stdin` thing. – Mohit Jain Jul 13 '16 at 06:37
  • @MohitJain, I saw that fflush(stdin) has undefined behaviour , and even after changing to stdin , it is nt working , it works only when I give spaces before %c as u mentioned , now why is it so , I am not getting when I am flushing the buffer as well – user94 Jul 13 '16 at 08:27
  • @user94 Yes you never `fflush` the `stdin` stream because it doesn't make sense. `"%c"` can be used to read a character. This character is just after the last read of the `scanf`. Last scanf may have been stopped reading for multiple reasons for example end of input marked with new line or a space. Now when you read a character you may end up reading this space. One solution is to skip characters explicitly as long as they are space characters `do { scanf("%c", &rot); } while(rot != 'L' && rot != 'R');` or let `scanf` do it for you by adding an extra space. (Skip white spaces (`\t\n `) if any) – Mohit Jain Jul 13 '16 at 09:10

1 Answers1

-1

Try removing space after the format specifier in the scanf statement. It will not give you correct final output.But,I think thats what you needed to know.

Try this code:

#include <stdio.h>
#include<malloc.h>
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; i<m; i++)
    {
        scanf("%c %d", &rot, &a);
        fflush(stdout);
        fflush(stdin);
        if (rot == 'L')
        {
            ptr = head1;
            for (j = 0; j<a; j++)
            {
                temp = ptr->next;
                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; j<a; j++)
            {
                temp->prev = 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;
}
  • But I am giving space even during input of character and integer so I guess it should not be an issue . – user94 Jul 13 '16 at 12:26