Thread: My Linked List

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    My Linked List

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
      }
    }
    My best code is written with the delete key.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    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

    What about the C++ stylistic issues? I'd like to hear about them too.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM