Related Tags

print
cout

# How to print all odd numbers in a linked list Educative Answers Team ## What is a linked list?

A linked list is a common data structure made of a chain of nodes in which each node contains a value and a pointer to the next node in the chain.

The head pointer points to the first node, and the last element of the list points to null. When the list is empty, the head pointer points to null.

## How to print odd numbers

Before implementing the logic of printing the odd numbers, we need to devise a plan to identify them.

We all know that all even numbers are divisible by $2$; therefore, they yield a remainder of $0$. We can further this to realize that odd numbers, when divided by $2$, will produce a remainder of $1$.

This difference of remainders can be exploited to differentiate between odd and even groups of numbers.

## Modulo operator

The modulo operator helps us generate the remainder when one number is divided by another. Here is a list of modulo operators in different languages. For the rest of this article, we will use %, the modulo operator for C++.

## Printing odd numbers in data

To print the odd numbers in data you need to:

1. Traverse the whole linked list.
2. At each node, check if the data of that node gives $0$ as the remainder when it is divided by $2$. If it does, print it.
3. Move to the next node.

Here is a sample code:

void LinkedList::displayOdd()
{

do
{
int tempData = dummy->data;
if (tempData % 2 == 1)
{
std::cout << "data: " << tempData << endl;
}
dummy = dummy->next;
} while (dummy != NULL);
}


Run the code cell below to observe the results.

main.cpp
struct node
{
int data;
node* next;
};

{
private:

public:

//Constructor

bool insertAtTail(int element);

void display();

//Display odd data
void displayOddData();

};

## Printing odd number nodes

To print the odd number nodes you need to:

1. Traverse the whole linked list.
2. Maintain a counter to print odd nodes.
3. At each node check if the counter gives $0$ as the remainder when it is divided by $2$. If it does, print it.
4. Increment the counter and move to the next node.

Here is a sample code:

void LinkedList::displayOddNodes()
{
// mainting a counter
int counter = 0;

do
{
//condition to check odd
if (counter % 2 == 1)
{
std::cout << "data: " << dummy->data << endl;
}

//incrementing counter
counter++;
//jumping to next node
dummy = dummy->next;
} while (dummy != NULL);
}


Run the code cell below to observe the results.

main.cpp
#include "LinkedList.h"

{
}

{
node* newNode = new node;
newNode->next = NULL;
newNode->data = element;

{
}
else
{
while(tail->next != NULL)
{
tail = tail->next;
}
tail->next = newNode;
}
}

{

do
{
std::cout << "data: " << dummy->data << endl;
dummy = dummy->next;
} while (dummy != NULL);
}

{
// mainting a counter
int counter = 0;

do
{
//condition to check odd
if (counter % 2 == 1)
{
std::cout << "data: " << dummy->data << endl;
}

//incrementing counter
counter++;
//jumping to next node
dummy = dummy->next;
} while (dummy != NULL);
}

RELATED TAGS

print
cout 