Thread: Strange errors in linked list program

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Angry Strange errors in linked list program

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    class List
    {
    private:
    	int size;
    	struct Node {
    		int value;
    		struct Node * next;};
    	typedef struct Node * nodePtr;
    	nodePtr startPtr;
    public:
    	List();
    	~List();
    	void addNode(int , int);
    	void insert(int, nodePtr);
    	int getInfo(int);
    	nodePtr retrieve(int);
    };
    
    
    List::List()
    {
    	startPtr = NULL;
    	size = 0;
    }
    
    void List::addNode(int info, int pos)
    {
    	nodePtr p;
    	p = new Node;
    
    	insert(pos, p);
    }
    
    void List::insert(int pos, nodePtr newPtr)
    {
    	nodePtr nextPtr;
    
    	if(pos<0 && pos>size+1)
    	{
    		cout << "out of range" << endl;
    		exit(1);
    	}
    
    
    	if(this->startPtr == NULL)
    	{
    		startPtr = newPtr;
    	}
    	else
    	{
    		if(pos==0)
    		{
    			newPtr->next = startPtr;
    			startPtr = newPtr;
    		}
    		else
    		{
    			nextPtr = retrieve(pos);
    			if(nextPtr!=NULL)
    			{
    				newPtr->next = nextPtr->next;
    				nextPtr = newPtr;
    			}
    		}
    		size++;
    	}
    }
    	
    nodePtr List::retrieve(int pos)
    {
    	nodePtr ptr = startPtr;
    
    	if(startPtr != NULL)
    	{
    		for(int i=0; ptr != NULL, i!=pos; i++)
    		{
    			ptr = ptr->next;
    		}
    	}
    
    	return ptr;
    }
    
    int main(void)
    {
    	return 0;
    }
    oookeeey, what the hell is going on?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The error messages would have been nice. Compiles for me if I change:
    Code:
    nodePtr List::retrieve(int pos)
    to
    Code:
    List::nodePtr List::retrieve(int pos)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    aah, i should've thought of that !

    Thank you so much JaWiB
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Did you manage to find what you was looking for regarding, doubly linked lists?

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Oh. Brain Cell is active again... !?!. Welcome...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. Linked list generates errors..
    By Nutshell in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 05:19 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM