Linked List

i'm having a trouble about my program. it is having an unhandled exception.

here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//MyLinkedList.h
#ifndef MyLinkedList_h
#define MyLinkedList_h

class LinkedList
{
	struct nodeType
	{
		int info;
		nodeType* link;
	};

	nodeType* head;
	nodeType* current;

public:
	void AddNode();
	void DisplayNodeData();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//MyLinkedList.cpp
#include <iostream>
#include "MyLinkedList.h"

using namespace std;

void LinkedList::AddNode()
{
	nodeType* newNode;

	newNode = new nodeType;

	cout << "Enter data: ";
	cin >> newNode -> info;
	newNode -> link = NULL;

	if(head == NULL)
	{
		head = newNode;
	}
	else
	{
		current = head;
		while(current -> link != NULL)
		{
			current = current -> link;
		}
		current -> link = newNode;
	}
}

void LinkedList::DisplayNodeData()
{
	nodeType *temp;
	temp = head;
	cout << "\n\n=========================\n";

	if(temp == NULL)
		cout << "List is empty!" << endl;
	else
		while(temp != NULL)
		{
			cout << "Info: " << temp -> info;
			cout << "\n";
			temp = temp -> link;
		}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//ProgExer01.cpp
#include <iostream>
#include "MyLinkedList.h"

using namespace std;

int main()
{
	int numberOfNodes;
	LinkedList linked;
	cout << "How many nodes do you want to add? (integers only): ";
	cin>> numberOfNodes;
	while(numberOfNodes > 0)
	{
		linked.AddNode();
		--numberOfNodes;
	}

	linked.DisplayNodeData();

	system("pause");
	return 0;
}


im hoping for your answers.
Last edited on
closed account (o1vk4iN6)
What line?

You can always use the STL 'list';

1
2
3
#include <list>

std::list<int> values;
in line 24 of MyLinkedList.cpp
closed account (o1vk4iN6)
Do you have a constructor that sets "head" to NULL? If you don't initialize it to NULL than how's it being set to NULL? So you are trying to use a bad pointer, which could be anything really.
ohh. thanks.
Topic archived. No new replies allowed.