Need help abstracting my linked list.

This is a discussion on Need help abstracting my linked list. within the C++ Programming forums, part of the General Programming Boards category; Okay, so here is my current code: I don't want the Linked_List class to be an actual link in the ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Need help abstracting my linked list.

    Okay, so here is my current code:
    I don't want the Linked_List class to be an actual link in the linked list. Why should the links have functions to add more links?
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    template< typename T_ >
    class Linked_List
    {
    public:
    
    	Linked_List<T_>()
    	{
    		start_ptr = NULL;
    		current = NULL;
    	}
    
    	T_ data;
        Linked_List<T_> * nxt;// Pointer to next node
    
    	void add_node(T_ & t)
    	{
    		Linked_List<T_> *temp, *temp2;   // Temporary pointers
    		// Reserve space for new node and fill it with data
    		temp = new Linked_List<T_>;
    		temp->data = t;
    		temp->nxt = NULL;
    		// Set up link to this node
    		if (start_ptr == NULL)
    		{
    			start_ptr = temp;
    			current = start_ptr;
    		}
    		else
    		{
    			temp2 = start_ptr;
    			// We know this is not NULL - list not empty!
    		    while (temp2->nxt != NULL)
    			{
    				temp2 = temp2->nxt;
                    // Move to next link in chain
                }
    			temp2->nxt = temp;
    		}
    	}
    
    	void display_list()
    	{
    		Linked_List<T_> *temp;
    		temp = start_ptr;
    		cout << endl;
    		if (temp == NULL)
    			cout << "The list is empty!" << endl;
    		else
    		{
    			while (temp != NULL)
    			{
    				// Display details for what temp points to
    				cout << temp->data;
    				if (temp == current)
    					cout << " <-- Current node";
    				cout << endl;
    				temp = temp->nxt;
    			}
    		}
    	}
    
    	void delete_start_node()
    	{
    		Linked_List<T_> *temp;
    		temp = start_ptr;
    		start_ptr = start_ptr->nxt;
    		delete temp;
    	}
    
    	void delete_end_node()
    	{ 
    		Linked_List<T_>  *temp1, *temp2;
    		if (start_ptr == NULL)
    			cout << "The list is empty!" << endl;
    		else
    		{ 
    			temp1 = start_ptr;
    			if (temp1->nxt == NULL)
    			{
    				delete temp1;
    				start_ptr = NULL;
                }
    			else
    			{
    				while (temp1->nxt != NULL)
    				{
    					temp2 = temp1;
    					temp1 = temp1->nxt;
                    }
    				delete temp1;
                    temp2->nxt = NULL;
    			}
    		}
    	}
    	 
    	void move_current_on ()
    	{
    		if (current->nxt == NULL)
    			current = start_ptr;
    		else
    			current = current->nxt;
        }
    
    	void move_current_back ()
    	{ 
    		if (current == start_ptr)
    			cout << "You are at the start of the list" << endl;
    		else
    		{ 
    			 node *previous;     // Declare the pointer
    			 previous = start_ptr;
    			 while (previous->nxt != current)
    			 {
    				 previous = previous->nxt;
    			 }
    			 current = previous;
    		 }
    	 }
    
    	 Linked_List<T_> * start_ptr;
    	 Linked_List<T_> *current;		 // Used to move along the list
    };
    Here is a picture telling you what I want to do with it:

    http://img.photobucket.com/albums/v1...ewhatimean.jpg

    Basically, I want to make the Linked_List class more or less just a controller and entry point of the actual linked list.

    I want to have an abstract node class that I can add functions to and derive those functions into subclasses.

    By doing this I hope to accomplish a system where I can automatically and recursively update the data held in the links. Which should all be derivations of type link.

    Does this make sense?
    Last edited by Shamino; 12-28-2007 at 02:04 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    template< typename T_ >
    struct listnode
    {
      T_ data;
      listnode< T_ >* next;
    };
    
    template< typename T_ >
    class linkedlist
    {
      // ...
    };

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I can see cleeearly now the rain is gonee... Thanks :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

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, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11: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, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21