Thread: linked list problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    linked list problem

    Hello this is my first post.
    I am making a linked list. The problem is that whenever I try to retrieve a value I get the value from the first node.

    LinkedList.cpp
    Code:
    #include <iostream>
    
    using namespace std;
    /*constructor*/
    List::List():head(NULL),length(0)//sets first heads pointer to null
    {
        //remeber that head is not a node and does not contain any data
    	//it simply points to the current first node
    	//thus after the constructor is called, there trully still is no link list
    }
        
    /*destructor*/
    List::~List()
    {
        eraseAll();
    }
    
    void List::eraseAll()
    {
        while(head != NULL)
        {
            NodePtr temp = head;
            head = head -> next;//the node pointed to by head, the value of next (which is an adress)
            delete temp;
        }//heads pointer is now null
    }
    
    bool List::isEmpty() const
    {
        return head == NULL;//if head is null then the link list was just made, or deleteAll was just called or the list was lost (memorey leek)
    }
    
    unsigned int List::size() const
    {
        int count = 0;//this means the first element (list) is at 0
        NodePtr toEnd = head;
        while(toEnd->next != NULL)
        {
            toEnd = toEnd->next;
            count++;
        }
        return count;
    }
    /*returns true upon sucess, otherwise false*/
    bool List::insert(DataType item, unsigned int position)
    {
    	if(position > length)
    		return false;//couldn't be inserted (would cause empty spaces in list)
    	if(position == 0 && head == NULL)
    	{
            cout<<"inisde insert"<<endl;
    		head = new Node(item);
            cout<<"this is head "<<head<<endl;
    		return true;
    	}
        int count = 0;
        NodePtr temp = head;
        NodePtr previous;
    	while(count != position)
    	{
    		previous = temp;
    		temp = temp->next;
    		count++;
    	}//the new node should now be inserted after previous, and be followed by temp
    	previous = new Node(item);
    	previous->next->next = temp;
    	length++;
        return true;
    }
    
    DataType List::get(unsigned int position) const
    {
        NodePtr temp = head;
        if(head->next == NULL)//head's next is always null for some reason
            cout<<"head's screwed up"<<endl;
        unsigned int count = 0;
        cout<<"position: "<<position<<endl;
        while(count < position && temp->next != NULL)
        {
            count++;
            temp = temp->next;
        }
    
        cout<<"element from temp "<<temp->element<<endl;
        return temp->element;//problem occures here
    }
    this is the file I used to test it

    tester.cpp
    Code:
    #include "LinkedList.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        List aList = List();
        aList.insert(4.2, 0);
        aList.insert(22, 1);
        aList.insert(5,3);
        cout<<aList.get(3)<<endl;
        
        return 0;
    }
    for some reason head is always pointing to null. This confuses me because the only place I assign a variable the value of NULL is in the constructor.
    actually I miss-spoke. Head is not pointing to null the first time I call the insert method, but it is pointing to null whenever I call the get method.

    here it is again slimmed down:
    Code:
    #include <iostream>
    
    using namespace std;
    
    List::List():head(NULL),length(0)
    {
    
    }
    
    
    /*returns true upon sucess, otherwise false*/
    bool List::insert(DataType item, unsigned int position)
    {
    	if(position > length)
    		return false;
    	if(position == 0 && head == NULL)//it appears head is always true
    	{
    		head = new Node(item);
    		return true;
    	}
        int count = 0;
        NodePtr temp = head;
        NodePtr previous;
    	while(count != position)
    	{
    		previous = temp;
    		temp = temp->next;
    		count++;
    	}
    	previous = new Node(item);
    	previous->next->next = temp;
    	length++;
        return true;
    }
    
    DataType List::get(unsigned int position) const
    {
        NodePtr temp = head;
        unsigned int count = 0;
        while(count < position && temp->next != NULL)//this condition is never true
        {
            count++;
            temp = temp->next;
        }
        return temp->element;//problem occures here
    }
    tester.cpp
    Code:
    #include "LinkedList.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        List aList = List();
    	aList.insert(4.5, 0);
        aList.insert(22, 1);
        aList.insert(5,3);
        cout<<aList.get(3)<<endl;
        
        return 0;
    }
    LinkedList.h
    Code:
    #include <cstdlib>
    
    typedef double DataType;
    
    // our list is zero based, that is, the first element in the list is in position 0.
    
    class List
    {
        public:
            List();
            List(const List & rhs);
            List& operator=(const List & rhs);
            bool insert (DataType item, unsigned int position);
            DataType get(unsigned int position) const;
    #ifndef NDEBUG
    #endif
        private:
            struct Node
            {
                DataType element;
                Node* next;
                Node(DataType item):element(item),next(NULL){}
            };
            typedef List::Node * NodePtr;
    
            NodePtr head;//private field
    		unsigned int length;
    };
    #include "LinkedList.cpp"
    Last edited by help_seed; 11-23-2009 at 06:44 PM. Reason: added last line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM