Sorted Link List ADT

Mar 3, 2017 at 8:11pm
No compiler errors, but my retrieve method is not displaying anything in my list.

SortLabList.h
1
2
3
4
5
bool insert(SortListItemType& newItem);

	bool remove(int position);

	bool retrieve(int position, SortListItemType& dataItem) const;


SortLabListP.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

bool SortListClass::retrieve(int position, SortListItemType& dataItem) const
{
	bool success = bool( (position >= 1) && (position <= size) );
	success = true;
	if (success)
	{
		// get pointer to node, then data in node
		SortListNode *cur = ptrTo(position);

		dataItem = cur->item;
	}

	return(success);
}

bool SortListClass::insert(SortListItemType& newItem) {
	
	SortListNode *prev = NULL;   SortListNode *cur = head;




	while ((cur != NULL) && (newItem > cur->item)) 
	{ 
		prev = cur;    
		cur = cur->next; 
		
	}

	SortListNode *newPtr = new SortListNode;  newPtr->item = newItem;

	newPtr->next = cur;

	if (prev == NULL)   head = newPtr;   else   prev->next = newPtr;
	return(0);
}


Mar 4, 2017 at 3:32am
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool SortListClass::retrieve(int position, SortListItemType& dataItem) const
{
	bool success = bool( (position >= 1) && (position <= size) );
	success = true;
	if (success)
	{
		// get pointer to node, then data in node
		SortListNode *cur = ptrTo(position);

		dataItem = cur->item;
	}

	return(success);
}

is logically equivalent to:
1
2
3
4
5
6
7
8
9
bool SortListClass::retrieve(int position, SortListItemType& dataItem) const
{
    // get pointer to node, then data in node
    SortListNode *cur = ptrTo(position);

    dataItem = cur->item;

    return true;
}


although it may not have any bearing on your issue. I don't see anything else that really sticks out in your code (other than that is incomplete.) retrieve doesn't display anything, so some clarification may be in order.

Topic archived. No new replies allowed.