Thread: Initializing a Linked List

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    Initializing a Linked List

    I'll keep this short and to the point.

    Each line in a data file I will open up in my C program to read contains the vertices for one face of the object, made by an amount of vertices, made up of it's x y z points.

    Ex: (1st) x, y, z, (2nd) x, y, z, (3rd) x, y, z, (4th) x, y, z ... (can have more than four vertices per face)
    Ex: 0.139383 -0.239560 0.394645 0.140653 -0.243475 0.393312 0.141220 -0.243473 0.393834 0.139949 -0.239557 0.395167 (has four vertices in this face)

    The data file has many lines, aka, many faces in the object.

    After opening the file with fopen, how can I put the data into a linked list?

    I know I will need a while loop that will sort through the data starting at the beginning and assigning the values into a struct I have called point.
    Code:
    typedef struct _point
    {
      float x, y, z;
    } point;
    I also have setup a linked list.
    Code:
    struct node
    {
      int vCount;                   // pointer to data value to be stored
      point *vptr[10];              // pointer to array of structs containing vertices
      struct node *next;            // pointer to next node
    };
    Problem is I have no idea how to tell the program to read the file (other than fopen) and to assign the info into the struct and how to tell it to go onto the next node when hitting the return character at the end of each line. If you could give me a push in the right direction, it would be a big help. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How do you ever add nodes to a linked list? Don't you have a function like "addtolist(struct node **head, node to_be_added)"?

    Also, if the only idea you have for reading a file is fopen, then you don't have any ideas for reading a file, since fopen, as you might guess, opens a file. You could use, say, fgets for reading from a file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM