Deleting a custom doubly linked list - c++

List Property:
one pointer pointing to the next node and the other pointer to any arbitrary node in the list.
Structure
struct node
{
int val;
node* link[2];
node(int x);
~node();
};
node :: node(int x)
{
val = x;
link[0] = NULL;
link[1] = NULL;
}
node :: ~node()
{
delete(link[0]);
delete(link[1]);
}
Class
class List
{
node *head, *cloneHead;
node *stack[100];
int childIndex[2][100];
int stptr;
public:
List();
~List();
void createList(int[] , int[][2], int );
int createListStruct(node*);
void createCloneList();
void clone();
void printClone();
};
Creating the list
void List::createList(int a[], int child[][2], int size)
{
node* linkedList[size];
for(int i=0;i<size;i++)
{
linkedList[i] = new node(a[i]);
}
head = linkedList[0];
for(int i=0;i<size;i++)
{
for(int j=0;j<2;j++)
{
if(child[i][j]!=-1)
{
linkedList[i]->link[j] = linkedList[child[i][j]];
}
}
}
}
Main
int main()
{
int a[]={10,1,3,7,2,8,20};
int child[][2] = {{1,4},{1,2},{3,-1},{6,5},{6,5},{-1,0},{5,5}};
int size = sizeof(a)/sizeof(a[0]);
List L;
L.createList(a,child,size);
L.clone();
L.printClone();
return 0;
}
in normal circumstances the destructor work perfectly but for list with the above List property its failing
eg:
Node : 1
Link1 : Node 2
Link2 : Node 3
Node : 2
Link1 : Node 3
Link2 : Node 1
in the above case by the time destructor reaches Link2 of Node2, which point to node 1, node 1 is already deleted, so the code is throwing segmentation error.
I came up with : have a array of unique nodes in list and delete one by one
Is there any other way to do that?

You can use shared_ptr's and they will release memory when the last pointer become destroyed or reassigned. The only thing you have to remember is to avoid cycles, thats why for arbitrary node ponter use weak_ptr instead.
struct node; // forward declaration
struct node
{
int val;
shared_ptr<node> next;
weak_ptr<node> other;
};

2 alternatives to your list idea (which seems ok)
For each Node keep a list of the parents. When a node gets deleted, set all the parents their pointers to that node to nullptr. You can safely delete a nullptr as often as you like.
If your graph is a tree (i.e. without cycles) you can use reference counting using either shared pointers or unique_pointers

Related

Why are changes made to a pointer inside a function not visible outside the function?

I am implementing a linked list. There is a problem: when I change a pointer inside a function, the change is not visible outside of the function.
#include<bits/stdc++.h>
using namespace std;
//define
class List{
public:
int data;
List *next;
public:
List(int n){
this->data=n;
this->next=NULL;
}
};
void add(List *head,int data){
List * nn = new List (data);
head=nn;
}
// driver code
int main(){
List *head=NULL;
List * nn = new List (45);
head=nn;
cout<<head->data;
return 0;
}
This code is printing 45.
class List{
public:
int data;
List *next;
public:
List(int n){
this->data=n;
this->next=NULL;
}
};
void add(List *head,int data){
List * nn = new List (data);
head=nn;
}
// driver code
int main(){
List *head=NULL;
add(head,45);
cout<<head->data;
return 0;
}
This program prints nothing. It simply crashes.
This function:
void add(List *head, int data)
takes the head pointer by copy, so changes to the pointer inside the function are not visible to the caller. This means the pointer head in main is not pointing to valid memory, and dereferencing it invokes undefined behavior (which could cause the crash).
Make the function take the pointer by reference instead:
void add(List * &head, int data)
and now changing head inside the function changes head in the main function.
There are 2 issues in add method:
1) You need to accept pointer to reference (or pointer to pointer)
2) You need to add logic to traversal until the last node in the list and add the new node at the end. Otherwise everytime you are resetting list head with new node and new node is pointing to null and becoming one node list.
Similar implementation you can refer here https://github.com/alokkusingh/DataStructure . The implementation is in C (not C++)

C++ pointers not working?

