runtime determines bool eval (Single linked list)

Depending on the runtime, the bool func will eval true or false. I did the debugging and sure enough it happens. I am wondering if there is an issue with my if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 bool  List<Object>::isDecreasing() const
{
	bool isd = false;

	ListNode<Object>* front = head;
	ListNode<Object>* nodeafter = head->getNext();
	

	/*if list is empty true*/
	if (head->nextIsNull()) 
		isd = true;
	
		//checks if sorted. returns true even if a new value is sorted.
	if (front > nodeafter)			
		isd = true;	
	if (front < nodeafter)
		isd = false;

	return isd;


Created list

1
2
3
4
5
		int one = 1, two = 2, three = 3, four = 4, five = 5;
	List<int> l1;
	List<int> l2;
	l1.insert(one);
	l1.insert(two);	


Test case that fails w/ created list
1
2
3
		l1.insert_back(three);
		assert(stringList(l1) == "2 -> 1 -> 3 -> NULLPTR");
		assert(!l1.isEmpty());


Test case that passes with created list
1
2
3
4
		l2.insert_back(three);
		assert(stringList(l2) == "3 -> NULLPTR");
		assert(!l2.isEmpty());
		break;
Line 14 and 16 compare the memory addresses (pointers) of the nodes. Probably not what you wanted.
Last edited on
not at all. I do see what you are saying. I'm running visual studio and it does indeed show that it's comparing addresses.

could the insert back method an added issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//tail( insert back) method
template <class Object>
void List<Object>::insert_back(const Object& data) {

	Object a;
	//allows multiple values to be entered. keeps pointing to next node
	if (tail->getNext() == nullptr)
	{
		ListNode<Object>* wnode = new ListNode<Object>(data, nullptr);
		tail->setNext(wnode);
		tail = wnode;		
	}
	//allows data to be inserted when list is empty, otherwise nothing is inserted
	if (head->getNext() == nullptr)
	{
		ListNode<Object>* anode = new ListNode<Object>(data, head->getNext());
		head->setNext(anode);
		a = data;
	}
	
}
Topic archived. No new replies allowed.