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;
Related
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.
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;
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 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
Context: Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown.
Prompt the user for the number of nodes to delete and then delete accordingly from the list.
Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.
Question: My delete node function is not working, as when I go to display what is in the node that was supposedly deleted, the data is still there. Any Help?
My code: http://www.cplusplus.com/forum/general/210015/
(I am linking to this other website because it was easier to format there.)
My delete node function is not working, as when I go to display what is in the node that was supposedly deleted, the data is still there.
Accessing a destroyed (deleted) object has undefined behaviour.
You cannot expect how the program behaves when the behaviour is undefined. Therefore your expectation that "data shouldn't be there" was ill-founded. A correct expectation is that data might, or might not be there, and that the program might or might not crash and that daemons might or might not fly out of the users nose.
Your code for void deleteNode(struct node *&head) has a lot of bugs.
you don't check if head is nullptr
you don't check if current->next and/or current->prev are nullptr
accessing nullptr pointers is undefined behaviour, that may mean a crash or unexpected results or whatever.
Try using following delete function to delete the node.
Note: assuming delete starts from the head.
void deleteNode(struct node *&head,int deleteCount)
{
if(head != NULL){
struct node *current=head; //copy of head.
struct node *temp,*headPrev,*headNext;
headPrev = head->prev;
headNext = head->next;
int count = 0;
while(count++ < deleteCount)
{
//add code to free current node from me
temp = current; //get rid of this node
currrent = current->next; //skip all the nodes you want delete
}
current->prev = headPrev;
headPrev->next = current;
head = current;
}
}
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 3 years ago.
Improve this question
Alright guys, so I know how the pop function works obviously. I also know that I need to set a LinkNode * top = head and that if top == NULL that you need to return NULL. I'm just not sure what I'm honestly supposed to do after that. The return is supposed to remove and return a value and the data type of the function is a pointer.
I'm not going to post my code on here unless people are honestly going to help me out because I've already been criticized greatly once and it was quite discouraging. :\
“I … know that I need to set a LinkNode * top = head and that if top == NULL that you need to
return NULL. I'm just not sure what I'm honestly supposed to do after that.”
Well, the things you mention have nothing to do with pop.
For a linked list, pop is about unlinking the first node. Depending on the level of abstraction the function might return (a pointer to) that node, or the node’s “value”, or nothing. At the lowest level of abstraction you want just the unlink functionality, which can go like this:
struct Node
{
Node* next;
double value;
};
Node* unlinked( Node*& p )
{
Node* const result = p;
p = p->next;
return result;
}
Then, as an example, a pop that destroys the node goes like this:
void pop( Node*& first )
{
delete unlinked( first );
}
while a pop that returns the value in the node goes like this:
double pop( Node*& first )
{
std::unique_ptr<Node> p( unlinked( first ) );
return p->value;
}
A subtle point here is whether the value is guaranteed to be copied before the node is destroyed. I'm just assuming it is. I leave it to the lawyers to find the standardese for that.
Cheers & hth.