How to display odd and even numbers from a queue link list?
hello everyone i really need some help right now on this program. what this program is supposed to do is copy 11 22 44 77 33 99 66 into a linked list that behaves like a queue. the program should display the queue, display the odd numbers, display the even numbers.

display queue should look like this 11 22 44 77 33 99 66
display odd numbers should look like this 11 77 33 99
display even numbers should look like this 22 44 66

so far i can only display the queue with the program i have so far and odd and even numbers do not show up at all. here is my program would really appreciate the help. thanks to whomever can help me. also the way i got the queue link list to display is there a better way to do it or is it ok how i did it.

#include <iostream>
using namespace std;

struct NODE
{
int info;
NODE *next;
};
NODE *Queue;
NODE *t;
NODE *rear;

int main()
{
//ask user to enter numbers in Stack link list.
Queue=rear=NULL;
for(int i=0; i<=6; ++i)
{
t=new(NODE);
cout<<"Enter a number:"; cin>>t->info;
t->next=Queue;
if(rear==NULL)
{
Queue=rear=t;
}
else
{
rear->next=t;
rear=t;
}
}
//displays the nodes in the Queue link list.
t=Queue;
for(int i=0; i<=6; ++i)
{
int x;
x=Queue->info;
Queue=Queue->next;
cout<<x<<'\t';
}
cout<<endl;

//finds and displays the odd numbers.
t=Queue;
while(t!=NULL)
{
if(t->info%2!=0)
t=t->next;
}
cout<<t<<'\t';

//finds and displays the even numbers.
t=Queue;
int EvenCounter=0;
while(t!=NULL)
{
if(t->info%2==0)
t=t->next;
}
cout<<t<<endl;

return 0;
}

Recommended Answers

All 3 Replies

replace lines 43-60 with this:

//finds and displays the odd numbers.
t=Queue;
while(t!=NULL)
{
if(t->info%2!=0)
  cout<<t->info<<'\t';
t=t->next;
}
cout<<endl;

//finds and displays the even numbers.
t=Queue;
int EvenCounter=0;
while(t!=NULL)
{
if(t->info%2==0)
  cout<<t->info<<'\t';
t=t->next;
}
cout<<endl;

i replace it but it keeps on repeating some numbers and i does not stop running.

I think it is because next member is not initialised in your struct. That's why the while loop never exits.

There are two possible solution for this problem depending how far they tought you. If they tought you the constructors and initalisation lists you can create a constructor for struct NODE like this:

struct NODE
{
NODE():
info(0),
next(NULL)
{;}
int info;
NODE *next;
};

Otherwise, you will need to set next for the last element to NULL manually in the code when you asked the user for all elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.