Trusted answers to developer questions

Educative Answers Team

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.

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.

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++.

To print the odd numbers in data you need to:

- Traverse the whole linked list.
- 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.
- 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(); };

To print the odd number nodes you need to:

- Traverse the whole linked list.
- Maintain a counter to print odd nodes.
- At each node check if the counter gives $0$ as the remainder when it is divided by $2$. If it does, print it.
- 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

Related Courses