return middle node of linked list in c
i am trying to return a middle node of linked-list, it works well when number of nodes are even and when number of nodes are odd, segmentation fault is all i see. initially i return slow if fast reached NULL, but it still fails for odd number of nodes.
i tried two conditions: 1. if fast reached NULL AND length of linked-list is even: return slow
2. if fast reach NULL AND length of linked-list is odd: return slow->next. i also run gdb which points the seg fault to: fast = fast->next->next; in the middle_node(node *) function. Appreciate.
unexpected output from suspected pointer arithmetic error
I am trying to build a linked-list in C++ from scratch. Consider the following commented class declaration.
Modifying a Linked List Head Node Without Pointer Dereferencing in C
void myLinkedListAddAtHead(MyLinkedList* obj, int val) { // Function body } The problem arises when I try to link the new node to the head of the linked list. Although I’m updating the obj pointer within the function to point to the new head node, it seems that the change isn’t reflected in the actual linked […]
Is there a cleaner way to write this C++ code?
I’m learning C++, coming from a dynamically typed language background, and implementing algorithms in C++ to do this learning. My current (sub) project is a function that inserts an element into a sorted (double) linked list. The implementation is a collection of Nodes. Each node contains the data in the list element and a pointer to the previous and next element of the list (or nullptr
for the first and last elements.) List elements are accessed by following the pointers, starting from a reference to the head or tail.