Haven't been able to figure out how to make a doubly linked list from this code using m_pPrev (for previous), any suggestions would be appreciated. I have a sinus infection and can't think for the past two days!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
class Node
{
public:
Node ( void ) { m_pNext=0; m_pContent=0;};
~Node ( void ) { };
void SetNext ( Node *a_pn) {m_pNext = a_pn; };
void SetContent (void *a_pv) {m_pContent = a_pv;};
const Node* GetNext (void) {return m_pNext;};
const void* GetContent (void) {return m_pContent; };
private:
Node *m_pNext;
void *m_pContent;
};
class List
{
public:
List (void) {m_pHead = 0; m_pLast = 0; m_pCache = 0;};
~List (void) { };
void Append (void *a_pv)
{
Node *pn = new Node;
pn->SetContent (a_pv);
if (0==m_pHead && 0 == m_pLast )
{
m_pHead = m_pLast = pn;
}
else if (m_pHead == m_pLast)
{
m_pHead->SetNext (pn);
m_pLast = pn;
}
else
{
m_pLast->SetNext (pn);
m_pLast = pn;
}
};
const void* GetHead (void)
{
const void *pv=0;
m_pCache = m_pHead;
if (m_pCache !=0)
{
pv=m_pCache->GetContent();
}
return pv;
};
const void* GetNext (void)
{
const void *pv=0;
if (m_pCache !=0)
{
m_pCache = (Node*)m_pCache->GetNext();
}
if (m_pCache !=0)
{
pv=m_pCache->GetContent();
}
return (pv);
};
private:
Node *m_pHead;
Node *m_pLast;
Node *m_pCache;
};
class Person
{
public:
Person () { };
~Person () { };
string m_sFirstName;
string m_sLastName;
};
void main (int argc, char *argv[] )
{
if (argc <2)
{
cout << "no input data" << endl;
return;
}
List list;
for (int i = 1; i < argc; i++)
{
Person *p=new Person;
p->m_sFirstName = argv [i];
list.Append (p);
}
cout << "you entered" << (i-1) << "names - dumping list" << endl;
Person *pi = (Person*)list.GetHead();
int indx=0;
while (pi !=0)
{
cout << "Person("<<indx++ <<") FirstName("<<pi- >m_sFirstName <<")" << endl;
pi = (Person*)list.GetNext();
}
}