[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

This only prints -1,2,-4

Posted on 1998-01-06
1
Medium Priority
?
217 Views
Last Modified: 2010-04-02
What is wrong with this code, please make all code enhancements in a new c file..
It only prints -1,2-4 plus it doesn't seem to sort the incomming integer..


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

 typedef struct node
 {
      int number;
      struct node *next;
 }Node;

 void additem(int a);
 void print(void);
 Node *head=NULL;

 main()
 {
      int i=0;
      int a1[5]={-1,2,-4,67,78};
      for (i=0;i<5;i++)
      {
        additem(a1[i]);
      }
      print();
}

void additem(int a)
{
  Node *curr;
  Node *new_node;
  Node *prev;
  Node *temp;

  new_node=(Node *)malloc(sizeof(Node));
  if (new_node==NULL)
  {
       printf("I am dreadfully sorry.. You don't have enough conventional memory to add the New Node\n");
       exit(1);
  }
  new_node->next=NULL;
  new_node->number=a;
  if (head==NULL)
  {
       head=new_node;
       return;
  }

  prev=NULL;
  curr=head;
  temp=NULL;
  temp=curr->next;
  while(curr->next != NULL)
  {

       if (curr->number< new_node->number&&new_node->number>temp->number)
       {
            new_node->next=temp;
            temp=new_node;
          return;
       }
       else
       {
             curr=curr->next;
             temp=curr->next;
       }
  }
  curr->next=new_node;
  return;
}

void print(void)
{
  Node *curr;
  int i=0;

  curr=head;



      if (curr==NULL)
      {
            printf("I am sorry this is an empty list..There is no data in the list\n");
            return;
      }

      while(curr != NULL)
      {
            printf("Node: [%d] = %d\n",i,curr->number);
            i++;
            curr=curr->next;
      }
}

Please rewrite the code. with /* showing why and what you did*/
0
Comment
Question by:johnm112496
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
mlev earned 400 total points
ID: 1256927
There are 3 bugs here:
1. The comparison clause is incorrect, should be
if (curr->number < new_node->number && new_node->number < temp->number)
2. When inserting a new element into the middle of the list, the pointer in the previous element is not updated.
3. The special case where a new element should be placed in the head of the list is not handled.
4. Of course, this can only work if all numbers are distinct. Otherwise '<=' instead of '<' is needed.

The whole code (corrected):
#include <stdio.h>
#include <stdlib.h>

typedef struct node
  {
    int number;
    struct node *next;
  }
Node;

void additem (int a);
void print (void);
Node *head = NULL;

main ()
{
  int i = 0;
  int a1[5] =
  {-1, 2, -4, 67, 78};
  for (i = 0; i < 5; i++)
    {
      additem (a1[i]);
    }
  print ();
}

void
additem (int a)
{
  Node *curr;
  Node *new_node;
  Node *prev;
  Node *temp;

  new_node = (Node *) malloc (sizeof (Node));
  if (new_node == NULL)
    {
      printf ("I am dreadfully sorry.. You don't have enough conventional memory to add the New Node\n");
      exit (1);
    }
  new_node->next = NULL;
  new_node->number = a;
  if (head == NULL)
    {
      head = new_node;
      return;
    }

  if (new_node->number < head->number) /* this case was not handled */
    {
      new_node->next = head;
      head = new_node;
      return;
    }

  prev = NULL;
  curr = head;
  temp = NULL;
  temp = curr->next;
  while (curr->next != NULL)
    {

      if (curr->number < new_node->number && new_node->number < temp->number) /* the second comparison was inverted */
        {
          new_node->next = temp;
          temp = new_node;
          curr->next = new_node; /* this line was mising */
          return;
        }
      else
        {
          curr = curr->next;
          temp = curr->next;
        }
    }
  curr->next = new_node;
  return;
}

void
print (void)
{
  Node *curr;
  int i = 0;

  curr = head;



  if (curr == NULL)
    {
      printf ("I am sorry this is an empty list..There is no data in the list\n");
      return;
    }

  while (curr != NULL)
    {
      printf ("Node: [%d] = %d\n", i, curr->number);
      i++;
      curr = curr->next;
    }
}

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question