I have a problem with working with c++ pointers. I'm trying to code a splay tree by using a Node struct and Tree struct. However, upon testing, I have encountered a problem. The portion of my code that's not working is below:
struct Node {
Node* l, *r, *p;
int v;
Node() {}
Node(int _v, Node* _p) : v(_v), p(_p) {}
};
struct Tree {
Node* root;
Tree() : root(0) {}
//...
void insert(int k) {
if (!root) {
root = new Node(k, 0);
return;
}
Node* cur = new Node();
cur->v = root->v;
while (1) {
int x = cur->v;
cout << x << endl;
return;
if (k <= x) {
//cout << x << endl;
//return;
if (!cur->l) {
cur->l = new Node(k, cur);
//splay(cur->l);
return;
} else cur = cur->l;
} else {
if (!cur->r) {
cur->r = new Node(k, cur);
//splay(cur->r);
return;
} else cur = cur->r;
}
}
}
//...
};
int main() {
Tree t = Tree();
t.insert(1);
t.insert(5);
return 0;
}
First, I inserted a node with value 1 in the tree; since there was no root, the tree assigned its root as a new node with value 1. Then, when I inserted 5 into the tree, something weird happened. If you leave the code like it is (keeping the first cout), then it will print out 1 for x. However, if you comment out the first cout and return and uncomment the second cout and return, you'll find that it prints out a random garbage number for x, even though no modifications were made. Can somebody tell me what's wrong?
C++ does not initialize class members automatically.
struct Node {
Node* l, *r, *p;
int v;
Node() {}
Node(int _v, Node* _p) : v(_v), p(_p) {}
};
When you create a new node in your code C++ allocates a piece of memory for the Node but it will not clear it. So the values of l, r & p will be whatever was there.
In your algorithm the tests: if (!cur->r) & (!cur->l) currently fail because there is uninitialized garbage in the nodes and not NULL.
As a result when you try to insert the second node the algorithm thinks that there is a valid node to the right of root. And tries to read the memory there and the value there which is the junk x you see. Depending on the value of the junk it may also crash for some people running the code :)
Also I'm 99.9% certain that Node* cur should be a pointer to a Node in the tree and not a new node so:
Node* cur = new Node(); cur->v = root->v; is wrong and should be Node* cur = root;
Proper Initialization -
In c++11 you can do:
struct Node {
Node* l = nullptr;
Node *r = nullptr;
Node *p = nullptr;
int v = 0;
Node() {}
Node(int _v, Node* _p) : v(_v), p(_p) {}
};
Otherwise
struct Node {
Node* l;
Node *r;
Node *p;
int v;
Node() : l(NULL), r(NULL), p(NULL), v(0){}
Node(int _v, Node* _p) : l(NULL), r(NULL), p(_p), v(_v) {}
};
You should initialize members of a class in the same order they were defined.
Now there are a lot of other things that are problematic in the code:
Tree seems to allocate lots of nodes but does not release any memory. (easiest to just use unique_ptr for l and r and root Node)
Is tree the owner of subnodes? Or should it be Node owning and allocating left and right? (goes away if you use std::unique_ptr for left and right)
You are not initializing the members in the order they are defined. This can cause all kind of errors. (since the compiler reorders initialization without telling you)
Node and Tree handle raw pointers but do not define a proper operator=, copy ctor (or delete them) (goes away if you use unique_ptr)
Tree is missing a dtor to clean allocated memory (goes away if you use unique_ptr)

How to 'delete' this linked list from memory?

I've created class for building a linked list. The class declaration is as follows:
class LinkedList
{
private:
int data;
LinkedList *next;
static int count;
public:
LinkedList(void);
~LinkedList(void);
int insert(int arg);
int remove(int arg);
bool find(int arg);
};
How can I make sure all nodes of this linked list are deleted? The destructor is made responsible for deleting just one node. I used to make linked list like this previously but never thought about clearing the memory.
The naive implementation
~LinkedList() {delete next;}
will do the right thing - delete will call the destructor on the next element, which will delete the one following it, and so on, to delete the whole list.
However, this means that the destructors are called recursively, so that deleting a very long list could cause a stack overflow. Iteration might be better:
~LinkedList() {
while (LinkedList * head = next) {
next = head->next;
head->next = nullptr;
delete head;
}
}
As noted in the comments, it might be more appropriate to have separate List and Node classes, with List responsible for memory management, and Node a simple aggregate containing the data and the link. Then there's less scope for error in the destructor, as it doesn't need to nullify any pointers to prevent recursion:
struct Node {
int data;
Node * next;
};
struct List {
Node * head;
~List() {
while (Node * victim = head) {
head = victim->next;
delete victim;
}
}
};

