In an attempt to get a better understanding of pointers, I have created my own linked list code. I would appreciate it if you could look over it and tell me if I have made any errors (maily with pointers), so that I may learn more.
Code:
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
//=============================================
// LinkedList Node
//=============================================
class Node
{
public:
Node(Node * PreviousNode, int Value)
{
Data = Value;
Next = NULL;
Prev = PreviousNode;
}
int Data;
Node * Next;
Node * Prev;
};
//=============================================
// LinkedList class
//=============================================
class LinkedList
{
public:
LinkedList(int NewVal)
{
Topnode = new Node(NULL, NewVal);
Length = 1;
}
~LinkedList()
{
Node * CurrNode = Topnode;
while (CurrNode != NULL)
{
CurrNode = CurrNode->Next;
}
while (CurrNode != NULL)
{
CurrNode = CurrNode->Prev;
delete CurrNode->Next;
}
}
bool PrintStructure();
bool Insert(int NewValue);
bool Find(int SearchKey);
Node * Topnode;
unsigned long Length;
};
bool LinkedList::Insert(int NewValue)
{
Node * CurrNode = Topnode;
while (CurrNode->Next != NULL)
{
CurrNode = CurrNode->Next;
}
CurrNode->Next = new Node(CurrNode, NewValue);
if (CurrNode->Next == NULL)
{
return false;
}
Length++;
return true;
}
bool LinkedList::PrintStructure()
{
cout << Topnode->Data;
Node * CurrNode = Topnode->Next;
while (CurrNode != NULL)
{
cout << ", " << CurrNode->Data;
CurrNode = CurrNode->Next;
}
return true;
}
//===========================================
// Main
//===========================================
int main()
{
LinkedList MyList(7);
MyList.Insert(15);
MyList.PrintStructure();
return 0;
}
Thankyou for any comments, suggestions or "Hey, thats naughty!"s anyone has to offer