Im trying to do insertion sort using linked list but this code seems to have runtime error.
void Insert(int data)
{
node* temp=new node();
temp->data=data;
temp->link=NULL;
if(head==NULL)
{
head=temp;
return;
}
node* current=head;
if(current->data>data)
{
temp->link=head;
head=temp;
return;
}
else
{
current=head;
node* trail=head;
while(current->data<=data)
{
trail=current;
current=current->link;
}
trail->link=temp;
temp->link=current;
}
}
Your issue is in the else block of your second if.
You're looping through the list, and everything seems fine...but what happens if you get to the end of the list and current->data is still less than or equal to data?? Uh oh! current = current->link, current will now be NULL, so the next current->data will be trying to dereference a null pointer!
Just add a check for that in your loop condition and everything will be peachy:
while(current && current->data <= data) {
This expression will now short circuit if current is the null pointer, saving you from that issue.
Not sure if this is the only error but:
current=head;
node* trail=head;
while(current->data<=data) // <-- This doesn't check if current->link is NULL
{
trail=current;
current=current->link; /// <-- If current is NULL this would explode
}
trail->link=temp;
temp->link=current;
Related
void DeleteInBetween(node **head,int data_to_be_deleted){
node *newnode= (*head);
while ((((newnode)->next)->data!=data_to_be_deleted)and (newnode!=NULL))
{
(newnode)=(newnode)->next;
}
if (newnode!=NULL)
{
node *nw=(newnode)->next;
(newnode)->next=((newnode)->next)->next;
free(nw);
}
}
the code is working fine for the linked list if the element that has to be deleted is in the list but if the element is not there then the function clears the whole list and iI am unable to get it back. Help me with the error.
The below will be searching for the node with given data, it will delete the first node that has this data, and delete that node only and return. If there was a need to delete any number of nodes that has this data, then you need to modify the function, but this is a good start for you.
node* DeleteInBetween(node **head, int data_to_del)
{
node *temp;
//check if searched data is in the head node.
if((*head)->data == data_to_del)
{
temp = *head; //backup to free the memory
*head = (*head)->next;
free(temp); //in c, for c++ you could call delete
return *head; //this is the new head
}
else
{
node *current = *head;
while(current->next != NULL)
{
if(current->next->data == data_to_del)
{
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
free(temp);
break;
}
//Otherwise, move the current node and proceed
else
current = current->next;
}
}
}
I have a problem in a question of removing duplicates from an unsorted linked list. My code is not returning the output as I need.
Node * removeDuplicates( Node *head)
{
Node *cur=head;
Node *prev=NULL;
Node *nextptr;
while(cur!=NULL){
nextptr=cur->next;
cur->next=prev;
prev=cur;
cur=nextptr;
}
head=prev;
Node *temp=head;
while(temp->next!=NULL){
if(temp->data=temp->next->data){
temp->next=temp->next->next;
}
}
return head;
}
The linked list given is 5->2->2->4->NULL.
This is code for removing duplicates from an unsorted linked list. Its return type is a pointer. I am not getting required output. The required output is 5->2->4->NULL, but my output coming is only 5. Please help me with correct code.
There are these issues:
Instead of comparing data in the if statement, you are assigning. So change:
if(temp->data=temp->next->data){
to:
if(temp->data==temp->next->data){
When that comparison is false, nothing else happens in the loop's body, so the while loop will then get stuck. You should move the temp pointer. So add an else block:
} else {
temp = temp->next;
}
Some other remarks:
Don't compare/initialise pointers with NULL, but with nullptr
The first part of your function reverses the list. This seems unrelated to the purpose of the function, so omit that.
Corrected:
Node * removeDuplicates( Node *head) {
Node *temp=head;
while (temp->next != nullptr) {
if (temp->data == temp->next->data) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
return head;
}
I am getting a segmentation fault for the code below when the program reaches addnode() function. So I don't know if there would be any issues elsewhere since the flow is not going forward
#include<iostream>
using namespace std;
struct node
{
int value;
node *next;
};
node *head;
void addnode(int);
void destroytree();
int main()
{
head=new node;
head=NULL;
int no,ch=1;
while(ch!=0)
{
cout<<"Enter Choice (0 exit, 1 enter): ";
cin>>ch;
if(ch==0) break;
cout<<"Enter no: ";
cin>>no;
addnode(no);
}
cout<<"\nTime to print.\n\n";
destroytree();
cout<<endl;
return 0;
}
void addnode(int no)
{
node *n=new node;
node *trav;
trav=head;
while(trav!=NULL && trav->next!=NULL)
{
trav=trav->next;
}
if(trav==NULL)
{
trav->value=no;
trav->next=NULL;
return;
}
n->value=no;
n->next=NULL;
trav->next=n;
}
void destroytree()
{
node *n;
while(head!=NULL)
{
n=head;
cout<<head->value<<"->NEXT ** ";
head=head->next;
delete n;
}
}
Classes are not allowed. I want 'head' to point to start of the list in all cases except destroytree(). To add a node to the end of the list, start from the 'head' and move on till next is NULL. To destroy the tree, print the first element then delete it. Print the next element and delete it till node->next is NULL
You are dereferencing a null pointer which doesn't make sense.
You are also repeating yourself in the code below so maybe you're not thinking through what you're doing.
if(trav==NULL)
{
trav->value=no;
trav->next=NULL;
return;
}
n->value=no;
n->next=NULL;
trav->next=n;
Apart from assigning the values to the node, all you're looking to do is drop the new node into the end of the list. If you didn't find that by traversing the list it's because the list is empty so the end of the list is the head.
n->value=no;
n->next=NULL;
(trav != NULL ? trav->next : head) = n;
Problem is here
head=new node;
head=NULL; // Remove this line
After you assign new allocated memory to head, you overwrite it with null and trying to dereference it.
Also here, you would dereference NULL pointer as well
if(trav==NULL)
{
trav->value=no;
trav->next=NULL;
return;
}
Since it is redundant you should remove it and use following
n->value=no;
n->next=NULL;
n = (trav != NULL ? trav->next : head);
nullptr is of pointer type , while NULL has the tendency to be integer, so please, in C++11 and higher use nullptr.
if(trav==NULL)
{
trav->value=no;
trav->next=NULL;
return;
}
You are trying to dereference a null pointer.
Remove that piece of code, and add the following to the beginning of addnode:
if(head == NULL)
{
head = new node;
head->value = no;
head->next = NULL;
return;
}
This also allows you to remove the trav == NULL from the while loop condition.
Also note that in main you have
head=new node;
head=NULL;
So you are allocating memory for a node, then you are dropping the only reference you have to that memory address.
You should remove the first of those, and you can replace the assignment of head to NULL to the definition of head (the line that reads node *head;, replace it with node *head = NULL;)
Lastly, if you have a c++11 compliant compiler, use nullptr instead of NULL.
It works well when I insert node at 0th position,but not when I insert somewhere in the middle .I have kept pointer prev to traverse till the previous node of target position and i have also checked if the node is to be inserted at the tail position. I have no idea why it is giving segmentation fault .Can anyone help me out in solving this problem?
code is:
Node* InsertNth(Node *head, int data, int position)
{
Node * temp=new Node;
int i=1;
temp->data=data;
temp->next=NULL;
Node * prev;
if(position==0)
{if(head==NULL)
head=temp;
else
{
temp->next=head;
head=temp;
}
}
else
{
prev=head;
while(i!=position)
{
i++;
prev=prev->next;
}
if(prev->next=NULL)
{
prev->next=temp;
}
else
{
temp->next=prev->next;
prev->next=temp;
}
}
return head;
}
Without a Minimal, Complete, and Verifiable example nobody can really help you. Nevertheless, this sticks out in a major way:
if(prev->next=NULL) {
prev->next=temp;
}
You assign NULL to prev->next instead of comparing it with ==.
I doubt below code :
while(i!=position)
{
i++;
prev=prev->next;
}
You should modify condition like this
while(prev && i!=position)
{
i++;
prev=prev->next;
}
Maybe asked million times before but I simply cannot understand what's wrong with this. I didn't want to use any code on the internet so I've just tried to program what's on my mind. Either this or my print function is wrong. Is there anything wrong with the code below?
void addNode(int value)
{
Node* newNode=new Node;
newNode->data=value;
if(root==NULL)
root=newNode;
else {
Node* temp=root,*parent;
while(temp!=NULL)
{
parent=temp;
if(temp->data == value)
return;
else if(temp->data < value)
temp=temp->left;
else
temp=temp->right;
}
temp=newNode;
}
}
temp=newNode;
This assigns the pointer to a local variable, which is discarded when the function returns, losing the new node. Instead, you want to assign it to a pointer within the tree; perhaps something like:
if (temp->data < value) { // If the new node should be to the left
if (temp->left) { // If there is a left subtree
temp = temp->left; // Move into the left subtree
} else { // Otherwise
temp->left = newNode; // Insert the new node there
return; // Done.
}
}
and likewise for temp->right if value < temp->data.
Also:
if (temp->data == value)
return;
You have a memory leak there; you should delete newNode before returning.