Circular Doubly Linked List - Segmentation fault: 11 [closed] - c++

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 6 years ago.
Improve this question
I'm trying to implement a circular doubly linked list but I keep getting a segmentation fault: 11 error (I believe it's because of the add and delete functions). I have no idea whether my code is even close, but I can't get past this error to test it properly. This is the code I have that I believe is involved:
(Circular_DLList.cc)
void Circular_DLList::add_to_tail(int a)
{
if (is_empty()) {
tail = new DLLNode(a);
tail->next = tail;
}
else {
tail->next = new DLLNode(a, tail->next);
}
}
int Circular_DLList::delete_from_tail()
{
if(!is_empty())
{
int a = tail->info;
tail = tail->prev;
tail->next = null;
return a;
}
else
{
tail = 0;
}
return a;
}
Any help would be fantastic, thanks.

One way to find a segmentation fault is by using cout statements throughout your code and compiling and running it. If the cout statement prints something to the console then the segmentation fault happens in a line after the cout statement. Keep doing this to narrow down and locate where the line with the segmentation fault is.

There is more than one problem in your code but here is one of them.
When you add the first element, you do:
tail = new DLLNode(a);
tail->next = tail;
so you leave prev equal to 0 (BTW: use nullptr instead of 0).
If you then delete the element you do:
int a = tail->info;
tail = tail->prev; // tail becomes 0
tail->next = null; // Dereference 0 cause seg fault
return a;
BTW: Your delete function should also delete the DLLNode ! Just changing pointer values isn't sufficient.
So this leads to 3 changes:
1) When adding new elements make sure to always set both nextand prev
2) Remember to delete the DLLNode created with new
3) In your delete function you need a special case for checking whether the list contains exactly one element, i.e. if (tail == tail->next) { .. delete last element .. set tail equal nullptr}

Related

I have created function to modify a node in Linkedlist in C++ but its not working: [closed]

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 2 years ago.
Improve this question
This Function is Created in LinkedList to modify a node at a given position. But, this function is not working properly and it is giving some random values.
void update_data(int old, int new_data) {//Function toupdate node
Node *curr=header;//Data members
int pos = 0;
while(curr->next!=NULL) {
if(curr->isbn == old)
{
curr->isbn = new_data;
cout<<old<<" Found at position "<<pos<<" Replaced with "<<new_data<<endl;;
}
curr = curr->next;
pos++;
}
}
For starters the variable pos is not used within the function.
Secondly the condition of the while loop
while(curr->next!=NULL) {
is incorrect and in general can invoke undefined behavior because the pointer header can be equal to nullptr. And moreover if the list contains only one node pointed to by the pointer header and its data member isbn is equal to the value of the variable old it will not be changed.
The function should not output any message.
The function can look the following way
void update_data( int old, int new_data )
{//Function toupdate node
for ( Node *curr = header; curr != nullptr; curr = curr->next )
{
if ( curr->isbn == old )
{
curr->isbn = new_data;
}
}
}

Cycle in Merge K-Sorted LinkedLists [closed]

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
My merge k-sorted lists algorithm uses divide and conquer and utilizes merge 2 lists algorithm as a helper in the process.
The issue lies is during an iteration a cycle is created and I can't figure out why.
I traced down the code to the exact place where this happen, but I am still unable to discern the issue.
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2, bool debug=false)
{
ListNode *head = new ListNode(-1);
ListNode *curr = head;
while(l1 && l2)
{
if(l1->val <= l2->val)
{
curr->next = l1;
l1 = l1->next;
}
else
{
curr->next = l2;
l2 = l2->next;
}
curr = curr->next;
}
// some list may be still populated
l1 != NULL ? curr->next = l1 : curr->next = l2;
return head->next;
}
ListNode* mergeKLists(std::vector<ListNode*>& lists)
{
// approach of divide and conquer
int size = lists.size();
int interval = 1;
int tmp_val = 1;
bool debug= false;
while(interval < size)
{
for(int i=0; i<size-interval; i*=2)
{
lists[i] = mergeTwoLists(lists[i], lists[i+interval], debug=debug);
if (i==0)
i++;
}
interval*=2;
}
if (size)
return lists[0];
else
{
ListNode* ret=NULL;
return ret;
}
}
};
For some reason this input [[-10,-9,-9,-3,-1,-1,0],[-5],[4],[-8],[],[-9,-6,-5,-4,-2,2,3],[-3,-3,-2,-1,0]]
provokes an infinite loop.
I get an infinite loop in the second list parameter to the sort 2 lists algorithm.
I believe it happens in some iteration in the lines of code:
curr->next = l2;
l2 = l2->next;
Can somebody give me any hints?
It appears that your mergeTwoLists modifies the two lists passed to it, in such a way that they can come out of it sharing nodes. This would not be a problem (at least not a big problem) if you made sure to set one of them aside and never use it again.
Clearly that's what you intend with the index-juggling in mergeKLists, but there's a bug: you increase i incorrectly. You reuse a list you shouldn't, call mergeTwoLists on two lists that share a node, it creates a loop in the list and iterates forever.
The quick-and-dirty solution is to fix the index arithmetic in mergeKLists. The deeper solution is to be more careful with the pointers in mergeTwoLists so that two lists that go in disjoint come out disjoint.

