Hi,

I wrote a program that inserts nodes into a linked list in descending order.But whenever I test my code with numbers 12,14,13,19,7 in this order.Whenever I entered 7 I took 7 is already in the list.But as easily seen 7 is not in the list before I inserted.After give this error,if I choose print option by typing 2 my program entered in an infinite loop.I can not see my mistake and I am very confused.Please help me.My code is in the following:

include
include

struct node { int content; struct node *nextLink; };

typedef struct node NODE;

void print(NODE *); int insertNode(NODE **head,int x);

int main(void) {

int num,choice; NODE *head; head=NULL;

do {

printf("\nPlease press 1 to insert or press 2 to print or press 0 to exit\n");
scanf("%d",&choice);
switch(choice)
{

case 0:
     return 0;
     break;
case 1:
     printf("Enter an integer to insert into the linkedlist:  ");
     printf("\n");
     scanf("%d",&num);
     insertNode(&head,num);
     break;
case 2:
     print(head);
     break;
default:
     printf("You entered an invalid number\n");
     return 0;
     break;
}   

}while(choice==1 || choice==2);

return 0; }

int insertNode(NODE **head,int i) { NODE *newNode; newNode = (NODE *)malloc ( sizeof (NODE)); newNode->content = i; NODE *temporary= *head; newNode->nextLink=NULL;

if ( (*head == NULL) || ((*head) -> content) < i)
{
    *head = newNode ;
    (*head)-> nextLink = temporary ;
}


else
{
    do
    {
        if ( ((temporary-> content) > i ) && ((temporary-> nextLink ->content) < i))
        {


            newNode -> nextLink = temporary -> nextLink;
            temporary -> nextLink = newNode;
            return ;

        }


       else if(temporary->content==i)
       {

            printf("To be inserted value is already in the list\n");
            return ;

       }
  temporary=temporary->nextLink;
  }while ( temporary->nextLink != NULL );

  if(temporary->content==i)
  {

    printf("To be inserted value is already in the list\n");
    return;

  }


  temporary->nextLink=newNode;

} return 0; }

void print( NODE *head) {

if(head==NULL)
{
  printf("\nLinkedList is empty \n");

}
while ( head != NULL )
{
    printf( "%d ", head -> content ) ;
    head = head -> nextLink ;
}

}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Your formatting leaves a lot to be desired. It's a lot easier to read and debug if you only put one statement on a line instead of trying to put several on the same line. Most debuggers can't work with that formatting style. Once you learn how to format your code better it will be a lot easier to find mistakesw.


Here is an example of what I am talking about

int insertNode(NODE **head,int i) 
{ 
    NODE *newNode; 
    newNode = (NODE *)malloc ( sizeof (NODE)); 
    newNode->content = i; 
    NODE *temporary= *head; 
    newNode->nextLink=NULL;

    if ( (*head == NULL) || ((*head) -> content) < i)
    {
        *head = newNode ;
        (*head)-> nextLink = temporary ;
    }
    else
    {
        do
        {
            if ( ((temporary-> content) > i ) && ((temporary-> nextLink ->content) < i))
            {
                newNode -> nextLink = temporary -> nextLink;
                temporary -> nextLink = newNode;
                return ;
            }
            else if(temporary->content==i)
            {
                printf("To be inserted value is already in the list\n");
                return ;
            }
            temporary=temporary->nextLink;
        }while ( temporary->nextLink != NULL );

        if(temporary->content==i)
        {
            printf("To be inserted value is already in the list\n");
            return;
        }
        temporary->nextLink=newNode;

    } 
    return 0; 
}

I dunno about you but I tried to run your code (with minor modifications on your "include" part) and it seems to be working fine. I tried it on cygwin though (I'm too lazy to boot my linux box).

Here's the output of your code for your reference:

$ ./a.exe

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
12

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
14

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
13

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
19

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
7

Please press 1 to insert or press 2 to print or press 0 to exit
2
19 14 13 12 7
Please press 1 to insert or press 2 to print or press 0 to exit
0

<<nice exit here>>

It does, however, go on infinite loops when you insert non-numbers.
Try to use your debugger and step through your code. I, however will go to sleep. :)

And yes, your formatting is terrible. You will do humanity a great service by formatting your code well.

Good luck!

Edited 5 Years Ago by yan0: n/a

Your code runs good... just take care of formatting... it will help you to debug

$ ./a.out

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
19

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
14
Segmentation fault
sourabh@ubuntu:~/C codes$ ./a.out

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
12

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
14

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
13

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
19

Please press 1 to insert or press 2 to print or press 0 to exit
1
Enter an integer to insert into the linkedlist:
7

Please press 1 to insert or press 2 to print or press 0 to exit
2
19 14 13 12 7
Please press 1 to insert or press 2 to print or press 0 to exit
0

This article has been dead for over six months. Start a new discussion instead.