Linked List

I have been given an assignment to add to an already given program. I have been told to add the following: "isEmptyList, print, destroyList, deleteNode and insertNode. What I was given is below. I have been adding to the program but I come up with errors. I am not sure where or what and how to add these functions. Please help.

createNodeExample.txt
// createExam.cpp : main project file.
#include "stdafx.h"
#include <cassert>
#include <cstdlib>
#include <iostream>
using namespace std;
using namespace System;
//R. C. Dawkins
// September 26, 2014
// Example of Creating a linked list of numbers until -99 is entered.
class nodeType
{
private:
int info;
nodeType *link;
public:
void create()
{
nodeType *headPtr, *tailPtr, *newNodePtr;
headPtr = NULL;
tailPtr = NULL;
int num;
cout << "Enter a list of integers. To end enter -99" << endl << "number:";
cin >> num;
headPtr = NULL;
tailPtr = NULL;
while(num != -99)
{
newNodePtr = new nodeType;
assert(newNodePtr != NULL); // Checks to see if memory was allocated
newNodePtr->info = num;
newNodePtr->link = NULL;
if( headPtr == NULL)
{
headPtr = newNodePtr;
tailPtr = newNodePtr;
}
else
{
tailPtr->link = newNodePtr;
tailPtr = newNodePtr;
}
cout << "Enter Number: ";
cin >> num;
}
}
};
int main()
{
cout << "Example of creating a linked list" << endl;
nodeType myNode;
myNode.create();
system("pause");
return 0;
}
Last edited on
I have been adding to the program but I come up with errors.
A couple of things.

When posting code, please use the code format tags otherwise we can't read your code.

When asking help with a compiler/linker error, please post the error verbatim, don't edit, summarise, paraphrase, ... the error is always specific.

I've taken a look at your code and it's pretty obvious what's wrong, just read the error.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// createExam.cpp : main project file.
//#include "stdafx.h"
#include <cassert>
#include <cstdlib>
#include <iostream>

using namespace std;
using namespace System; // error C2871: 'System' : a namespace with this name does not exist

//R. C. Dawkins
// September 26, 2014
// Example of Creating a linked list of numbers until -99 is entered.
class nodeType
{
private:
	int info;
	nodeType *link;

public:
	void create()
	{
		nodeType *headPtr, *tailPtr, *newNodePtr;
		headPtr = NULL;
		tailPtr = NULL;

		int num;
		cout << "Enter a list of integers. To end enter -99" << endl << "number:";
		cin >> num;

		headPtr = NULL;
		tailPtr = NULL;

		while(num != -99)
		{
			newNodePtr = new nodeType;
			assert(newNodePtr != NULL); // Checks to see if memory was allocated
			newNodePtr->info = num;
			newNodePtr->link = NULL;

			if( headPtr == NULL)
			{
				headPtr = newNodePtr;
				tailPtr = newNodePtr;
			}
			else
			{
				tailPtr->link = newNodePtr;
				tailPtr = newNodePtr;
			}
			cout << "Enter Number: ";
			cin >> num;
		}
	}
};

int main()
{
	cout << "Example of creating a linked list" << endl;
	nodeType myNode;
	myNode.create();
	system("pause");
	return 0;
}

Solution? Remove that line, you shouldn't be using namespace, it's bad practice and isn't normally used in production code. std is the C++ standard library namespace and System is the .Net namespace, which you won't be using at all, so shouldn't be there.

I have been told to add the following: isEmptyList, print, destroyList, deleteNode and insertNode. I am not sure where or what and how to add these functions. Please help.

The functions are members of the class, so they're added as show, but you need to implement them yourself.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// createExam.cpp : main project file.
//#include "stdafx.h"
#include <cassert>
#include <cstdlib>
#include <iostream>

using namespace std;

//R. C. Dawkins
// September 26, 2014
// Example of Creating a linked list of numbers until -99 is entered.
class nodeType
{
private:
	int info;
	nodeType *link;

public:
	void create()
	{
		nodeType *headPtr, *tailPtr, *newNodePtr;
		headPtr = NULL;
		tailPtr = NULL;

		int num;
		cout << "Enter a list of integers. To end enter -99" << endl << "number:";
		cin >> num;

		headPtr = NULL;
		tailPtr = NULL;

		while(num != -99)
		{
			newNodePtr = new nodeType;
			assert(newNodePtr != NULL); // Checks to see if memory was allocated
			newNodePtr->info = num;
			newNodePtr->link = NULL;

			if( headPtr == NULL)
			{
				headPtr = newNodePtr;
				tailPtr = newNodePtr;
			}
			else
			{
				tailPtr->link = newNodePtr;
				tailPtr = newNodePtr;
			}
			cout << "Enter Number: ";
			cin >> num;
		}
	}

	bool isEmptyList() const
	{
		return false;
	}

	void print()
	{
	}

	void destroyList()
	{
	}

private:
	void deleteNode(nodeType *node)
	{
	}

	void insertNode(nodeType *node)
	{
	}
};

int main()
{
	cout << "Example of creating a linked list" << endl;
	nodeType myNode;
	myNode.create();
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.