0
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node* prev;
    Node(int d)
    {
        data = d;
    }
};

Node* insertEnd(Node* head, int data)
{
    Node* node = new Node(data);
    Node* last = head;
    node->next = NULL; // link new node to NULL as it is last node
    if (head == NULL) // if list is empty add in beginning.
    {
        head = node;
        node->prev = NULL;
        return head;
    }
    while (last->next != NULL) // Find the last node
        last = last->next;
    last->next = node; // Add the node after the last node of list
    node->prev = last;
    return head;
}

// This function prints contents of linked list starting from head
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << ' ';
        node = node->next;
    }
}

Node* rearrangeList(Node* head)
{
    Node* newN = NULL;
    Node* odd = head;
    Node* even = head->next;
    while (even != NULL) {
        insertEnd(newN, even->data);
        if (even->next == NULL) {
            break;
        }
        even = even->next->next;
    }
    while (odd != NULL) {
        insertEnd(newN, odd->data);
        if (odd->next == NULL) {
            break;
        }
        odd = odd->next->next;
    }
    return newN;
}

int main()
{
    int t;
    cin >> t;
    while (t--) {
        Node* head = NULL;
        int n, m, x, y;
        cin >> n;
        while (n--) {
            cin >> m;
            head = insertEnd(head, m);
        }
        head = rearrangeList(head);
        printList(head);
        cout << endl;
    }
    return 0;
}

In this code we have to arrange the even position nodes in front and odd position nodes at end.

Whenever I try to run it I get a segmentation fault(on Code quotient) (on online gdb it gives no output).

Sample Input

1

7

1 -> 3 -> 5 -> 7 -> 9 -> 11 -> 13

Sample Output

3 7 11 1 5 9 13 Can anyone tell me where I am messing up in this problem .Thanks For the help!

drescherjm
  • 9,653
  • 5
  • 43
  • 62
Geek
  • 59
  • 8
  • `Node(int d) { data = d; }` -- You should initialize the `next` and `prev` pointers to `nullptr` also. – PaulMcKenzie Sep 09 '21 at 15:43
  • *Can anyone tell me where I am messing up in this problem* -- [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [what is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – PaulMcKenzie Sep 09 '21 at 15:46
  • Don't just run code on onlinegdb. Use onlinegdb to step through the code line by line and watch what the program does as it does it. Keep an eye on the variables in use. As soon as you see something you don't expect, stop and figure out why you have the difference because if that difference isn't a bug, it means your expectations are wrong. Either way, you need to fix the problem. – user4581301 Sep 09 '21 at 15:48
  • Thanks @PaulMcKenzie and I will try it on debugger – Geek Sep 09 '21 at 17:02
  • Thanks @user4581301 – Geek Sep 09 '21 at 17:03
  • 2
    Not a problem. Proper use of a debugger is something that never seems to be covered in general programming books or courses. Personally I think it's a vital skill because a debugger pulls off all the blinders. If you made a bad assumption writing the code and proceed to debug the code on paper or in your head, odds are really good you're going to proceed from the same bad assumption and not be able to find the mistake. The debugger won't make the same assumption. It doesn't give a what you think. It shows you what is. Then you have to figure out WHY it is. – user4581301 Sep 09 '21 at 17:29
  • 1
    Another thing that will speed up figuring out the issue -- hardcode the input data that gives the wrong results directly into the source code of the program. Right now, you have `cin` statements, which means you have to sit at the keyboard every time you run the program, typing in the data. Instead: `int n = 7; int test[] = {1,3,5,7,9,11,13}`, then loop on `test` populating the linked list, i.e. `for (int i =0; i < n; ++i) head = insertEnd(head, test[i])`. Forget about `t` -- that is not important. – PaulMcKenzie Sep 09 '21 at 19:54
  • @PaulMcKenzie the platform I am using only allows to change the "rearrangeList" function everything else in the code has been provided from the platform and cannot be changed – Geek Sep 10 '21 at 03:27
  • @Geek -- Then get yourself one of the many free C++ compilers, install it, and learn to debug that way. Relying on a website to compile code and run code is not the way to learn a complex language such as C++. You should have been able to take the code you posted, compile it locally, run it locally in a debugger. – PaulMcKenzie Sep 10 '21 at 03:54

0 Answers0