I am trying to solve this spoj problem.
Here is my solution to the problem in C:
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node*next;
};
struct node* head=NULL;
void insertFront(int data)
{
    struct node*newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    newnode->next=head;
    head=newnode;
}
int returnSize(){
    int cnt=0;
    struct node*temp=head;
    while(temp!=NULL)
    {
        cnt++;
        temp=temp->next;
    }
    return cnt;
}
void insertAt(int position,int data)
{
    struct node*newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    if(head==NULL&&position>0)//marker1
    {
        head=newnode;
        return;
    }//marker2
    int cnt=returnSize();
    if(position>cnt)
    {
        struct node*var=head;
        while(var->next!=NULL)
        var=var->next;
        var->next=newnode;
        return;
    }
    int i;
    struct node*temp=head;
    if(position==0)
    {
    newnode->next=head;
    head=newnode;
    return;
    }
    else
    {
        for(i=0;i<position-1;i++)
        {
            temp=temp->next;
        }
        newnode->next=temp->next;
        temp->next=newnode;
    }   
}
void deleteAt(int position)
{
    if(head==NULL)
    {
        printf("empty");
        return;
    }
    int i,cnt=0;
    struct node*dummy=head;
        while(dummy->next!=NULL)
        {
            cnt++;
            dummy=dummy->next;
        }
        if(position>cnt)
        return;
    if(position==0)
    {
        struct node*temp=head;
        head=head->next;
        free(temp);
    }
    else
    {
        struct node*temp=head;
        for(i=0;i<position-1;i++)
        {
            if(temp!=NULL)
            temp=temp->next;
            else
            return;
        }
        temp->next=temp->next->next;
    }
}
void deleteFront()
{
    if(head==NULL)
    {
        printf("empty");
        return;
    }
    struct node*temp=head;
    head=head->next;
    free(temp);
    if(head==NULL)
    {
        printf("empty");
        return;
    }
}
void print()
{
    struct node*temp=head;
    while(temp!=NULL)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
int main()
{ 
char a;
do{
    char tmp;
    int b,c;
    scanf("%c",&a);
    if(a=='r')
    {
        deleteFront();
        print();
    }
    else if(a=='i')
    {
        scanf("%d",&b);
        scanf("%d",&c);
        insertAt(b,c);  
        print();
    }
    else if(a=='f')
    {
        scanf("%d",&b);
        insertFront(b);
        print();
    }
    else if(a=='d')
    {
        scanf("%d",&b);
        deleteAt(b);
        print();
    }
    scanf("%c",&tmp);
}while(a!='q');
    return 0;
}
If I remove the lines I marked as marker in the form of comment lines in the function insertAt(), I get a segfault. 
When I use them I get a wrong answer. I tested for many cases and I couldn't figure out where I was wrong. 
Could someone help me out?
 
     
     
    