• 01-15-2004
nickname_changed
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 :)
• 01-15-2004
Prelude
C++ stylistic issues aside, my only problem is with your destructor. You traverse the list twice (forward, then back) for an operation that should only need to traverse it once:
Code:

```~LinkedList() {   Node *curr;   Node *save;   for ( curr = Topnode; curr != NULL; curr = save ) {     save = curr->Next;     delete curr;   } }```
• 01-15-2004
nickname_changed
Quote:

Originally posted by Prelude
C++ stylistic issues aside, my only problem is with your destructor. You traverse the list twice (forward, then back) for an operation that should only need to traverse it once:
Code:

```~LinkedList() {   Node *curr;   Node *save;   for ( curr = Topnode; curr != NULL; curr = save ) {     save = curr->Next;     delete curr;   } }```

Hehehe.... hey your right, that is kinda silly of me. Thanks :D

What about the C++ stylistic issues? I'd like to hear about them too.
• 01-15-2004
ripper079
A nice workable linked list indeed.

Although there is nothing that says what is the "correct" linked list, but some form of linked listed should support some basic operations that are fundametal for linked list like

• Removal/deletion of a node/s
• Insertion of a node
• Size of the list
• List empty

Try to implement some more operations to your linked list so it is more "useable". You could also seperate the interface and the implementation in a header och a source file.
P.S. When you got a "complete" linked list why not try to template it. Data structures are very generic.