hI GUYS c++ trying to delete nodes with the given value. i write this code and c
ID: 3731397 • Letter: H
Question
hI GUYS
c++
trying to delete nodes with the given value. i write this code and compile perfectly, however, i do get a seg fault somehow. I couldnt think of any bugs in this code. Plese take a look and tell me whats wrong
btw lets say linkedlist is 3-3-2-3-null after i input the list and 3, the list should be 2-null;
void deleteNode Iteratively (LinkedListList, int value){ Node next: Node *p List->head; Node prev = NULL; while (List->head->data value) p = List->head->next ; delete List->head List->head = p; //if the head node has the same value, delete them and set a new head assert (List->head !=NULL) ; while (p.= NULL){ // start iterate if (p->data value) next = p->next ; delete p: p = next; if (p NULL) List->ta!! //reset tall = prev; else prev = p; // point to the previous node, incase of deleting the tail p->next ;Explanation / Answer
There are two problems in your code.
1. You have to reinitalize List->head in this section
while(List->head->data == value) {
p = List->head->next;
delete List->head;
List->head = p; // Re initailize List head
}
2. When your are deleting a node in the middle, you have to connect previous node to the next node after deleting
while(p!=NULL) {
if (p->data == value) {
next = p->next;
delete p;
if (prev != NULL) prev->next = next; // Connect prev to next node after deleting current one.
p = next;
if (p == NULL) List->tail = prev;
} else {
prev = p;
p = p->next;
}
}
Here is complete function
void deleteNodeIteratively(LinkedList *List, int value) {
Node * next;
Node *p = List->head;
Node * prev = NULL;
while(List->head->data == value) {
p = List->head->next;
delete List->head;
List->head = p; // Re initailize List head
}
assert(List->head!=NULL);
while(p!=NULL) {
if (p->data == value) {
next = p->next;
delete p;
if (prev != NULL) prev->next = next; // Connect prev to next node after deleting current one.
p = next;
if (p == NULL) List->tail = prev;
} else {
prev = p;
p = p->next;
}
}
}
And a complete test file
#include <iostream>
#include <assert.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkedList
{
private:
public:
Node *head,*tail;
LinkedList()
{
head = NULL;
tail = NULL;
}
void display()
{
Node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void add_node(int n)
{
Node *tmp = new Node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
};
void deleteNodeIteratively(LinkedList *List, int value) {
Node * next;
Node *p = List->head;
Node * prev = NULL;
while(List->head->data == value) {
p = List->head->next;
delete List->head;
List->head = p; // Re initailize List head
}
assert(List->head!=NULL);
while(p!=NULL) {
if (p->data == value) {
next = p->next;
delete p;
if (prev != NULL) prev->next = next; // Connect prev to next node after deleting current one.
p = next;
if (p == NULL) List->tail = prev;
} else {
prev = p;
p = p->next;
}
}
}
int main()
{
LinkedList *list = new LinkedList;
list->add_node(3);
list->add_node(3);
list->add_node(2);
list->add_node(3);
list->add_node(5);
list->add_node(6);
list->add_node(3);
list->display();
deleteNodeIteratively(list, 3);
cout<<" ";
list->display();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.