ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1019. Line Painting

Why my program get "access violation"
Posted by NOL 4 Oct 2002 12:09
I think my program is correct but why it get crash access violation
This is my program. Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

struct Node{long A;Node *next;Node *prev;};

int main()
{
    int n,i,W,nW;
    long x,y,max=0,maxa,maxb;
    char s[10];
    Node *home,*now,*T;
    home=(Node *)malloc(sizeof(Node));
    home->next=(Node *)malloc(sizeof(Node));
    home->prev=NULL;
    home->A=0;
    home->next->A=1000000000;
    home->next->next=NULL;
    home->next->prev=home;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%ld%ld%s",&x,&y,&s);
        if(x==y) continue;
        nW=(s[0]=='w');
        for(now=home,W=0;now!
=NULL;now=now->next,W=!W)
            if(now->A>=x)
            {
                if
(nW==W)
                {

    if(y<=now->A) break;

    T=now->prev;
                }
                else
                {

    if(now->A>y)

    {

        T=now->prev;

        now->prev=(Node *)malloc(sizeof
(Node));

        T->next=(Node *)malloc(sizeof(Node));

        now->prev->next=now;

        T->next->prev=T;

        now->prev->prev=T->next;

        T->next->next=now->prev;

        now->prev->A=y;

        T->next->A=x;

        break;

    }

    now->A=x;

    T=now;
                }
                for
(now=now->next,W=!W;now!=NULL;now=now->next,W=!W)
                {

    if(now->A>y)

    {

        if(W==nW)

        {

            now->prev=T;

            T->next=now;

        }

        else

        {

            now->prev->A=y;

            now->prev->prev=T;

            T->next=now->prev;

        }

        break;

    }

    else

        if(now->A==y)

        {

            if(W!=nW) now=now-
>next;

            now->prev=T;

            T->next=now;

            break;

        }
                }
                break;
            }
    }
    for(now=home->next,W=1;now!=NULL;now=now-
>next,W=!W)
        if(W && (((now->A)-(now->prev->A))
>max))
        {
            max=(now->A)-(now-
>prev->A);
            maxa=now->prev->A;
            maxb=now->A;
        }
    printf("%ld %ld\n",maxa,maxb);
    return 0;
}