0

Hi
Does anybody knows what is wrong with taht code? Why it says it is empty link list? It should fill with the numbers I enter. Could anybody help to answer the question?

Thanks

#include<stdio.h>
#include<conio.h>

struct Node
{
 int number;
 struct Node *link;
};

typedef struct Node* NodePtr;

void printNode(NodePtr head);
void insertNode(NodePtr *head,int numb);

int main(void)
{
    NodePtr head;
    int data;
   
    head=NULL;
    
     printf("\nWhat is the number : ");
     scanf("%d", &data);
     
    while(data>0)
    {
     insertNode(&head,data);
     printNode(head);
     printf("\nWhat is the number : ");
     scanf("%d", &data);
    }
       
    
       
    getch();
    return 0;
}

void printNode(NodePtr head)
{
     if (head==NULL)
     printf("The linked lists is NULL");
     else
     {
          printf("The nodes contain the numbers: \n");
     while (head!=NULL)
     {
           printf("%d -->",head->number);
           head=head->link;
     }
     printf(" NULL");
     }
} 

void insertNode(NodePtr *head,int numb)
{
     NodePtr newPtr;
     NodePtr previousPtr;
     NodePtr currentPtr;
     
     newPtr=malloc(sizeof(struct Node));
     newPtr->number=numb;
     newPtr->link=NULL;
     
     previousPtr=NULL;
     currentPtr=*head;
     
     while(currentPtr!=NULL && numb>currentPtr->number)
     {
      previousPtr=currentPtr;
      currentPtr=currentPtr->link;
     }
     
     if (previousPtr==NULL)
     {
      newPtr->link=currentPtr;
      currentPtr=newPtr;
     }
     else
     {
      previousPtr->link=newPtr;
      newPtr->link=currentPtr;
     }
}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by mauro21pl
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.