1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Connecting link lists

Discussion in 'C++' started by PcCopat, Mar 20, 2010.

  1. PcCopat

    PcCopat New Member

    Joined:
    Mar 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I want to make a linked list program consisting of one main link, and each main links have sub link like

    Code:
    Main 1 -> Main 2 -> Main 3 -> ... -> Main N -> NULL
       |        |          |                        
    Sub 1-1    ...       ...
       |                
    Sub 1-2    
       |              
      ...         
    
    I have two structs for these:

    Code:
    struct NodeSmall
    {
        int documentNum;
        NodeSmall *nextNum;
        NodeSmall()
        {
            documentNum = 0;
            nextNum = NULL;
        }
    };
    
    struct NodeBig
    {
        string keyword;
        NodeBig *nxtkyw; //Link to next keyword
        NodeSmall *docNum; //Link to document number
    
        NodeBig()
        {
            keyword = "";
            nxtkyw = NULL;
        }
        
    };
    And I have a test main which reads first line from text file:

    Code:
    int main()
    {
    
        string line, key;
        int number;
        ifstream dbfile;
        dbfile.open("docdb.txt");
    
        dbfile >> key >> number;
    
        NodeBig *head = new NodeBig;
        NodeSmall *docNum = new NodeSmall;
        
        head->keyword = key;
        head->nxtkyw = NULL;
        head->docNum->documentNum = number;
    
        cout << "keyword is " << head->keyword << " and it is on " << head->docNum->documentNum;

    It does not give compiler error (vs2008) but it gives stopped working error after running the program. I think I've made a mistake while connecting link lists together. But I cannot find similar probles on internet. What should I do for fixing this error? Thank you.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    head->docNum->documentNum = number;

    is not valid because docNum is not assigned anything.

    Have you checked if your file exists and it reads the number into the variables fine.
     
  3. PcCopat

    PcCopat New Member

    Joined:
    Mar 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I've found the error.

    I should have used
    Code:
    head->docNum = new NodeSmall;
    instead of
    Code:
    NodeSmall *docNum = new NodeSmall;
     

Share This Page