Adding a "Node" instance to a linked list

Hello! I am very new to C++. I'm stuck on a problem and am in need of assistance; so help is greatly appreciated.
I want to add another node instance to the code I have, call it "gh"
or whatever.
Here is what I have so far:

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
 #include "Node.h"

#include <iostream>

#include <string>

using namespace std;


int main()

{

	static int  itemCount = 0;

	Node<string>* headPtr=nullptr;



	// Add to beginning of chain: new node references rest of chain;

	// (headPtr is null if chain is empty)        

	Node<string>* nextNodePtr = new Node<string>();

	nextNodePtr->setItem("IJ");

	nextNodePtr->setNext(headPtr);  // New node points to chain

    //Node<ItemType>* nextNodePtr = new Node<ItemType>(newEntry, headPtr); // alternate code


	headPtr = nextNodePtr;          // New node is now first node

	itemCount++;


	cout << headPtr->getItem()<<endl;


	system("pause");

	return 0;


}

And the Header file:
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
/** @file Node.h
Listing 4-1 */
#ifndef _NODE
#define _NODE

template<class ItemType>
class Node
{
private:
	ItemType        item; // A data item
	Node<ItemType>* next; // Pointer to next node

public:
	Node();
	Node(const ItemType& anItem);
	Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
	void setItem(const ItemType& anItem);
	void setNext(Node<ItemType>* nextNodePtr);
	ItemType getItem() const;
	Node<ItemType>* getNext() const;
}; // end Node

//#include "Node.cpp"
#include "Node.h"
#include <cstddef>

template<class ItemType>
Node<ItemType>::Node() : next(nullptr)
{
} // end default constructor

template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor

template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :
item(anItem), next(nextNodePtr)
{
} // end constructor

template<class ItemType>
void Node<ItemType>::setItem(const ItemType& anItem)
{
	item = anItem;
} // end setItem

template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)
{
	next = nextNodePtr;
} // end setNext

template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
	return item;
} // end getItem

template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
	return next;
} // end getNext

#endif
It could make sense to put the "add node" into a function.

For example:
1
2
3
4
5
template<typename T>
void prepend( Node<T>* & head, const T & value )
{
  head = new Node<T>( value, head );
}

(However, smart pointers ...)

Then:
1
2
3
4
5
6
7
8
9
10
11
int main() {
  Node<string>* headPtr {nullptr};
  prepend( headPtr, "IJ" );
  prepend( headPtr, "GH" );
  append( headPtr, "KL" );
  prepend( headPtr, "EF" );
  prepend( headPtr, "CD" );

  print( headPtr );
  demolish( headPtr );
}
Last edited on
That's a start. But I'm afraid I need a bit more than that. I'm too much of a noob ))
I added the first piece of code to the Header file, and then the second to my main.cpp but am getting a bunch of errors...
What library must I include to use append, prepend and demolish?
You have to write them. If prepend adds a node to the beginning of the list, what would append do?

What might print do?

The most important is the demolish function.
You have dynamically allocated memory for nodes during the program.
Therefore, you have to deallocate that memory too.
Prepend adds to the beginning, append adds to the end of linked list?
And print displays output. But I still can't write something I have never even seen before. Can't even find a proper tutorial on this.
You can skip the append(), unless your teacher requests it.

There must be a lot of material about linked lists.

Your code had: cout << headPtr->getItem()<<endl;
How would that look in a function?

Perhaps:
1
2
3
4
5
template<typename T>
void print( const Node<T>* node )
{
  std::cout << node->getItem() << '\n';
}

Do you see a danger in that? What if I call print( nullptr );?
How to cope with that?


Furthermore, the function prints (at most) one node's value. You want whole list.
Iteration, loop, recursion.

Lets look at a loop:
1
2
3
4
5
6
std::list<Bar> foo;
auto gaz = foo.begin();
while ( gaz != foo.end() ) {
  // use gaz
  ++gaz;
}

The gaz is this is an iterator. Probably a sugar-coated pointer.
It refers to an element of foo. The ++gaz updates the gaz to refer to the next element.
If gaz is at the last element, then ++gaz makes it equal the foo.end().

If you have a pointer to node of your list, how would you get to the next node?
How could you see whether you point one past the last node?
(You already have code that holds the answers. Now you have to see them too.)
Topic archived. No new replies allowed.