1

I am getting error while executing this. Compiler does not give any error but when executed it give random output.

What am i doing is taking input from user & storing them to linked list & then implementing insertion sort. ( I am able to call the subroutine insertion sort, so I guess the problem lies within subroutine only)

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

struct node
{
    int info;
    struct node *next;
};
typedef struct node *NODEPTR;

NODEPTR getnode();
NODEPTR insertionSortLinkedList(NODEPTR head);

int main()
{
    int i,n,temp;
    NODEPTR head,lptr,prevlptr;
    printf("No of input integers to be sorted\n");
    scanf("%d",&n);
    if (n<2){printf("n should be atleast 2 \n");return 0;}
    printf("\nType space-separated array of %d integers\n",n);
    scanf("%d",&temp);

    head=getnode();
    head->info=temp;
    head->next=NULL;
    prevlptr=head;
    for (i=0;i<n-1;i++)
    {
        scanf("%d",&temp);
        lptr=getnode();
        lptr->info=temp;
        prevlptr->next=lptr;
        lptr->next=NULL;
        prevlptr=lptr;  
    }

    head=insertionSortLinkedList(head);

    lptr=head;
    while(lptr!=NULL)
    {
        printf("%d ",lptr->info);
        prevlptr=lptr;
        lptr=lptr->next;
        free(prevlptr);
    }

    return 0;
}


NODEPTR getnode()
{
    NODEPTR p;
    p=(struct node*)malloc(sizeof(struct node));
    return p;
}



NODEPTR insertionSort(NODEPTR head)
{
    NODEPTR listptr,tempptr,prevptr,prevtempptr;
    prevptr=head;
    listptr=prevptr->next;
    while(listptr!=NULL)
    {
        while (listptr->info < prevptr->info)
        {
            prevptr->next=listptr->next;
            tempptr=head;
            prevtempptr=head;
            while(tempptr->info <= listptr->info)
            {
                prevtempptr=tempptr;
                tempptr=tempptr->next;
            }
            if(tempptr->info == prevtempptr->info)
            {
                listptr->next=head;
                head=listptr;
            }
            else
            {e
                listptr->next=prevtempptr->next;
                prevtempptr->next=listptr;              
            }
            listptr=prevptr->next;
            if (listptr==NULL)
            break;
        }
        prevptr=prevptr->next;
        if (prevptr=NULL)
            break;
        listptr=prevptr->next;              
    }
    return(head);
}

What is wrong with my code and how do I fix it?

mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
  • 1
    You might like to take your time to trace your code using a debugger. – alk Sep 28 '13 at 09:09
  • In C there is not need to cast the results of `malloc/calloc/realloc`, not is it recommended: http://stackoverflow.com/a/605858/694576 – alk Sep 28 '13 at 09:11

1 Answers1

4

The 6th line form last

if (prevptr=NULL)

it should be

if (prevptr==NULL)

Single "=" does the assignment instead of comparison.

mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
  • 3
    +1. Might be worth mentioning that compiling with warnings enabled would have probably flagged this (possibly as "assignment in conditional expression") – simonc Sep 28 '13 at 09:04
  • 2
    @user2825927: Turn on compiler warnings (`-Wall`) to help catch bugs like these in the future. Also click the checkmark next to an answer to accept it. – Jon Purdy Sep 28 '13 at 09:04
  • how to do it , I am on windows , generally use DevC++ or CFree. – user2825927 Sep 28 '13 at 09:06
  • 4
    After a certain number of debugging session having lead to this kind of error I decided to **always** put the constant to the left if "trying" to test for equality. – alk Sep 28 '13 at 09:07