Program to print elements of linked list in C++

Home » C/C++ Data Structure Programs

C program to print the even elements of the linked list

Here, we are going to learn how to print the even elements of the linked list using C program?
Submitted by Nidhi, on August 23, 2021

Problem Solution:

Given a singly linked list, we have to print the even elements.

Program:

The source code to print the EVEN elements of the linked list is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to print the even elements // of linked list #include #include //Self-referential structure to create the node. typedef struct tmp { int item; struct tmp* next; } Node; //structure for creating the linked list. typedef struct { Node* head; Node* tail; } List; //Initialize List void initList[List* lp] { lp->head = NULL; lp->tail = NULL; } //Create node and return the reference of it. Node* createNode[int item] { Node* nNode; nNode = [Node*]malloc[sizeof[Node]]; nNode->item = item; nNode->next = NULL; return nNode; } //Add a new item at the end of the list. void addAtTail[List* lp, int item] { Node* node; node = createNode[item]; //if list is empty. if [lp->head == NULL] { lp->head = node; lp->tail = node; } else { lp->tail->next = node; lp->tail = lp->tail->next; } } //Add a new item at the beginning of the list. void addAtHead[List* lp, int item] { Node* node; node = createNode[item]; //if list is empty. if [lp->head == NULL] { lp->head = node; lp->tail = node; } else { node->next = lp->head; lp->head = node; } } //To print the list from start to end of the list. void printList[List* lp] { Node* node; if [lp->head == NULL] { printf["\nEmpty List"]; return; } node = lp->head; while [node != NULL] { printf["| %05d |", node->item]; node = node->next; if [node != NULL] printf["--->"]; } printf["\n\n"]; } void printEvens[List* lp] { Node* temp; temp = lp->head; while [temp != NULL] { if [temp->item % 2 == 0] printf["%d ", temp->item]; temp = temp->next; } } //Main function to execute program. int main[] { List* lp; lp = [List*]malloc[sizeof[List]]; initList[lp]; addAtHead[lp, 101]; addAtHead[lp, 102]; addAtHead[lp, 103]; addAtHead[lp, 104]; addAtHead[lp, 105]; printf["List:\n"]; printList[lp]; printf["Even elements: \n"]; printEvens[lp]; printf["\n"]; return 0; }

Output:

List: | 00105 |--->| 00104 |--->| 00103 |--->| 00102 |--->| 00101 | Even elements: 104 102

Explanation:

Here, we created a self-referential structure to implement a linked list, a function to add a node at the start and end of the list, a function printEvens[] to print elements of the linked list.

In the main[] function, we created a linked list. Then we printed the even elements of the linked list on the console screen.

ADVERTISEMENT


Preparation


Aptitude Questions MCQs Find Output Programs

What's New

  • JavaScript MCQs
  • jQuery MCQs
  • ReactJS MCQs
  • AngularJS MCQs
  • JSON MCQs
  • Ajax MCQs
  • SASS MCQs
  • HTML MCQs
  • Advanced CSS MCQs
  • CSS MCQs
  • PL/SQL MCQs
  • SQL MCQs
  • MS Word MCQs
  • PL/SQL MCQs
  • SQL MCQs
  • Software Engineering MCQs
  • MIS MCQs
  • Goods and Services Tax [GST] MCQs
  • Cooperative Society MCQs
  • Capital Market MCQs
  • Business Studies MCQs
  • Basic Accounting MCQs
  • MIS Executive Interview Questions
  • Go Language Interview Questions
ADVERTISEMENT

Top Interview Coding Problems/Challenges!

  • Run-length encoding [find/print frequency of letters in a string]
  • Sort an array of 0's, 1's and 2's in linear time complexity
  • Checking Anagrams [check whether two string is anagrams or not]
  • Relative sorting algorithm
  • Finding subarray with given sum
  • Find the level in a binary tree with given sum K
  • Check whether a Binary Tree is BST [Binary Search Tree] or not
  • 1[0]1 Pattern Count
  • Capitalize first and last letter of each word in a line
  • Print vertical sum of a binary tree
  • Print Boundary Sum of a Binary Tree
  • Reverse a single linked list
  • Greedy Strategy to solve major algorithm problems
  • Job sequencing problem
  • Root to leaf Path Sum
  • Exit Point in a Matrix
  • Find length of loop in a linked list
  • Toppers of Class
  • Print All Nodes that don't have Sibling
  • Transform to Sum Tree
  • Shortest Source to Destination Path

Comments and Discussions!



Please enable JavaScript to view the comments powered by Disqus.
ADVERTISEMENT

Video liên quan

Chủ Đề