How to handle nullptr in methods when it is not expected? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've a method inside my LinkedList class called insertAtHead and it accepts a Node* pointer as an argument.
void insertAtHead(Node* newNode)
{
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
};
newNode could be nullptr, how do I gracefully handle the situation in that case?
I had few ideas:
Create a custom error class derived from std::exception (say NullPointerParameterException, and add the following statements before anything in the method.
if (newNode == nullptr)
throw NullPointerParameterException("New Node to be added cannot be a null pointer."
Call assert
Print a message to the standard output stream and exit()
What would be the best way to deal with this?

Change the signature to reference:
void insertAtHead(Node& newNode);
So caller is responsible to check its pointer.

Related

Node creation in Linked list in c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
For this class, the object creation statement is node* n=new node(5);
Why do we need to add the * after node? What will happen if I write node n=new node(5)?
Are they both the same?
Why do we need to add the * after node?
new returns a pointer to the memory that was allocated, in this case a node instance, so you need a (node*) pointer variable to receive the returned memory address (of the node instance).
What will happen if I write node n=new node(5)?
The code will fail to compile, since n is not a (node*) pointer.

Compiler says that "head" is not declared: LinkedList [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I define my Node as a struct and trying to get it. But when I run my code compiler says that "head", "tail" was not declared. Am I define it wrongly?
Tried to use template but it didn't work;
You are having head and tail declared inside class List as it's private member fields. But you are accessing them from methods outside the class. The compiler is throwing errors because of that.
In the insertFront function, you have coded
head = node
node->next = head;
This is wrong. head is the only access you have to the remaining linked list. By doing head = node, you lose access to the rest of the list. and doing node->next = head after that essentially points the node to itself. It should be done in reverse.
node->next = head;
head = node;
Another mistake in the insertBack function. prev is not declared in struct Node. The right way to insert at tail is
tail->next = node;
tail = node;

Replacing Nodes in the nth position Link List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
so i want to Write code to implement the replaceNode(…) function that takes pointers to OldNode and
NewNode as inputs using the function header given below. Also include code for the special
cases where OldNode is the head or tail nodes of the list. Assume the list is never empty
before this function is called and that all nodes have been created dynamically
void List::replaceNode(Node *OldNode, Node *NewNode)
{
//write code here
}
i have no idea how to go about doing this can some one please help me with this
CASE 1: where OldNode is neither HEAD or TAIL (Singly LinkedList)
You need to know about the node pointing the OldNode, lets call it *PrevPntr & the node which oldNode is pointing to.
Then it will be something like:
`PrevPntr->next = NewNode;
NewNode->next = OldNode->next
else
just replace the DATA of OldNode by data of NewNode (some constraints will be there in this case).
CASE 2: where OldNode is HEAD (Singly LinkedList).
Then it will be something like:
`
NewNode->next = OldNode->next
CASE 3: where OldNode is TAIL (Singly LinkedList).
You need to know about the node pointing the OldNode, lets call it *PrevPntr.
Then it will be something like:
`PrevPntr->next = NewNode;

Struct allocated with malloc, why? C++ code analysis [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The line:
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
Why does the new struct being instantiated involve malloc (allocating the blocks of memory for the size of the struct) ?
Also, is the re-declaration of struct redundant?
Wouldn't the following accomplish the same task?
Node* newNode = new Node;
MODE CODE BELOW:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct List
{
struct Node *head; // pointer to head node of list
};
//creates a new list Node
struct Node* newListNode(int data)
{
struct Node* newNode =
(struct Node*) malloc(sizeof(struct Node));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
You asked
Wouldn't Node* newNode = new Node; accomplish the same task?
No, this is not equivalent in some very important ways. If part of the interface (contract) of this function is that it returns a pointer that can be passed to free(), then implementing it with new would violate that contract.
Additionally, the repeated redundant use of the struct keyword suggests that an effort has been made to ensure this code compiles correctly both as plain C and as C++.

Why can't i print binary tree? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I created a class binary search tree.
but the problem is when i print the tree it crashes.
i think it can be an endless recursion in function print().
Here is my code
struct node{
node *l,*r;
int data;
};
class BinTree
{
private: node *root;
public:
BinTree(){ root=NULL; }
void add(int a){ add_node(a,root); };
void add_node(int a, node *rot)
{ node *curr; curr=rot;
if(curr==NULL)
{
curr=new node;
curr->data=a;
curr->l=NULL;
curr->r=NULL;
return;
}
if(a>=curr->data) curr=curr->r,add_node(a,curr);
if(a<curr->data) curr=curr->l,add_node(a,curr);
}
void print(){ inorder(root); }
void inorder(node *curr)
{
if(curr->l!=NULL) inorder(curr->l);
cout<<curr->data<<" ";
if(curr->r!=NULL) inorder(curr->r);
}
};
Can anyone help me?
In your add_node method, you never actually assign a value to the root. It should be something like this:
if(curr==NULL)
{
curr=new node;
curr->data=a;
curr->l=NULL;
curr->r=NULL;
root = curr;
return;
}
But, for the future, I have the same advice as Basile - use your compiler and your debugger to your advantange.
Your add_node is broken. If curr is NULL, it creates a new node but it never actually adds it to the existing tree. Thus all additions you make are effectively ignored and the tree stays empty.
The inorder function dereferences curr without checking whether it is NULL, and print calls it without checking whether root is NULL. Thus, your crash most likely is caused by tryin to print out an empty tree and then dereferencing a null pointer.
Learn how to use a debugger. Enable all warnings in the compiler.
On Linux, this means compile with g++ -Wall -g and debug with gdb