Heres my problem. I can add a node to my linked list in the front and middle, and at the end. But if I add a node to the end of the list and try to print it out, the program crashes. I studied the code and didn't find any reason why it would do this. I also asked my programming teacher why and he couldn't figure it out.
Secondly, if i try to add to the end more than twice, i loose the linked list completly and the program crashes even without trying to print it. Any help would be appreciated.:)

Heres my header file:

#include <iostream>
using namespace std;

struct node
{
	  int data;
		node * next;
};

class linked_list
{
  private:
		node *hd;

  public:
		linked_list()
		{
			hd = NULL;
		}

		void add_to_end(int data)
		{
			node *temp;

			if (hd == NULL)
			{
				node *new_node;
				new_node = new node;
				new_node->data = data;
				//if I omit the next line then the print function works fine
				hd = new_node;
			}
			else
			{
				for (temp = hd; temp->next != NULL; temp = temp->next){}

				node *new_node;
				new_node = new node;
        new_node->data = data;
				//if I omit the next line then the print function works fine
				temp->next = new_node;
			}
		}

		void add_to_begin(int data)
		{
			node *new_head;
			new_head = new node;
			new_head->data = data;
			new_head->next = hd;
			hd = new_head;
		}

		void add_to_pos(int pos, int data)
		{
			int i;
			node *temp;
			node *new_node;
			new_node = new node;
			new_node->data = data;

			if (pos == 0)
				add_to_begin(data);
			else
			{
				for (temp = hd, i = 1; temp->next != NULL && i < pos; temp = temp->next, i++){}

				if (temp->next != NULL)
					  new_node->next = temp->next;
				temp->next = new_node;
			}
		}

		void print_list()
		{
			node *temp;
			
			for (temp = hd; temp != NULL; temp = temp->next)
			{
				cout << temp->data << " ";
			}
		}
};

And heres my main file:

#include <iostream>
#include "linked_list.h"
using namespace std;

int main()
{
	linked_list test;

	test.add_to_end(1);
	test.add_to_begin(2);
	test.add_to_begin(3);
	test.add_to_end(5);
	test.add_to_pos(1,4);
  test.print_list();

	return 0;
}

Any help would be appreciated.:)

Recommended Answers

All 3 Replies

I think the problem is that you are not setting the 'next' pointer of the last node to NULL. It cab be containing some garbage and giving unexpected results. Can you try this.

void add_to_end(int data)
{
	cout << "add to end" << endl;
	node *temp;

	if (hd == NULL)
	{
		node *new_node;
		new_node = new node;
		new_node->data = data;
		[B]new_node->next = NULL[/B];
		//if I omit the next line then the print function works fine
		hd = new_node;
	}
	else
	{
		for (temp = hd; temp->next != NULL; temp = temp->next){
			cout << "for loop" << endl;
		}

		node *new_node;
		new_node = new node;
		new_node->data = data;
		[B]new_node->next = NULL[/B];
		//if I omit the next line then the print function works fine
		temp->next = new_node;
	}
}

edit:concurrent replies i guess. hope you got the point.

Sweet. Putting the line:
- new_node->next = NULL;
solves the crashing problem. Now I see that the program was crashing because it was trying to access memory that didn't exist. Thanks a lot Salem and Agni.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.