delete in binary search tree C++ (tree won't update) and heap corruption

I am trying to write a node deletion for a binary tree. These are my node and tree structures:
class node{
public:
int value;
node* left;
node* right;
~node();
};
class tree{
public:
node* root;
....
};
And this is the function I wrote:
void tree::del(node** r, int x){
if(*r)
{
if((*r)->value==x)
{
if(!(*r)->left)
*r= (*r)->right;
else if(!(*r)->right)
*r= (*r)->left;
else
{
int k= delMax((*r)->left);
(*r)->value= k;
}
}
else if((*r)->value > x)
{
node* k= (*r)->left;
del(&k, x);
}
else
{
node* k= (*r)->right;
del(&k, x);
}
}}
My problem is that once I get to the desired element, the pointers change but then when the tree is rebuilt recursively it goes back to what it was originally and no element is deleted. I thought passing a pointer to the pointer would solve this but it didn't. delMax deletes the maximum element from the tree and it works correctly on its own.
Also, in the destructors for the last two classes, how should I place the deletes? because if I just put delete right; delete left; in ~node() and delete root in ~tree() I get an error that I'm corrupting the heap.
Thanks!
By making a local variable k and passing its address, the assignments through *r affect the local variable rather than any pointer in the tree.
As an aside, writing node *&r might save you several & and *s.

Pointers - passing ptr to a ptr or passing address of a ptr

I am trying to remove the left child (10) of a sample binary search tree using two methods:
Method1: By passing pointer to a pointer to the current node.
Method2: By passing address of the pointer to the current node. This does not removes the node, but calling delete corrupts the pointer arrangement, causing a crash while printing the nodes.
The tree looks like this and I am trying to delete 10 and replace it with 5
20
|
10--|---30
|
5---|
I have some understanding of pointers. But still, I am not clear with this behavior of pointers.
#include <iostream>
class Node
{
public:
Node(int key) : leftChild(0), rightChild(0), m_key (key){}
~Node(){}
Node *leftChild;
Node *rightChild;
int m_key;
};
Node* build1234(int, int, int, int);
void print(Node *);
void print1234(Node *);
void removeLeft(Node **nodePtr)
{
Node *oldPtr = *nodePtr;
if(*nodePtr)
{
*nodePtr = (*nodePtr)->leftChild;
delete oldPtr;
}
}
int main()
{
Node *demo1 = build1234(10, 20, 30, 5);
Node *demo2 = build1234(10, 20, 30, 5);
print1234(demo1);
print1234(demo2);
//Method1 - 10 is correctly removed with 5
Node **nodePtr = &demo1;
nodePtr = &(*nodePtr)->leftChild;
removeLeft(nodePtr);
print1234(demo1);
//Method2 - 10 is not removed
Node *node = demo2;
node = node->leftChild;
removeLeft(&node);
print1234(demo2);
return 0;
}
Node* build1234(int B, int A, int C, int D)
{
Node *root = new Node(A);
root->leftChild = new Node(B);
root->rightChild = new Node(C);
root->leftChild->leftChild = new Node(D);
return root;
}
void print(Node *node)
{
if(node)
{
print(node->leftChild);
std::cout << "[" << node->m_key << "]";
print(node->rightChild);
}
}
void print1234(Node *node)
{
std::cout << std::endl;
print(node);
}
Note: This question is not about BST, but pointers. If you see the two calls to removeLeft(nodePtr) and the removeLeft(&node) in the main() function.
How are these two different?
Why the second method fails to achieve the desired result?
In the first case, you are passing an address of a pointer that exists in the tree, so you are modifying the contents of the tree directly.
In the second case, you are passing an address of a variable that is local to main() instead. The tree is not modified, and deleting from the address is accessing stack memory, which is why it crashes
You're overthinking it. All you need is a function removeLeft(Node*) that unhooks the left node and deletes it, recursively:
void removeLeft(Node * p)
{
removeBoth(p->leftChild); // recurse, OK if null
delete p->leftChild; // OK if already null
p->leftChild = 0; // necessary to make recursion terminate
}
void removeBoth(Node * p)
{
if (!p) return;
removeLeft(p);
removeRight(p);
}
If you are bad with pointers consider using smart pointers.
When using smart pointers use shared_ptr<Node> instead of Node * and make_shared(new Node); instead of new Node and remove all deletes. now you can handle pointers without caring for deletes and memory corruption.