• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ Linked list insert problem

Joined
May 19, 2007
Messages
8
I HAVE A NEW QUESTION!

How do I use the overloaded >> operator to print the result? I have no idea how to do it!


--------------------------------------------------------------------------------------------------------------------------------------

OK so I wrote this function which is supposed to add peoples names alphabetically. It is called InsertPassenger, and it calls on a function called Add (just to make it fancy). If you ask me, I think the codes works. But if you ask the compiler it'll say I'm wrong (apparently)
I also wrote a Print function to check if Add works. I'm not sure which one doesn't work, Add or Print. When I say doesn't work, I mean the following:
The program starts, I enter a name, and everything is fine. Then I choose to print and that's where I get an error. The program says it had to exit out. When I debugg, I'm told that there's a problem with this line within the Print function:

Code:
while ( current->next != NULL )
Could someone please tell me why this doesn't work:

First of all my structure:

Code:
struct Air
{
	string person;
	Air *next;
};

These are the functions:
Code:
void JGOp::InsertPassenger()
{
	cout << "Enter passenger: ";
	cin >> input;
	Add(input);	

}

void JGOp::Add(string input)
{

    Air *current, *previous;
    Air *newp=new Air;
    
    // populate newp
    newp->person =  input;
    newp->next = NULL; // very important to initialize this
    
    if (head == NULL)
    {
        head = newp;
		return;
    }
    else if (newp->person < head->person)
    {
        newp->next=head;
        head=newp;
        return;
    }

    current = head;
    while (current) 
	{
        if (newp->person < current->person)
           break;
        previous = current;
        current = current->next;
    }
    
    previous->next = newp;
    newp->next = current;
    return;
}

void JGOp::Print()
{
	Air *current;
	Air *head=new Air;
	
	current = head;

	if ( current != NULL )
	{
		while ( current->next != NULL ) 
		{
			cout << current->person;
			current=current->next;
		}
		cout<< current->person;
	}
}

Any help would be greatly appreciated
 
one problem i see is that in your print code you allocate head to be a new Air object. so when you set current = head its being set to an empty struct. I'm assuming what you meant to do was to make head a global variable or something.
 
hehe funny was just going over link list with someone today. Anyways, I have 2 major concerns, why are you creating a new Node (air), when its not really pointing to anything. Also, the if and while loop condition might be your issue, inside your while you evalute for current next, but you also update it for current next. So you are essentially doing current->next->next. Here is how I would fix it.

Code:
//Assuming you have a member variable head pointer in your Link List class
void JGOp::Print()
{
      for(Air *current = head; current; current = current->next)
      {
         cout << current->person;
      }
}
 
Thanks for all the feedback. So from reading what you guys wrote, there's nothing wrong with my Add fucntion... right?

The reason I ask is because I tried your Print function (YoNeX), and this time, the program doesn't give me an error, but it also doesn't print out the passenger. Wouldn't that mean that the add isn't working properly?

NEVERMIND!!!! IT WORKS! I just didn't see the words print out. THANK YOU SO MUCH!!!!
 
You didn't ask us to look at Add(). It's working, I think, but it's slower than death.
 
You didn't ask us to look at Add(). It's working, I think, but it's slower than death.

It may be slow, but it appears that it'd be a list sorted on Air.person, provided that his sting class implements the `<' operator correctly.

Colossusx10 is input a private variable in your class? If so, why pass it as a parameter to the add function?
 
Back
Top