?
Solved

Linked list error

Posted on 2004-11-03
7
Medium Priority
?
320 Views
Last Modified: 2013-12-14

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;
}
0
Comment
Question by:blaze_wk
  • 4
  • 2
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12489093
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
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 600 total points
ID: 12489113
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
 

Author Comment

by:blaze_wk
ID: 12489158
hm, im kind of a beginner here.. my atempt at it might not be enough..
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Accepted Solution

by:
Kent Olsen earned 900 total points
ID: 12489163
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
 

Author Comment

by:blaze_wk
ID: 12489282
sorry i would need an example to assist me
0
 

Author Comment

by:blaze_wk
ID: 12489491
sorry for my lack in C knowledge...
0
 

Author Comment

by:blaze_wk
ID: 12490584
brilliant!
it works!
thanks for the help guys
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

621 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