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;
}
blaze_wkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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.
0
Jaime OlivaresSoftware ArchitectCommented:
Sorry, some typos. I mean:
To know....

Your FindValue() function must scan all the linked list asking for matching value. If found must return 1, if end of list is reached, then return 0
So you can use as:

if ( !FindValue(num) ) {
    /* Proceed to create node and insert it into linked list. */
}
0
blaze_wkAuthor Commented:
hm, im kind of a beginner here.. my atempt at it might not be enough..
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Kent OlsenData Warehouse Architect / DBACommented:
Hi blaze wk,

I think that it will be much easier for you to develop your application if you'll split it into logical pieces.  Here's a sample, using your program as a guide.  (I'm not testing this, just demonstrating.....)

typedef struct node {
  int num;
  struct node *next;
}message;

message *head = NULL;

message *FindNode (message *Head, int Key)  /*  Search for the node where num == Key  */
{
}

message *AddNode (message *Head, char *Buffer)
{
  message *temp;
  message *previous;
  message *ptr;
 
  temp = malloc (sizeof(struct node));
  temp -> num = atoi(array);
         
  previous = 0;
  ptr = Head;                               /*  You forgot this the fist time around  */
 
  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. */
}  

int main ()
{
   FILE *file;
   char array[5];
 
   if ((file=fopen(argv[1],"r")) == NULL)
   {
     printf("nofile\n");
     exit (0);
   }

   
   while (fgets(array, sizeof(array),file)!= NULL)
   {
     head = AddNode (head, array);
   }
   return 0;
}


Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blaze_wkAuthor Commented:
sorry i would need an example to assist me
0
blaze_wkAuthor Commented:
sorry for my lack in C knowledge...
0
blaze_wkAuthor Commented:
brilliant!
it works!
thanks for the help guys
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.