-1

Everyone. I am Kartik. I am new to all the programming scene. So I am facing some silly issues.

So here is the problem.

I was supposed to the separate even and odd numbers in a linked list. more specifically to put all the even elements at last after odd elements.

#include <bits/stdc++.h>
using namespace std;

class node {
public:
    int data;
    node *next;
    node *prev;

    node(int d) {
        data = d;
        next = NULL;
    }
};

node *first = NULL;
node *second = NULL;

void create(int arr[], int n) {
    node *t;
    node *last;
    first = new node(arr[0]);
    last = first;

    for (int i = 1; i < n; i++) {
        node *t = new node(arr[i]);
        last->next = t;
        last = t;
    }
}
void EvenafterOdd(node *&first, node *&second) {
    node *temp = first;
    node *temp1 = second;
    node *t;
    node *last;
    while (temp) {
        int x = temp->data;
        if (second == NULL) {
            second = new node(x);
            last = second;
            // second = last;
        } else if (x % 2 == 0) {
            node *t = new node(x);
            last->next = t;
            t = last;
        } else if (x % 2 == 1) {
            node *t = new node(x);
            t->next = second;
            second = t;
        }
    }

}


void Rprint(node *&head) {
    node* temp = head;
    if (temp == NULL) {
        return;
    }
    cout << temp->data << " ";
    Rprint(temp->next);
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    create(arr, n);
    Rprint(first);
    cout<<endl;
    EvenafterOdd(first, second);
    Rprint(first);
    cout<<endl;
    Rprint(second);



    return 0;
}

So for the input 1 2 2 2 1.

The expected output is 1 1 2 2 2.

But in my code. the problem is the program just gets stuck after printing the first list. and its actually supposed to print the second list in the proper expected out.

Thanks in advance for everone who helps.

Andro Tech
  • 13
  • 4
  • 1
    I see one thing that might bite you: Your `node` class doesn't init its two pointers. Also, init using the so-called "initializer list", not using assignment in the constructor body. – Ulrich Eckhardt Jan 28 '20 at 18:26
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Jan 28 '20 at 18:27
  • `#include using namespace std;` - *Never* do that. *Especially* the first bit. – Jesper Juhl Jan 28 '20 at 18:29
  • Don't use `NULL` in new code. Use `nullptr`. – Jesper Juhl Jan 28 '20 at 18:29
  • `int arr[n];` - Variable Length Arrays are *not* a standard C++ feature. They are a compiler *extension* provided by *some* compilers. Don't rely on/use them. Use `std::vector` instead. – Jesper Juhl Jan 28 '20 at 18:32
  • I won't comment on more now, but suffice to say that this code has numerous issues - some I pointed out, some I didn't. That it crashes is no surprise. – Jesper Juhl Jan 28 '20 at 18:33
  • @JesperJuhl thanks for all the points. as i said in my post, im very new in the programming and thats why i have numerous mistakes. but i would seriously appreciate all the support. Again thanks sir. – Andro Tech Jan 28 '20 at 18:49

1 Answers1

0

so i worked on my problem and came with the solution. i missed a few things in the code. and eventually after adding them, the code works fine. i am adding a comment after the lines i changed or added.

#include <bits/stdc++.h>
    using namespace std;

    class node {
    public:
        int data;
        node *next;
        // node *prev;

        node(int d) {
            data = d;
            next = NULL;
        }
    };

    node *first = NULL;
    node *second = NULL;

    void create(int arr[], int n) {
        node *t;
        node *last;
        first = new node(arr[0]);
        last = first;

        for (int i = 1; i < n; i++) {
            node *t = new node(arr[i]);
            last->next = t;
            last = t;
        }
    }
    void EvenafterOdd(node *&first, node *&second) {
        node *temp = first;
        node *temp1 = second;
        node *t;
        node *last;
        while (temp) {
            int x = temp->data;
            if (second == NULL) {
                second = new node(x);
                last = second;
            } else if (x % 2 == 0) {
                node *t = new node(x);
                last->next = t;
                last = t; //  earlier i wrote this in reverse i.e. t = last.
            } else if (x % 2 == 1) {
                node *t = new node(x);
                t->next = second;
                second = t;
            }
            temp = temp->next; //most stupid mistake actually. i didnt had this line written before
        }

    }


    void Rprint(node *&head) {
        node* temp = head;
        if (temp == NULL) {
            return;
        }
        cout << temp->data << " ";
        Rprint(temp->next);
    }

    int main(int argc, char const *argv[])
    {
        int n;
        cin >> n;
        int arr[n] = {1, 2, 2, 2, 1};
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        create(arr, n);
        Rprint(first);
        cout << endl;
        EvenafterOdd(first, second);
        Rprint(first);
        cout << endl;
        Rprint(second);



        return 0;
    }
Andro Tech
  • 13
  • 4