Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linked list
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()
{
//variable to traverse linked list
node* dummy = head;

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
LinkedList.cpp
LinkedList.h
struct node
{
int data;
node* next;
};

class LinkedList
{
private:
node* head;

public:

//Constructor
LinkedList();

//Add element
bool insertAtTail(int element);

//Display linked list
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;

//variable to traverse linked list
node* dummy = head;

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
LinkedList.cpp
LinkedList.h
#include "LinkedList.h"

LinkedList::LinkedList()
{
head = NULL;
}

bool LinkedList::insertAtTail(int element)
{
node* newNode = new node;
newNode->next = NULL;
newNode->data = element;

if(head == NULL)
{
head = newNode;
}
else
{
node* tail = head;
while(tail->next != NULL)
{
tail = tail->next;
}
tail->next = newNode;
}
}

void LinkedList::display()
{
//variable to traverse linked list
node* dummy = head;

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

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

//variable to traverse linked list
node* dummy = head;

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

linked list
print
cout
Copyright Â©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright Â©2022 Educative, Inc. All rights reserved.