linked list problems (null values)

This is a discussion on linked list problems (null values) within the C++ Programming forums, part of the General Programming Boards category; I'm trying to load a bunch of information in to a linked list so I don't have to create a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    linked list problems (null values)

    I'm trying to load a bunch of information in to a linked list so I don't have to create a ton of class declarations. I want the pointer 'next', that points to the next link, to be null on the last link. I can't seem to accomplish this. I tried it a couple different ways and it either thinks the head link is null, or the link is never ending. If I treat the linked list as a list with only one link (just a head) then all goes well. So any help here would be appreciated.

    Code:
    //The beginning portion of my loading function where I think it's having trouble
    
    bool LOADOBJ::LoadObjFromFile(char* filename)
    {
        std::ifstream file; //create filestream
        file.open(filename); //open the file for reading
        if(!file.is_open()) return false; //if the file could not be opened
        else{
    
            /*first find next available spot in the list*/
            ObjectList *temp = GetNextSpace(); //get the next space available
            temp = new ObjectList; //allocate the space
            temp->face_count = 0; //set face count
            temp->vert_per_face.clear(); //set vertices per face
            temp->next = NULL; //set next link to NULL
    
    //....... there's more to this function but its kind of long
    
    
    
    //the function that finds the end of the link
    //I tried dynamically allocating here vs. doing it inside the load
    //function, but it doesn't make a difference
    
    ObjectList* LOADOBJ::GetNextSpace()
    {
        if(head == NULL){
            return head;
        }
        else if(head != NULL){
            ObjectList *temp = head;
            while(temp != NULL)
                temp = temp->next;
            return temp;
        }
    }
    
    //and the structure for the linked list
    
    struct ObjectList
    {
        ObjectList *next; //pointer to next loaded object
        vector<float> v_x, v_y, v_z; //store x, y, and z components for vertices
        vector<float> vn_x, vn_y, vn_z; //store x, y, and z components for vertice normals
        vector<float> vt_x, vt_y, vt_z; //store x, y, and z components for vertice textures
        vector<int> fv, fvt, fvn; //vertices, vertice textures, vertice normals of each face
        int face_count; //face count
        vector<int> vert_per_face; //vertices per face
        int fvarray[][4]; //face vertex array (not currently used)
    };
    
    //finally the class
    
    class LOADOBJ
    {
        private:
            ObjectList *head; //head ptr in linked list of loaded object files
            int obj_load_count; //number of loaded object files
            void Destroy(); //destroys first link in the list
            ObjectList* GetNextSpace(); //get next available space in the linked list
        public:
            LOADOBJ(); //constructor
            ~LOADOBJ(); //destructor
            
            bool LoadObjFromFile(char*); //load object file
            int GetFileCount(); //get number of object files that have been loaded
            bool isEmpty(); //determines if the linked list is empty
            bool RenderAllObjects(); //render all the objects loaded in the linked list
            
    }; //end LOADOBJ class

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > ObjectList *temp = GetNextSpace(); //get the next space available
    Try
    ObjectList *tail = GetNextSpace(); //get the next space available

    Then after the rest of your blue code
    Code:
    if ( head == NULL ) {
      head = temp;
    }
    else {
      tail->next = temp;
    }
    You're already using <vector>, so read up on <list> as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I tried what you suggested and it worked for loading more than one object, but they some how did not end up being in the same linked list. It was more like I loaded obj1 to the head of the list, then loaded obj2 in to random space that was not access-able. I modified my constructor and GetNextSpace() function to get the link list set up properly. The problem I have now is I cannot load more than 2 objects in to the linked list. The program crashes on the third object load. So I am kind of stuck on why it will not go past two links. Here is what I have now...

    Code:
    bool LOADOBJ::LoadObjFromFile(char* filename)
    {
        std::ifstream file; //create filestream
        file.open(filename); //open the file for reading
        if(!file.is_open()) return false; //if the file could not be opened
        else{
            
            /*first find next available spot in the list*/
            ObjectList *temp = GetNextSpace(); //the variable 'temp' is needed since the rest of my load function uses temp
    
    //.... the rest of LoadObjFromFile....
    
    
    //function for getting the next space
    ObjectList* LOADOBJ::GetNextSpace()
    {
        ObjectList *temp = head;
        if(temp == NULL){
            temp = new ObjectList;
            temp->face_count = 0;
            temp->next = NULL;
            head = temp;
            return head;
        }
        else if(temp != NULL){
            while(temp->next != NULL) temp = temp->next;
            
            ObjectList *tail = new ObjectList;
            tail->next = NULL;
            tail->face_count = 0;
            temp->next = tail;
            return tail;
        }
        //return temp;
    }
    
    //constructor
    LOADOBJ::LOADOBJ()
    {
        obj_load_count = 0; //nothing is loaded so zero
        head = NULL;// new ObjectList; //pointer points allocated space
    }
    Last edited by scwizzo; 12-03-2008 at 05:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21