- 1

how to delete all negative nodes from singly liked list?

It is set of positive and negative numbers using linked list we have delete the all negative numbers from that list how i can write code or logic ?

30th Aug 2016, 3:39 PM
Rushabh Desarda
Rushabh Desarda - avatar
5 ответов
+ 4
But that is a singly listed list: just look at the struct, it has only one pointer to the next node. Also, the code does work when you actually build a list. I fixed my post so it compiles for people just copy/pasting, but otherwise, see here for a proper test: http://www.sololearn.com/app/cplusplus/playground/c3L7mCPF94GB/
30th Aug 2016, 9:37 PM
Zen
Zen - avatar
+ 2
I don't have the details of how your linked list is built, but this should be similar: struct node { int value; node* next; }; int main(void) { node* root = new node; root->value = 42; root->next = 0; //build your list here node* prev = 0; //previous node node* curr = root; //current node node* tmp; //used for deleting a node while (curr != 0) { if (curr->value < 0) { if (prev != 0) { prev->next = curr->next; } else { root = curr->next; } tmp = curr; curr = curr->next; delete tmp; } else { prev = curr; curr = curr->next; } } //do whatever you want with your list here return 0; }
30th Aug 2016, 5:11 PM
Zen
Zen - avatar
0
thank you zen, but I was tried with singly linked list not in doubly linked list. my task is to delete nodes with negative numbers from set of positive and negative numbers using singly linked list. it gives me error at middle of execution that is segmentation fault (core dumped ) not in compiled time error or run time error . help me for to remove that error and also give me logic to do newly.
30th Aug 2016, 5:40 PM
Rushabh Desarda
Rushabh Desarda - avatar
0
thank you brother it's working but it is predefined code I want user defined code, Zen can you give me your email id to send my code because it's too big.
31st Aug 2016, 3:20 AM
Rushabh Desarda
Rushabh Desarda - avatar
0
c program to delete all nodes from a header linked list that has negative values in its data part.
8th Oct 2020, 1:47 PM
A.LAVANYA,CSE(19-23) Vel Tech, Chennai
A.LAVANYA,CSE(19-23) Vel Tech, Chennai - avatar