I am working on creating a linked list to connect the previous value number (or string) to another value
I asked this question before I am just guessing I did it completely wrong since no one responded. So what would be the best way to start the function.
I need to linked the list of strings together and be able to add a new name and be able to add name either a the end or middle easily
Example
[John]->[Stewart]->[Hanna}
Insert Patrica in second spot
[John]->[Patrica]->[Stewart]->[Hanna]
//Platform: C++ Win32 Console Visual Studio 2010
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <stdlib.h>
#include <crtdbg.h>
#include <iomanip>
#include <windows.h>
#include <string>
using namespace std;
#include "CNode.h"
int main()
{
{
CNode<string>* head = NULL;
CNode<string>* ptr;
string players[8] = {"Becky","Aaron","John","Magret","Susan","Andrea","Morgan Freeman","Carmelo"};
for (int i=0; i<8; i++)
{
string playrs = players[i];
cout<< playrs << endl;
ptr = new CNode<string>(playrs);
ptr->insert(head,ptr);
}
cout << head->ToString()<<endl;
}
_CrtDumpMemoryLeaks();
cout << endl;
system("pause");
return 0;
}
And the h.file is
#pragma once
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
template <typename T>
class CNode
{
private:
CNode<T>* m_pNext;
T m_value;
public:
CNode(T value);
virtual ~CNode();
T& Value();
CNode<T>* getNext();
void setNext(CNode<T>* address);
string ToString();
void insert(CNode<T> head, CNode<T> item);
};
template <typename T>
CNode<T>::CNode(T value)
{
m_value = value;
m_pNext = NULL;
}
template <typename T>
CNode<T>::~CNode()
{
}
template <typename T>
CNode<T>* CNode<T>::getNext()
{
return m_pNext;
}
template <typename T>
void CNode<T>::setNext(CNode* address)
{
m_pNext = address;
}
template <typename T>
T& CNode<T>::Value()
{
return m_value;
}
template <typename T>
string CNode<T>::ToString()
{
stringstream result;
CNode<T>* ptr = this;
while (ptr != NULL)
{
result << "[" << ptr->Value() << "]";
ptr = ptr->getNext();
if (ptr != NULL)
{
result << "->";
}
}
return result.str();
}
template <typename T>
void CNode<T>::insert(CNode<T> head, CNode<T> item)
{
\\Dont fully understand what to type here...
}
Any suggestion or even links to pages that talk about linked list will be very helpful
Thanks