My code does not display the output . This is my question
Given a list, modify it such that all odd elements appear before the even ones. The order of odd elements and even shall remain intact.
Input Format The first line contains an integer N, the number of elements in the list.
The next line contains N space separated integral elements of the list
Sample Input 5 1 2 3 4 5
Sample Output 1 3 5 2 4
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node (int d)
{
data = d;
next = NULL;
}
};
void insert(node *&head, node*&tail ,int data)
{
if (head == NULL)
{
head = tail = new node (data);
return ;
}
else
{
node* n = new node(data);
tail->next = n;
tail = n;
}
}
void seg(node *&head)
{
node *current = head;
node *estart = NULL;
node *Eend = NULL;
node *ostart = NULL;
node *oend = NULL;
while (current != NULL)
{
int value = current->data;
if (value % 2 == 0)
{
if (estart == NULL)
{
estart = Eend = current;
}
else
{
Eend->next = current;
current = Eend;
}
}
else
{
if (ostart == NULL)
{
ostart = oend = current;
}
else
{
oend->next = current;
current = oend;
}
}
current = current->next;
}
if (ostart != NULL)
{
head = ostart;
oend->next = estart;
Eend->next = NULL;
}
else
{
head = estart;
Eend->next = NULL;
}
}
void print(node *head)
{
while (head != NULL)
{
cout << head->data;
head = head->next;
}
cout << endl;
}
int main()
{
node *head = NULL, *tail = NULL;
int n;
cin >> n;
int m;
for (int i = 1; i <= n; i++)
{
cin >> m;
insert(head, tail, m);
}
seg(head);
print(head);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire