Removing last element in LinkedList (C++) - c++

I am writing a function to delete the last node in a linked list. This is what I have, and other code I've found online searching for a solution is very similar (I have found several), but when I execute it it creates some sort of infinite loop when deleting the last element of a linked list (it deletes other elements just fine though).
Here is the code I imagine is causing a problem:
void delete_final(Node* head){
if(head == NULL) {
return; }
if(head->next == NULL) {
delete head;
head = NULL;
return;
}
//other code
}
I imagine it's an issue with the memory (particularly after the delete head; statement), but I'm really stuck and would appreciate any help or an explanation for why this doesn't work (I possibly don't have a very good understanding of pointers and memory in C++, I'm just starting with it)
Here is my Node code for reference:
struct Node {
int key;
Node* next;
};
Thanks for any help!

Original code:
void delete_final(Node* head){
if(head == NULL) {
return; }
if(head->next == NULL) {
delete head;
head = NULL;
return;
}
//other code
}
The "other code" is not specified, but if the list has exactly one node then the above code will
delete that first node, and
update the local pointer head, which doesn't update the actual argument since it was passed by value.
As a result the calling code will be left with a dangling pointer in this case, a pointer pointing to a destroyed object, or to where such an object once was. Any use of such a pointer is Undefined Behavior. It might appear to work, or crash, or just silently cause dirty words tattoo to appear on your forehead – anything…
One fix is to pass the first-pointer by reference:
void delete_final(Node*& head){
if(head == nullptr) {
return; }
if(head->next == nullptr) {
delete head;
head = nullptr;
return;
}
//other code
}
A nice helper function for dealing with linked lists, is unlink:
auto unlink( Node*& p )
-> Node*
{
Node* const result = p;
p = p->next;
return result;
}
The implementation is perhaps a bit subtle, but all you need to remember to use it is that it updates the pointer you pass as argument, which should be either a first-node pointer or a next pointer in the list, and returns a pointer to the unlinked node.
So e.g. you can do
delete unlink( p_first );

Related

Deleting nodes recursively

I have created this function to recursively delete nodes from a doubly linked list. The issue here is that based on the call stack, it starts from the second so it does not delete the entire list. I can delete the remaining node from the method where I'm calling this but there should be a way around that. Is there a way of resolving this issue?
void RecursiveClear(const Node* _curr) {
if(_curr != nullptr) {
//_curr->prev = _curr;
_curr = _curr->next;
RecursiveClear(_curr);
}
if (_curr != nullptr) {
delete _curr;
}
}
First: Don't use a leading _.
You modify _curr in the function so by the time you end up at the delete the original pointer is gone. So don't do that, just call the function wiht the next value without modifying the local vbariable:
RecursiveClear(_curr->next);
You also shouldn't do a recursion like that because lists can be long. Your code is not tail recursive. Every node in the list will use up a little bit of stack space. For long lists this will overflow the stack and crash.
Use a temporary so you can reorder the operations to be tail recursive:
void RecursiveClear(const Node* curr) {
if (curr != nullptr) {
const Node *next = curr->next;
delete curr;
RecursiveClear(next);
}
}

Is this a effective way to delete entire linked list?

In function given below, I simply delete the head-pointer of the list and set head pointer to nullptr (im setting it nullptr because in my print function,I check for nullptr for head node, and ask user to create list first if head-node is nullptr).
void del_list(stud* &orig_head)
{
cout << "Deleting entire list..." << endl;
delete orig_head;
orig_head = nullptr;
}
I have a question regarding the way I choose to delete the list, since im not clearing each node of list, im simply clear the head pointer, what will happen to all the other nodes? Will this approach create a memory leak ?
Edit:
Im not using OOP to implement linked list,im implementing linked list using struct and couple of functions.
I like to handle this problem recursively:
void deleteNode(Node * head)
{
if(head->pNext != NULL)
{
deleteNode(head->pNext)
}
delete head;
}
If we have a list of 5 items:
head->pNext->pNext->pNext->pNext->NULL;
Then, the function will first get called for head, then for each pNext until the last one. When we reach the last one, it will skip deleting the next one (since it's null) and just delete the last pNext. Then return and delete the list from back to front.
This is assuming that each node's pNext is initialized to NULL. Otherwise, you'll never know when you've reached the end of the linked list.
Your code will cause memory leak. To delete it correctly, traverse the list and while traversing delete each node separately. And finally make head pointer to point to NULL value. You can have a look at the following code.
void deleteList(struct Node** head_ref)
{
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
//Now make head_ref point to null
*head_ref = NULL;
}

function with return type node* in conjunction with OOP in c++

I need to write a program that takes an instance of a linear linked list and removes all of the items in the list except for the last two items. I'm doing this in c++ using classes, so I'll have 3 files: main.cpp, list.h, and list.cpp. There can be no loops: I can use multiple functions but the traversal part has to be recursive.
I thought about it and what I concluded is this: I'll have to have one public function that I can call from main which will take no arguments, called void lastTwo(). I'll have another private function called node* lastTwo(node *& current, node *& tail) which will be called by lastTwo(), and it will traverse the list recursively and delete the nodes it touches until it reaches the second to last node in the list, and then it will return the address of that node.
Then, when it returns the address of the second to last node in the list, the public function lastTwo() will catch that address and set head equal to it.
The problem I'm having is that I need to do this in vi and compile and run from the command line, and I'm getting a segmentation fault even though I drew a pointer diagram and can't find a problem with my code.
I'm working on this on the student server at my university, and all of the implementation of the data structure except for these two functions have been written by my instructors, so they're all solid. Also, every time you run the a.out file, they've written it to generate a new, random, non-empty linked list, of at least 5 nodes.
I think the problem is related to the function having the return type "node*" , because I tried doing this in visual studio as well and it wouldn't let me have functions of type node*. But, I know that when you don't use classes and just put everything in one file, functions of type node* work.
Here is my list.h:
#include<iostream>
struct node
{
int data;
node* next;
};
class list
{
public:
// these functions were already written by the school and included in the .o file
list();
~list();
void build();
void display();
// my functions
void lastTwo();
private:
node* head;
node* tail;
node* lastTwo(node *& current, node *& tail);
}
And list.cpp:
void list::lastTwo(){
node* current = head;
node * newHead = lastTwo(current, tail);
head = newHead;
delete newHead;
head->next = tail;
}
node* lastTwo(node *& current, node *& tail){
if(current->next == tail){
return current;
}
else if(current->next != tail){
node* temp = current;
current = current->next;
temp->next = NULL;
delete temp;
lastTwo(current, tail);
}
return NULL;
}
Any ideas on what might be the problem, and what the correct way to do this is, would be really appreciated! Thank you
Your problem happens when your recursion unwinds. Most of the calls to lastTwo happen in the else if. That base case is the if which returns current, but the call to lastTwo which triggered the base case is always going to return NULL when the else if ends.
Imagine this is your stack when the base case is reached:
lastTwo // Base case, returns current, but the call below this doesn't do anything with it.
lastTwo // returns null
...
lastTwo // returns null
That NULL is used by the other lastTwo and you get your seg fault.
You might use the following:
void list::lastTwo() {
// Stop work with less than 2 items
if (head == nullptr // 0 items
|| head == tail // 1 item
|| head->next == tail) { // 2 items
return;
}
// pop_front()
auto* next = head->next;
delete head;
head = next;
// loop
lastTwo();
}

Queue implementation is losing track of head node

I have a queue add function implemented
void queue::add(myObj info)
{
node* node = new node;
node->info = &info; //<---suspect
node->next = NULL;
if(head == NULL){
head = node;
}
else{
tail->next = node;
}
tail = node;
count++;
}
Every time this gets visited the head node's data points to whatever I'm passing in. I realize there is a template for this but I am trying to build one, because I obviously need practice.
I am trying to keep all the pointers pointed to the original objects. I wanted to pass in the object and point to the refrence.
The node is a struct with myObj * info and node * next
info is a parameter of your function, that is passed by value. In this case, &info is the address of the parameter, and not of the original data.
This is undefined behaviour and can only give weird results.
One possible solution would be:
void queue::add(myObj& info) // pass by reference
{
... // unchanged code
}
In this case, &info would refer to the address of the original object.

C++: Is this the proper way to delete a linked list recursively?

I was wondering if this is correct, since XCode gives me a EXC_BAD_ACCESS error when doing getNext(), but Visual Studio doesn't.
void deleteList(LinkedList* node) {
if (node == NULL)
return;
else {
delete node;
node=NULL;
deleteList(node->getNext());
}
}
You delete a node, nullify it, and then use it to call getNext(). That's undefined behavior.
Simply do the same work in the nodes destructor.
struct NodeT
{
NodeT* next;
~NodeT()
{
delete next;
}
};
Either that, or first delete the tail, and only then the node itself:
void deleteList(LinkedList* node) {
if (node == NULL)
return;
else {
deleteList(node->getNext());
delete node;
}
}
Setting node to NULL is normally a way to let the system know it's empty. Setting a NULL node usually makes your program safer, since the compiler or IDE may notify you that it's a NULL node when you try to delete a NULL node. For a deeper understanding, you can look for some instruction about empty pointers.