Deleting from middle of linked list

Hello, I am having trouble deleting from the middle of the linked lists depending on what number the user inputs.

This is my Linked List class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class NodeType {

public:
	NodeType(int = 0);  		// constructor with default value for
	// info field
	int info;                 	// data
	NodeType * nextPtr; 		// pointer to next node in the list
};

// Constructor
NodeType::NodeType(int data)
{
	info = data;
	nextPtr = 0;
}


And this is my delete function. It finds the inputted number and deletes it from the list. The deleteFront is working fine but the rest isnt.It deletes the link but when I try to print it out using my print function it prints out the correct head but a random numbers for the next in the list and it crashes. For example if I have

1 | 17 | 18 and I want to delete 17 and I enter that as my key and print it out after I will get

1 | (random numbers)

So i know it is being delinked but Im not sure where I went wrong.
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
void deleteKey(NodeType * &head, int key)
{
	bool check = findNode(head, key);
	if (check == false)
		cout << "The Number is not in the list\n";

	if (head->info == key)
		deleteFront(head);
	else
	{
		NodeType* currentPtr;
		NodeType* previousPtr;

		currentPtr = new NodeType;
		currentPtr = head;
		previousPtr = NULL;

		while (currentPtr != NULL)
		{
			if (currentPtr->info == key)
			{
				previousPtr = currentPtr;
				currentPtr = currentPtr->nextPtr;
				previousPtr->nextPtr = currentPtr;
			}
			currentPtr = currentPtr->nextPtr;
		}
		delete previousPtr;
		delete currentPtr;
	}
}


My delete front function looks like:
1
2
3
4
5
6
7
8
void deleteFront(NodeType * &head)
{
	NodeType* currentPtr;
	currentPtr = new NodeType;
	currentPtr = head->nextPtr;
	delete head;
	head = currentPtr;
}
Last edited on
In line 22 previousPtr should not point to the same location as currentPtr.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int* currentPtr = head->nextPtr
int* previousPtr = head

While (currentPtr)
{
        if (currentPtr->info == key)
        {
                previousPtr->next = currentPtr->next
                break // you missed this
        }
        currentPtr = currentPtr->next
}

delete currenPtr // or better call your node class destructor 


I don't know why you delete previous pointer you shouldn't.
also at line 4 it must be else if not if, and i don't see the point of function findNode.
You can return an index rather than a boolean value.
Yet better you can delete the function call as findNode will have to loop over the list and you are already doing it in this function, let's say findNode returned true then you will have to loop again over the entire list in case it was the last node

Also note that you can check the head in the loop rather than checking it alone, but since i don't know what is inside deleteFront function i'll leave it as it is.
I updated the OP with the function implementation. I see why having the find function return the index would be a better decision but the reason I have it set as boolean was because that was requirement for the function.

I'm still shaky on pointers and how they're used. Do you mind explaining why currentPtr is deleted but not previousPtr?
you should read more about dynamic memory allocating, here is a quick explanation but i suggest you look further in this topic.

your previousPtr is pointing to a memory location that you still need to use, so you should not delete it yet this will delete the information stored in that memory location also.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    int* xptr = new int;
    *xptr = 5;
    int* pptr = xptr;
    delete pptr;
    cout << *xptr << endl << &(*xptr) << endl << &(*pptr);
}


this outputs the following


[some random value not 5 as you would expect]
[an address]
[the same address]



in the above example you see how deleting a pointer to a certain value affects that value too even if the pointer is not the same one as the one used to allocate the value in the memory.

AFAIK this is it hope that is clear, maybe there is something I am missing, or I am not correct at all (anyone is welcomed to correct me). so i suggest you read an article or tutorial.

I didn't notice that you posted the code for deleteFront function here is a simplified one that I hope it is the correct way:
1
2
3
4
5
6
void deleteFront(NodeType * &head)
{
	NodeType* currentPtr = head->nextPtr;
	delete head;
	head = currentPtr;
}
Thank you CodeBlob for taking your time to give a small but useful quick lesson on pointers. I appreciate it.
Topic archived. No new replies allowed.