6 Replies - 661 Views - Last Post: 03 April 2013 - 11:28 AM Rate Topic: -----

#1 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Help! Link Lists

Posted 02 April 2013 - 07:51 PM

Ok so my program has been taken over by little creatures that are doing strange things to it! Heres the deal. These are just a couple functions in a pretty huge menu driven program(homework huge, not real life huge), but these two work directly together so im only posting these two. So if I run my program, add in nodes, then print our the values in the nodes( for example add 7, 8, 6) to have the value sorted the press 1 to have them sorted in order it prints out 116->7->8->6->NULL, then if I have it run the sort program again (in the same build) and have it put them in reverse I get this 116 -> 8 -> 7 -> 32767 -> 6 -> NULL. I cant figure out whats going on, its driving me up the wall haha thanks in advance for the help!

////////// Sorts the numbers in either order ///////////
template <class Object>
void cs20::List<Object>::sort( const Object& data ){
    ListNode<Object>* nextNum;
    int i = 0, a[nOneCount], sortCount, answer = 0, num = 0;
    nextNum = head->getNext();
    sortCount = countByO1();
    num = countByO1();
    
    checker = false;
    while( nextNum != NULL ){
        
        a[i] = getLargest();
        remove( a[i] );
        i++;
        nextNum = nextNum->getNext();
    }
   
   //makeEmpty();
   std::cout << "Press (1) to print from smallest to largest" << std::endl;
   std::cout << "Press (2) to print from largest to smallest" << std::endl;
   std::cin >> answer;
    
//////To Print in Order//////
   if( answer == 1 ){
        i = 0;
        while(  i < sortCount ){
            insert(a[i]);
            i++;
       }
    }
 //////To Print in Reverse//////
    else if( answer == 2 ){
    i = 0;
        while(  i < sortCount ){
            insert(a[num-1]);
            num--;
            i++;
        }
    }
    else
        std::cout << "Invalid Option" << std::endl;
    
    isReady = true;
    checker = false;
}




    
////////////Gets the largest number in the list///////////////
template<class Object>
int cs20::List<Object>::getLargest(){
    ListNode<Object>* isLargest;
    isLargest = head->getNext();
    maxNum = 0;

        while( isLargest != NULL ){
        
            if (isLargest->getElement() >= maxNum )
                maxNum = isLargest->getElement();
            
            isLargest = isLargest->getNext();
            
            if( checker == true ){
                if( isLargest == NULL )
                    std::cout<< "The largest number is: " << maxNum << std::endl;
            }
        }
    if( checker == true )
        if( head->getNext() == NULL )
            std::cout << "There are no nodes in this list" << std::endl;
    
    return maxNum;
}



//remove
template <class Object>
void cs20::List<Object>::remove( const Object& data ) {
	ListIterator<Object> iter = findPrevious( data );
	if (iter.isValid()) {
		ListNode<Object>* node = findPrevious( data ).current;
		if (node->getNext() != NULL) {
			ListNode<Object> *oldNode = node->getNext();
			node->setNext( node->getNext()->getNext() );  // Skip oldNode
			delete oldNode;
		}
	}
    nOneCount -= 1;
}

//insert
template <class Object>
void cs20::List<Object>::insert( const Object& data ) {
	// insert after the header node
	ListNode<Object>* newnode = new ListNode<Object>( data, head->getNext() );
	head->setNext( newnode );
  
    if(findLastNode == true){
     lastNodeAddress = newnode;
        findLastNode = false;
     }
    nOneCount += 1;
}
//count

template <class Object>
int cs20::List <Object>::countByO1() const{
    return nOneCount;
}

This post has been edited by jimblumberg: 03 April 2013 - 11:18 AM
Reason for edit:: Fixed Code Tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Help! Link Lists

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4148
  • View blog
  • Posts: 12,934
  • Joined: 25-December 09

Re: Help! Link Lists

Posted 02 April 2013 - 09:27 PM

I suggest you post the smallest possible complete program that illustrates your problem. Then we can compile and run the program to see the problem ourselves.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Help! Link Lists

Posted 02 April 2013 - 11:30 PM

hey jim thanks for your help again,

is there no way to see from what i am showing you? I only ask because im not sure how easy it will be to post this working program, It has 9 files that all work with one another, 2 switch cases and a ton of methods. its kind of complicated thanks again
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4148
  • View blog
  • Posts: 12,934
  • Joined: 25-December 09

Re: Help! Link Lists

Posted 03 April 2013 - 06:26 AM

That is why I said the "smallest possible complete program". I don't necessarily want to see the whole program, just enough so the program compiles and calls these functions and demonstrates the problem.

A couple of things you can do would be to run the program thru your debugger and single step thru these functions, stepping into any functions that are called and viewing the variables as you step.

The problem with the code you posted is it doesn't contain all the functions that are called by these two functions. For example what does countByO1() do?

Why are you "removing" things in your sort routine? The same with "inserting". What are your remove() and insert() functions actually doing?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7892
  • View blog
  • Posts: 13,419
  • Joined: 19-March 11

Re: Help! Link Lists

Posted 03 April 2013 - 06:38 AM

Also, please post exactly the sequence that led to the results you're seeing. Did you insert the value 116? You don't mention it in your post. I can imagine ways you'd get 32767 accidentally, but 116 is harder to grasp.
Was This Post Helpful? 1
  • +
  • -

#6 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Help! Link Lists

Posted 03 April 2013 - 10:27 AM

Ok I updated it with all the methods. Jon, in the paragraph at the top I gave the example 7->8->6. meaning 6 was inserted first.

Jim,

This is the idea behind the code.
in my while loop I find the largest number, insert that value into an array, then remove that node. I repeat that process until I have 0 nodes. Then, I insert the values in the array into new nodes that are now sorted in order. I would also like to mention, this code used to work. but then I discovered that my "getLargest()" function was semi wrong. At the time getLargest() would tell you that if you had 0 nodes in the list it would say you have a node with the value of 0. now it doesnt do that. but in fixing it it messed up my sort program.

Also, countBy01() just gives you the number of nodes in the list. Ive tested it several times and it is working. I just keeps track as it someone inserts or removes a node then returns that number when called. I did that in the hopes of making the program more efficient.

thanks again
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4148
  • View blog
  • Posts: 12,934
  • Joined: 25-December 09

Re: Help! Link Lists

Posted 03 April 2013 - 11:28 AM

Why are you removing the nodes?

Can't you just "move" the pointers?

Removing and reinserting the nodes seems very inefficient to me.

Also if you just stuff an array with your list, how do you know the size of your array at compile time? I don't see any dynamic memory allocation, and if your variable nOneCount is not a compile time constant you should get an error because VLA's are not allowed in C++.

Please show your class definitions.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1