Insert in Binary Serach Tree Unhandled exception thrown [closed]

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 4 years ago.
Improve this question
I have a function to insert nodes in a binary search tree. I get a crash when I try to insert a node. I use the debugger from VS and it tells me Unhandled exception thrown: write access violation.
m_father was nullptr.
Here is my function to insert:
NOD *INSERT(NOD k)
{
NOD *temp = new NOD(k);
NOD *m_father = NULL;
NOD *x = root;
while (x != NULL)
{
m_father = x;
if (m_father->m_key > x->m_key)
{
x = x->m_right_child;
}
x = x->m_left_child;
}
if (root == NULL)
root = temp;
else if (temp->m_right_child->m_key > m_father->m_key)
{
m_father->m_right_child = temp;
}
m_father->m_left_child = temp;
temp->m_father = m_father;
return 0;
}
And here is how i try to insert a node:
int temp_nod;
cin >> temp_nod;
binary_tree.INSERT(temp_nod);
else if (temp->m_right_child->m_key > m_father->m_key)
temp is copy-constructed here (is it the expected behaviour?). If it's created from a new node, m_right_child might not be assigned yet, so you try to dereference a nullptr.
I'm not sure, but you did you want to check temp key here? I don't see much reason to check child key when choosing the correct place in BST.
Also, as noted in the comments, you assign m_father->left_child always, without a condition. This happens even in an empty list, so again we try to dereference a nullptr. I suppose it should look more like this:
if (root == NULL)
root = temp;
else if (temp->m_right_child->m_key > m_father->m_key)
{
m_father->m_right_child = temp;
temp->m_father = m_father;
}
else
{
m_father->m_left_child = temp;
temp->m_father = m_father;
}
As a side note, if your function does not return anything useful, just make it void.
If root == NULL the only assignment to m_father would be NOD *m_father = NULL;, before the statement m_father->m_left_child = temp;.
EDIT: (Because if root == NULL, then x would also be NULL, so the while loop would not be executed)
EDIT2: else if (temp->m_right_child->m_key > m_father->m_key) => m_father->m_left_child = temp; - the former will not be evaluated if root == NULL, since it is an else if to an if that was true.

Remove node at the TAIL Linked List C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
All, I'm creating a function that would remove the tail from a linked list. And My function works fine for just one iteration but it doesn't for subsequent ones.
Could anybody shed some light?
Thanks
int List::removeAtTail(){
if(head == NULL)
{
cout << "Node cannot be deleted from an empty linkedList" << endl;
}
if(curr->next= NULL)
{
curr->next=curr;
}
return 0;
}
And also, if I would like to return the element that I deleted, how would I go around at doing that?
There are many ways this can be done, following is one:
int List::removeAtTail(){
while(curr != NULL) {
if(curr->next == NULL) { // depending on your implementation you might use Tail
int temp = *curr;
delete curr;
return temp;
}
curr = curr->next;
}
return 0;
}
Notice how we iterate through the list until we find the last item. At that point we store it in a temporary variable before freeing the memory. Lastly we return the value stored in the temporary variable.

Splitting linked list so many times puts into stack overflow c++

Oh dear; I seem to have misthought this.
I would like to split a singly-linked list 10,000 times, but evidently (and I didn't know this before you guys helped me) it causes a stack overflow.
I'm really new to this, so is there any way I could still do this and not cause a stack overflow? Using references or something?
Here's the method:
Node* Node::Split()
{
if(next == NULL)
{
return this;
}
Node *newNode = this->next;
if(this->next != NULL)
{
this->next = newNode->next;
}
if(newNode->next != NULL)
{
newNode->next = newNode->next->Split();
}
return newNode;
}
You'll have to write this as a loop rather than a recursive call. Keep track of your position in the original list, and both ends of the new lists, and append nodes alternately to each list.
Make sure your recursion does stop at some point (try a small data set). If it does then you have no problems there and the next thing to do is ask your compiler to increase the stack size for you. The default is quite small (I think it is one megabyte on vc++ 10).