dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: link list help

  1. #1
    Join Date
    Mar 2008
    Posts
    55

    link list help

    im working on a program that inserts 25 random intergers between 0 to 100 in order in a link list, and that calculates the sum of the elements and finds the average. so far my program prints out my numbers but it sometimes gives me only 2,3 or 5 numbers not 25. can someone tell me why im not getting 25 numbers?
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    class Node;
    
    class List{
    public:
    	List();
    	void setNode( int );
    	bool isEmpty( void );
    	void printList( void );
    
    private:
    	Node *firstPtr;
    	Node *lastPtr;
    };
    
    class Node{
    	friend class List;
    public:
    	Node( int );								
    	int getData( void );
    private:
    	int data;
    	Node *nextPtr;
    };
    
    int main(){
    
    	srand( time( 0 ) );
    
    	List myList;
    
    	for( int i = 0; i <= 25; i++ )
    		myList.setNode( rand() % 100 + 1 );
    	
    
    	myList.printList();
    	cout<<endl;
    
    	return(0);
    }
    
    List::List() : firstPtr( 0 ), lastPtr( 0 )
    {
    	
    }
    
    void List::setNode( int d ){
    
    	Node *newPtr = new Node( d ); //new temp node
    
    	Node *temp = firstPtr;
    	Node *trailTemp = firstPtr;
    
    
    	if( isEmpty() )
    		firstPtr = newPtr;
    
    	else if( newPtr->getData() > temp->getData() )
    	{
    			while( newPtr->getData() > temp->getData() && temp->nextPtr != 0 ){
    		
    				temp = temp->nextPtr;
    				trailTemp->nextPtr = temp;
    			}
    
    			if( temp->nextPtr == 0 ){
    				newPtr->nextPtr = temp->nextPtr;
    				temp->nextPtr = newPtr;
    			} else{
    				newPtr->nextPtr = temp;	
    				trailTemp->nextPtr = newPtr;
    			}
    	
    	} else{
    		newPtr->nextPtr = temp->nextPtr;
    		firstPtr = newPtr;
    	}
    }
    
    bool List::isEmpty() {
    
    	return( firstPtr == 0 );
    }
    
    void List::printList( void ){
    	
    	Node *temp = firstPtr;
    
    	temp = temp->nextPtr;
    
    	while( temp->nextPtr != 0 ){			//traverse the list
    		cout << temp->getData() << " " ;	// print out at each node.
    		temp = temp->nextPtr;
    	}
    
    	cout << temp->getData();
    }
    
    /***************** class Node functions *************************************/
    
    
    Node::Node( int d ) :  data( d ), nextPtr( 0 )
    {
    
    }
    
    
    int Node::getData()
    { 
    	return( data );
    }

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: link list help

    Code:
    	for( int i = 0; i <= 25; i++ )
    		myList.setNode( rand() % 100 + 1 );
    Even if setNode works as expected (apparently it doesn't), this loop will insert 26 items into the list. Also, it is absolutely not okay to allocate memory with new and then not delete it.
    - Alon

  3. #3
    Join Date
    Mar 2008
    Posts
    55

    Re: link list help

    Quote Originally Posted by Hermit
    Code:
    	for( int i = 0; i <= 25; i++ )
    		myList.setNode( rand() % 100 + 1 );
    Even if setNode works as expected (apparently it doesn't), this loop will insert 26 items into the list. Also, it is absolutely not okay to allocate memory with new and then not delete it.
    okay then ill make it 1-25 then. but ive compiled my program a couple of times and it sometimes arranges it in order and sometimes it doesnt why is that. also why doesnt it give me 25 numbers?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: link list help

    I see three problems:

    1)
    Code:
    			while( newPtr->getData() > temp->getData() && temp->nextPtr != 0 ){
    		
    				temp = temp->nextPtr;
    				trailTemp->nextPtr = temp;
    			}
    This does not do what you think it does. I suspect you want trailTemp to point to the node prior to temp, but instead, you're merely reassigning firstPtr->nextPtr to temp, whatever that may be. You never change what trailTemp points to, so it always equals firstPtr.

    2)

    Code:
    else{
    		newPtr->nextPtr = temp->nextPtr;
    		firstPtr = newPtr;
    	}
    In this case, you're *replacing* the first element of the list with newPtr, not prepending it as I suspect you want to. temp itself (which is firstPtr before the second line) will just go away, do you see why?

    3)
    Code:
    void List::printList( void ){
    	
    	Node *temp = firstPtr;
    
    	temp = temp->nextPtr;
    
    	while( temp->nextPtr != 0 ){
    Why are you skipping the first element during printout?

  5. #5
    Join Date
    May 2008
    Posts
    16

    Re: link list help

    you can write the code like the following.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    class Node;
    
    class List{
    public:
    	List();
    	void setNode( int );
    	bool isEmpty( void );
    	void printList( void );
    	
    private:
    	Node *firstPtr;
    	Node *lastPtr;
    };
    
    class Node{
    	friend class List;
    public:
    	Node( int );								
    	int getData( void );
    private:
    	int data;
    	Node *nextPtr;
    };
    
    int main(){
    	
    	srand( time( 0 ) );
    	
    	List myList;
    
    	int m = 0;
    	for( int i = 0; i <= 25; i++ )
    	{
    		myList.setNode(rand()%100 + 1);
    	}
    	
    	myList.printList();
    	cout<<endl;
    	
    	return(0);
    }
    
    List::List() : firstPtr( 0 ), lastPtr( 0 )
    {
    	
    }
    
    void List::setNode( int d ){
    	
    	Node *newPtr = new Node( d ); //new temp node
    	
    	Node *temp = firstPtr;
    	Node *trailTemp = firstPtr;
    	
    	
    	if( isEmpty() )
    	{
    		firstPtr = newPtr;
    		firstPtr->nextPtr = NULL;
    	}
    	
    	else if( newPtr->getData() > temp->getData() )
    	{
    		while( newPtr->getData() > temp->getData() && temp->nextPtr != 0 ){
    
    			trailTemp = temp;
    			temp = temp->nextPtr;
    		
    		}
    		if (newPtr->getData() <= temp->getData())
    		{
    			newPtr->nextPtr = temp;	
    			trailTemp->nextPtr = newPtr;
    		}
    		else
    		{	
    			newPtr->nextPtr = temp->nextPtr;
    			temp->nextPtr = newPtr;
    		}
    	/*	if( temp->nextPtr == 0 ){
    			newPtr->nextPtr = temp->nextPtr;
    			temp->nextPtr = newPtr;
    		} else{
    
    		}*/
    		
    	} else{
    		newPtr->nextPtr = temp;
    		firstPtr = newPtr;
    	}
    }
    
    bool List::isEmpty() {
    	
    	return( firstPtr == 0 );
    }
    
    void List::printList( void ){
    	
    	Node *temp = firstPtr;
    	
    //	temp = temp->nextPtr;
    	
    	while( temp->nextPtr != 0 ){			//traverse the list
    		cout << temp->getData() << " " ;	// print out at each node.
    		temp = temp->nextPtr;
    	}
    	
    	cout << temp->getData();
    }
    
    /***************** class Node functions *************************************/
    
    
    Node::Node( int d ) :  data( d ), nextPtr( 0 )
    {
    	
    }
    
    
    int Node::getData()
    { 
    	return( data );
    }

  6. #6
    Join Date
    May 2008
    Posts
    16

    Re: link list help

    Code:
    	
    	while( newPtr->getData() > temp->getData() && temp->nextPtr != 0 ){
    
    			trailTemp = temp;
    			temp = temp->nextPtr;
    		
    		}
    if you write like this, that's ok.

  7. #7
    Join Date
    Mar 2008
    Posts
    55

    Re: link list help

    Quote Originally Posted by hujing
    you can write the code like the following.
    thanks now i know what i was doing wrong. the adding and finding the average for this program shouldnt be too hard to figure out for me now hopefully.
    Last edited by ptg; May 19th, 2008 at 01:31 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)