Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. I've been pretty stumped with this problem for a little while and have exhausted youtube and google. I'm having trouble inserting data into a multi data linked list node. I'm not sure if I am blowing a pointer or what, but I am severely confused.


<If I call

inFile1 >> S >> N;
insertnode(head,S,N);

separately without the loop, it stores the data in the linked lists and prints it out (with an extra unintended value ==> 6.83919e+025, which I'm thinking is a memory location address)>

Any help will be greatly appreciated.

What I have tried:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>

using namespace std;

struct node
{
    string element;
    double weight;
    node *next;
};

void insertnode(node *cp,string,double);
void printlist(node *cp);

node *head;

ifstream inFile1, inFile2;
ofstream outFile;

int main()
{
    inFile1.open("Element.txt", ios::in);
    inFile2.open("Formula.txt", ios::in);

    string S;
    double N;
    int i;
    head = new node;
    head->element;
    node *c;

    c = head;

    //while(inFile1 >> S)
    //{
        inFile1 >> S >> N;
        insertnode(head,S,N);
        inFile1 >> S >> N;
        insertnode(head,S,N);
        inFile1 >> S >> N;
        insertnode(head,S,N);
    //}


    printlist(head);


    inFile1.close();
    inFile2.close();

    return 0;
}
node *
newnode()
{
    node *t;

    t = new node;
    t->element = "";
    t->weight = 0;
    t->next = NULL;
    return t;
}
void insertnode(node *cp, string S, double N)
{
    node *p, *t;
    p = NULL;

    if(head == NULL)
    {
        head = newnode();
        head->element = S;
        head->weight = N;
    }
    else
    {
        cp = head;
        while(cp != NULL && cp->element.compare(S) > 0)
        {
            p = cp;
            cp = cp->next;
        }
        if(p != NULL)
        {
            t = newnode();
            t->element = S;
            t->weight = N;
            p->next = t;
            t->next = cp;
        }
        else
        {
            t = newnode();
            t->element = S;
            t->weight = N;
            t->next = cp;
            head = t;
        }
    }
}
void printlist(node *cp)
{
    while(cp)
    {
        cout << cp->element << setw(8) << cp->weight << endl;
        cp = cp->next;
    }
}
Posted
Updated 22-Oct-18 17:04pm
Comments
CPallini 23-Oct-18 3:37am    
Is it an exercise? You know the C++ Standard Library provides linked lists.
What is the content of the input file?

1 solution

It appears to me the problem is in initializing the list. Start by setting Node to nullptr at its declaration. Next, assign values to the members of a structure in one and ONLY one place. This will simplify the rest of your logic. Here's a small sample of that -
C++
struct node
{
    string element;
    double weight;
    node * next;

    node()   // constructor
    {
        Set( "", 0, nullptr );
    }

    void Set( string e, double w, node* pnext=nullptr )
    {
        element = e;
        weight = w;
        next = pnext;
    }

    static node * Create()   // this replaces the newnode function
    {                        // usage is node *pn = node::Create();
        return new node;
    }
};

typedef node * pnode;

pnode Head = nullptr;  // I capitalize first letter of global variables
The insert function can be considerably simplified now. There is another problem though. That is you are passing head as an argument to the insert function and also accessing it as a global variable. That will almost always work out badly. Usually, a pointer to the head of the list is passed but that isn't absolutely necessary. Here's a way to deal with it only as a global variable.
C++
void insertnode( string s, double n )
{
    if( ! Head )
    {
        Head = node::Create();
        Head->Set( s, n );
        return;
    }

    node * prv = NULL;
    node * cur = Head;
    while( cur && ( cur->element.compare(s) > 0 ) )
    {
        prv = cur;
        cur = cur->next;
    }

    node * pnew = node::Create();
    pnew->Set( s, n, cur );

    if( prv )
        prv->next = pnew;
    else
        Head = pnew;   // this is the new head element
}
With this code, you don't pass the head of the list to the function. You easily could but you have to pass the address of the pointer. I didn't think you are quite ready for that yet.

This would be the prototype if a pointer to the head of the list is passed:
C++
void insertnode( pnode * pHead, string e, double w );
 
Share this answer
 
v3
Comments
KarstenK 23-Oct-18 2:21am    
Why are you using a struct? You better use a clase and it notation.
Rick York 23-Oct-18 3:36am    
The question uses a struct and for this purpose it works the same. Normally I would and I Typically use structs only for POD.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900