Linked List Delete

Hello,

I have a linked list that has delete and retrieve functions. The retrieve searches the linked list by index and returns the value contained in the node. The delete function has a value parameter that is passed in from the client. It must use the retrieve function to search the linked list to see if the value is found before deleting it. I am stuck on how to delete the node based on the returned value. I cannot return a pointer. I have two class instance variables....head and tail. I am thinking I need to use those in some way to get the linked list node where the value was found back to the delete function, but I am not sure. Don't they need to always indicate the first and last nodes?

My code is included below. I am not getting an error, but the client is simply not deleting the node. If anyone can point me in a general direction on what I need to do here, it is much appreciated.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

template< typename NODE >
NODE List< NODE >::removeNode( NODE &value )
{
	ListNode< NODE > *cPtr = head;
	ListNode< NODE > *pPtr = 0;
	ListNode< NODE > *tempheadPtr = tail;
	NODE returnValue;
	
	returnValue = retrieve(value);  //This is where my problem is occurring

	if (cPtr == head)
	{
		head = cPtr->nextPtr;
	}	
	else
	{	
		pPtr->nextPtr=cPtr->nextPtr;
	}

	if (cPtr == tail)
	{
		tail = pPtr;
	}
		
	returnValue = cPtr->data;
	delete cPtr;
	return returnValue;
} 


template<typename NODE>
NODE List< NODE >::retrieve( NODE &value )
{
	ListNode< NODE > *cPtr = head;

	if ( isEmpty() ) // List is empty
   {
	   throw invalid_argument ("Empty");
   } 

	while(cPtr != NULL)
	{
		if(cPtr->data == value)
		{
			// return the data from the node
			return cPtr->data;
		}
		else
		{
			//move to the next node
			cPtr = cPtr->nextPtr;
		}
	}

	if (cPtr == NULL)
	{
		throw invalid_argument ("Value does not exist");
	}
 
}
Topic archived. No new replies allowed.