Link to home
Start Free TrialLog in
Avatar of blaze_wk
blaze_wk

asked on

Linked list error


typedef struct node {
int num;
struct node *next;
}message;
int main ()
{
   FILE *file;
   char array[5];
   message *temp *head, *ptr, *previous;
   head = 0;  
 
   if ((file=fopen(argv[1],"r")) == NULL)
   printf("nofile\n");

   while (fgets(array, sizeof(array),file)!= NULL)
   {
      temp = malloc (sizeof(struct node));
      temp -> num = atoi(array);
         
      previous = 0;

      while ((ptr != 0) && ( (atoi(array)) > (ptr -> num)))
      {
          previous = ptr;
          ptr = ptr -> next;
      }

      temp -> next = ptr;

      if (previous != 0 )
          previous -> next = temp;
      else
          head = temp;

      /*im not so sure of how to not add the same value. For example, if num = a previous node,
         it wouldn't append that num to the list. */

      }
   return 0;
}
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

To now if value exist you have to separate problem in a different function. Let's say
     int FindValue(int value);
So, before trying to insert node into linked list, call function first.
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blaze_wk
blaze_wk

ASKER

hm, im kind of a beginner here.. my atempt at it might not be enough..
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry i would need an example to assist me
sorry for my lack in C knowledge...
brilliant!
it works!
thanks for the help guys