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.
Related
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 5 years ago.
Improve this question
Assigning a new object to a pointer pointer type in cpp crushes the program.
Code looks like this:
#include <iostream>
using namespace std;
struct Node{
int key;
Node * nex;
};
int main(){
Node * my_node = new Node;
Node ** pp_node;
*pp_node = my_node;
return 0;
}
I think that the issue here is following: pp_node is null and by typing *pp_node = my_node; I am trying to dereference null but the question is how can I assign my_node to the pointer, pp_node is pointing at?
pp_node should contain the address of my_node , in order to have pp_node point at my_node(which is itself a pointer and holding an address).
#include <iostream>
struct Node{
int key_;
Node * nex;
};
int main(){
Node node={34, nullptr};
Node* p_my_node = &node;
Node** pp_node;
pp_node = &p_my_node;
std::cout<<(**pp_node).key_;
return 0;
}
Now
p_my_nodeis a piece of memory whose value(data it is holding) is address of node. So it points at node
pp_node is a piece of memory whose value is address of p_my_node, So it points at p_my_node
the output of the code above is 34, as we expected.
pp_node is uninitialized. You need to initialize it first.
Node** pp_node = new Node*;
*pp_node = my_node;
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.
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;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I wrote a doubly linked list, and trying to add an append()(insert at the end)and len()(caculate the number of member in the list). I just don't understand why it doesn't work now. Here is the simplest code:
#include<iostream>
using namespace std;
class linkedList{
private:
struct node{
node* last;
node* next;
char* str;
};
node sentinel;
public:
linkedList();
~linkedList();
int len();
void append(char*);
};
linkedList::linkedList(){
sentinel.last=&sentinel;
sentinel.next=&sentinel;
sentinel.str="I am sentinel!!";
};
linkedList::~linkedList(){};
int linkedList::len(){
node* currentNode=&sentinel;
int count=0;
while ((*currentNode).next!=&sentinel){
count++;
currentNode=(*currentNode).next;
cout<<(*currentNode).str<<endl;
}
return count;
}
void linkedList::append(char* str){
node newNode;
newNode.str=str;
newNode.last=sentinel.last;
(*sentinel.last).next=&newNode;
sentinel.last=&newNode;
newNode.next=&sentinel;
}
int main(){
linkedList myList;
myList.append("Hello");
myList.append("World");
int length=myList.len();
cout<<length<<endl;
return 0;
}
What I am doing is just add two new nodes into the linked list, and caculate the total number of my nodes. it should return 2. but why it doesn't work?
newNode in your code below will go out of scope as soon as append is finished executing. Assigning it's memory address as a pointer to more global member is likely going to end in a segfault.
void linkedList::append(char* str){
node newNode;
newNode.str=str;
newNode.last=sentinel.last;
(*sentinel.last).next=&newNode;
sentinel.last=&newNode;
newNode.next=&sentinel;
}
Try allocating your node on the heap using new node, possibly using a shared_ptr to make memory management a bit simpler.
void linkedList::append(char* str){
node *newNode = new node;
newNode->str=str;
newNode->last=sentinel.last;
(*sentinel.last).next=newNode;
sentinel.last=newNode;
newNode->next=&sentinel;
}
With this approach, be sure to cleanup the nodes when destructing your linkedlist, via the delete operator on each node.
Alternatively, look into using shared_ptr's to a Node instead of raw pointers, which will always call delete when the linkedlist (and nobody else) is pointing to the node.
Use the new keyword to allocate a new node:
void linkedList::append(char* str){
node *newNode = new node();
newNode->str=str;
newNode->last=sentinel.last;
(*sentinel.last).next=newNode;
sentinel.last=newNode;
newNode->next=&sentinel;
}
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