CrazyEngineers
  • radha
    radha

    MemberJul 10, 2016

    what is error in below code ?

    #include 
    #include
    struct node
    {
    int data ;
    struct node *next,*prev;
    };
    int main()
    {
        int n ,m,a,count=0,i,j;
        char rot;
        scanf("%d",&n);
        fflush(stdin);
        scanf("%d",&m);
        fflush(stdin);
        printf("\n");
        struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2;
        if(head1==NULL)
        {
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d ",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("%d ",temp->data);
            ptr->next=temp;
            temp->prev=ptr;
            ptr=temp;
           
        }
        rear1=ptr;
        temp=NULL;
        ptr=NULL;
        printf("\n");
        if(head2==NULL)
        {
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d ",temp->data);
            temp->next=NULL;
            head2=temp;
            ptr=head2;
        }
        for(i=1;i<=n-1;i++)
        {
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d ",temp->data);
            ptr->next=temp;
            ptr=temp;
            ptr->next=NULL;
           
        }
        rear2=ptr;
        ptr=NULL;temp=NULL;
        printf("\n");
        for(i=0;inext;
                rear1->next=ptr;
                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;
            printf("\n");
            if(head1->data==head2->data && rear1->data==rear2->data)
            {
                break;
           
            }
           
        }
        }
        printf("%d",count);
       
        return 0;
    }
    This is a snippet of code which accepts two arrays initial and target and 'm' operations in the form of R X and L X where L X is for left rotation by 1 and R X is for right rotation by 1 ,we have to output the no of operations after which initial and target array both are same , I created queues where head1 and head2 are pointers to front nodes of initial and target arrays , Now the issue is that after I scan the first operation , it stops working ,please tell the point where I am making mistake .


    }
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • aishwarya mk

    MemberJul 12, 2016

    in scanf function u mis the "&" symbol u check once
    in the line scanf("%d",&header1->data);
    because data is integer data type
    Are you sure? This action cannot be undone.
    Cancel
  • brahmaasthra

    MemberJul 12, 2016

    #include <stdio.h>
    #include<malloc.h>
    struct node
    {
    int data ;
    struct node *next,*prev;
    };
    int main()
    {
    int n ,m,a,count=0,i,j;
    char rot;
    scanf("%d",&n);
    fflush(stdin);
    scanf("%d",&m);
    fflush(stdin);
    printf("\n");
    struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2;
    if(head1==NULL)
    {
    int inp;
    temp=(struct node*)malloc(sizeof(struct node));
    scanf("%d ",&inp);
    temp->data=inp;
    temp->next=NULL;
    head1=temp;
    ptr=head1;
    head1->prev=NULL;
    }
    for(i=1;i<=n-1;i++)
    {
    int inp;
    temp=(struct node*)malloc(sizeof(struct node));
    scanf("%d ",&inp);
    temp->data=inp;
    ptr->next=temp;
    temp->prev=ptr;
    ptr=temp;

    }
    rear1=ptr;
    temp=NULL;
    ptr=NULL;
    printf("\n");
    if(head2==NULL)
    {
    int inp;
    temp=(struct node*)malloc(sizeof(struct node));
    scanf("%d ",&inp);
    temp->data=inp;
    temp->next=NULL;
    head2=temp;
    ptr=head2;
    }
    for(i=1;i<=n-1;i++)
    {
    int inp;
    temp=(struct node*)malloc(sizeof(struct node));
    scanf("%d ",&inp);
    temp->data=inp;
    ptr->next=temp;
    ptr=temp;
    ptr->next=NULL;

    }
    rear2=ptr;
    ptr=NULL;temp=NULL;
    printf("\n");
    for(i=0;i<m;i++)
    {
    scanf("%c %d",&rot,&a);
    fflush(stdin);

    if(rot=='L')
    {
    ptr=head1;
    for(j=0;j<a;j++)
    {
    temp=ptr->next;
    rear1->next=ptr;
    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;
    printf("\n");
    if(head1->data==head2->data && rear1->data==rear2->data)
    {
    break;

    }

    }
    }
    printf("%d",count);

    return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • brahmaasthra

    MemberJul 12, 2016

    copy and run this program and tell. Whether it works well?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberJul 15, 2016

    #-Link-Snipped-# No one asked you the entire code
    Are you sure? This action cannot be undone.
    Cancel
  • aishwarya mk

    MemberAug 4, 2016

    scanf("%d ",temp->data);
    for structure also u should specifies te data qualifiers
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register