public void insertion(int data)
{
Node new_node = new Node(data);
if(head==null)
{
head=new_node;
}
new_node.next=head;
head=new_node;
}
I want to insert the new_node when head is null.
I am getting infinite loop of first value which i want to insert in the node
When head == null is true, you essentially do:
head = new_node;
new_node.next = head;
Think about those lines carefully. After the first line runs, head is new_node. With that in mind, the second line is essentially equivalent to
new_node.next = new_node;
Which should highlight the problem. You're setting the new node's tail to itself! That will lead to a cyclic list that will continue infinitely when iterated.
You only want to set head'snextwhenhead*wasn't*null` initially:
public void insertion(int data)
{
Node new_node = new Node(data);
if(head==null)
{
head=new_node;
} else {
new_node.next=head; // Only execute these lines if head already existed
head=new_node;
}
}
Related
For example i want to add an element at to the beginning list:
public void add(int Data){
//ignoring the possibility that the list is empty
Node currentNode = head;
do {
currentNode = currentNode.getNext();
} while (currentNode.getNext() != head);
Node newNode = new Node(Data);
newNode.setNext(head);
currentNode.setNext(newNode);
head = newNode;
What troubles can it cause if we use a while loop in the above code?
As I can see your example code is incorrect, at the end of the while the currentNode will be head but you need to stop at the node before the head.
Your stop condition should be:
...
}
while (currentNode.getNext() != head)
There will be no troubles if you use a while loop. All you need to do is to start from the next node after head and go until the node before head.
Node currentNode = head.getNext();
// #todo: test first if the currentNode is null
while (currentNode.getNext() != head) {
currentNode = currentNode.getNext()
would help me to solve this problem
i am trying to insert the node at ith location in linked list using recursion
here is the code please help me to imporve the code
i ma specifically facing problem at the end when i have to return the head i am not getting how to return
Node* insertNodeRecursively(Node*head, int n, int data)
{
if(head == nullptr)
{
return head;
}
else if(n==0)
{
Node* newNode= new Node(data);
newNode->next = head->next;
head->next = newNode;
return head;
}
Node * x = insertNodeRecursively(head->next,n-1,data);
}
So I guess you want the function insertNodeRecursively to return the new head, right? Anything else would not work without considering the special case of inserting at position 0 in the caller function.
You could do the following:
if (n == 0) {
Node *newNode = new Node(data);
newNode->next = head;
return newNode;
}
if (head == nullptr) {
return nullptr;
}
Node *node = insertNodeRecursively(head->next, n - 1, data);
head->next = node;
return head;
The idea is that when this is not the position to insert to, we need to assume the next recursive call might give us a new head (from that position on), so we need that head->next = node; assignment. To call the node passed as first argument head might confuse as it is not always the head of the list (Only at the start of the recursion)! It is rather the node in front of which you want to insert in case n is 0. The problem with what you wrote in the n == 0 case is that you always return the old head, even though you should return newNode. The head node should be what follows after newNode.
I'm facing weird problem that the only 1st element of the list is get printed. I have writing linked list program after long time. thanks for the help. Is there something wrong with printAll function or add function in list class. I have tried printing previous elements while adding new one & it works. So, I'm not getting why only 1st element .ie. head is getting printed & head->next seems to be null.
#include<iostream>
using namespace std;
class Node{
public: int data;
public: Node *next;
public: Node(int data){
this->data = data;
this->next = NULL;
}
};
class List{
Node *head, *trav;
public: List(){
this->head = NULL;
this->trav = NULL;
};
void add(int data){
if(this->head==NULL && this->trav==NULL){
cout<<"inside the if block"<<endl;
this->head = new Node(data);
this->trav = this->head->next;
}
else{
cout <<"inside the else block"<<endl;
this->trav = new Node(data);
this->trav = this->trav->next;
}
}
void printAll(){
this->trav = this->head;
while(this->trav!=NULL){
cout<<this->trav->data<<endl;
this->trav = this->trav->next;
}
}
};
int main(){
List list;
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.printAll();
cout<<sizeof(list);
}
The add() method else part is not linking list properly, do some paper work.
Here is the working one, I tried to explain in comments.
void add(int data){
if(this->head==NULL && this->trav==NULL){ /* for 1st node */
cout<<"inside the if block"<<endl;
this->head = new Node(data);
this->trav = this->head->next;
}
else{
this->new_node = new Node(data); /*new_node */
cout <<"inside the else block"<<endl;
this->trav = head;/*temp var to point to ast node */
while(this->trav->next!=NULL) {
this->trav = this->trav->next;
}
this->trav->next = this->new_node; /*adding at end */
this->new_node->next = NULL; /*new_node next make it NULL */
}
}
Your add function doesn't link anything. Whenever you get into the else block trav is NULL and you set it equal a new node. But you never link that new node to the previous last node.
Normally trav would be named tail and point to the last element so that you can link a new element to the current last element.
Something like:
if(this->head==NULL && this->trav==NULL){
cout<<"inside the if block"<<endl;
this->head = new Node(data);
this->trav = this->head;
}
else{
cout <<"inside the else block"<<endl;
this->trav->next = new Node(data);
this->trav = this->trav->next;
}
Edit
OP commented that trav is not considered a tail pointer but just a pointer to traverse the list.
So therefore the answer is different as the code need to find the current tail using a loop.
Something like:
if(this->head==NULL){
cout<<"inside the if block"<<endl;
this->head = new Node(data);
}
else{
cout <<"inside the else block"<<endl;
this->trav = this->head;
while(this->trav->next)
{
this->trav = this->trav->next;
}
this->trav->next = new Node(data);
}
However, notice:
If trav is "just" a pointer to traverse the list, there is no real purpose in making it a member of List. Simply use a local variable inside the functions that need to traverse the list.
Since your code adds new elements to end-of-list it's often a very good idea to have a tail pointer as member in List. Especially if the list can hold many elements and you frequently add new elements.
Your code use this->some_member in many places where it's not needed. Avoiding that will make your code easier to read.
You never set the next pointer in Node.
Your code also has less-than-ideal indentation, uses raw pointers in places where you could use unique_ptr, doesn't initialise variables in constructors the C++ way, uses NULL instead of nullptr, and performs undefined behaviour by falling off the bottom without returning a value in a function declared to return a value (main(), specifically).
The problem is in your add() method.
Replace it with this
void add(int data){
if(this->head == NULL){ /*If head is null, init it and trav node*/
cout<<"inside the if block"<<endl;
this->head = new Node(data); /*init head*/
this->trav = this->head; /*point to head of list*/
} else{
cout <<"inside the else block"<<endl;
this->trav->next = new Node(data); /* add new elem to next of trav*/
this->trav = this->trav->next; /*move trav to next node i.e. reset */
}
}
I have written this code for implementing Linked List in c++. It worked perfectly till i added the insert at end function to it. Please see what's wrong! Without the insertatend function, output is correct. After adding that function, the output is 1 10 which is actually the insert at start and end's outputs.
void List::insertatend(int num)
{
Node *new_node=new Node(num);
if(listptr==NULL)
listptr=new_node;
else
for(Node *temp=listptr; temp->next!=NULL; temp=temp->next)
temp->next=new_node;
}
The problem is in the lines:
for(Node *temp=listptr; temp->next!=NULL; temp=temp->next)
temp->next=new_node;
It seems like you haven't walked through the logic of how the code works.
You will first need to iterate until temp->next is NULL and then use
temp->next=new_node;
The code to implement that logic is:
Node* temp = listptr;
for ( ; temp->next != NULL; temp = temp->next )
{
// Do nothing in the loop.
}
temp->next = new_node;
Here's the updated function:
void List::insertatend(int num)
{
Node* new_node = new Node(num);
if( listptr == NULL)
{
listptr = new_node;
}
else
{
Node* temp = listptr;
for ( ; temp->next != NULL; temp = temp->next )
{
}
temp->next = new_node;
}
}
Go through the basic logic of how to add the every new node at end of previous node.Bug is in below two lines of insertatend function, explanation I mentioned in comments.
for(Node *temp=listptr; temp->next!=NULL; temp=temp->next) //it should be dummy loop
temp->next=new_node;//In all old nodes next part will replace by new_node which is wrong
Modiy Insert_end() function as
void List::insertatend(int num)
{
Node *new_node=new Node(num);
if(listptr==NULL)
listptr=new_node;
else{
Node *temp=listptr;
for( temp ; temp->next!=NULL ; temp=temp->next); /** rotate dummy loop until temp is not reaching to last node **/
temp->next = new_node;// in last node next put the new node address
new_node->next = 0; // and new node nnext put the zero
}
}
I have a method called Find, which, when the user enters the value of an element, the address gets saved in a new Node called *npt.
Using *npt as an argument, I am supposed to create a delete method which removes the value after the value saved in npt.
For example, if this is my list:
134, 564, 674, 253,and I enter 674 for Find, then 253 gets deleted.
Currently, this is my code for the delete function:
void LinkedList::Delete(Node *PrePosition){
Node *temp = PrePosition -> next;
PrePosition->next = PrePosition->next->next;
delete temp;
}
The problem with this though, is that there is no way to delete the head node currently. The only hint I was given, which does not really help me is this:
PrePosition is NULL if you want to delete the head of the list.
Assuming you have to do a recursive version.
Make a condition for Head. In Find:
Node* find(T value){
if (head->value == value)
return nullptr;
return find(head->next);
Now in Delete:
void Delete(Node * pre) {
Node* temp;
if (pre == nullptr) {
temp = head;
head = head->next;
} else {
temp = pre->next;
pre->next = pre->next->next;
}
delete temp;
}