C++ program count the occurrences of an element in the linked list without using recursion.

Count the number of occurrences of an element in a linked list without using recursion

In this article, we are going to see how to find no of occurrences of an element in a linked list without using recursion?
Submitted by Piyas Mukherjee, on January 11, 2019

Solution:

Input:

A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences.

Output:

The number of times key occurred in the list, [Count]

Data structure used:

Singly linked list where each node contains a data element say data and the address of the immediate next node say next, with Head holding the address of the first node.

Pseudo code:

Begin temp=Head Count = 0 while[temp != NULL] begin if[temp->data = key] count=count+1 endif temp=temp->link End while End

C program to Count the number of occurrences of an element in a linked list without using recursion

#include #include typedef struct list //linked list node { int data; struct list *next; }node; int main[] { node *head=NULL,*temp,*temp1; int choice,count=0,key; //building the linked list do { temp=[node *]malloc[sizeof[node]]; if[temp!=NULL] { printf["\nEnter the element in the list : "]; scanf["%d",&temp->data]; temp->next=NULL; if[head==NULL] { head=temp; } else { temp1=head; while[temp1->next!=NULL] { temp1=temp1->next; } temp1->next=temp; } } else { printf["\nMemory not avilable...node allocation is not possible"]; } printf["\nIf you wish to add m ore data on the list enter 1 : "]; scanf["%d",&choice]; }while[choice==1]; //finding occurence of key printf["\nEnter the data to find it's occurrence : "]; scanf["%d",&key]; temp=head; while[temp!=NULL] { if[temp->data==key] { count=count+1; } temp=temp->next; } printf["\n %d occurred %d times in the list",key,count]; return 0; }

Output

Enter the element in the list : 1 If you wish to add m ore data on the list enter 1 : 1 Enter the element in the list : 2 If you wish to add m ore data on the list enter 1 : 1 Enter the element in the list : 3 If you wish to add m ore data on the list enter 1 : 1 Enter the element in the list : 4 If you wish to add m ore data on the list enter 1 : 1 Enter the element in the list : 1 If you wish to add m ore data on the list enter 1 : 0 Enter the data to find it's occurrence : 1 1 occurred 2 times in the list
ADVERTISEMENT


Preparation


Aptitude Questions MCQs Find Output Programs

What's New

  • MongoDB MCQs
  • Java MCQs
  • C# MCQs
  • Scala MCQs
  • Blockchain MCQs
  • AutoCAD MCQs
  • PHP MCQs
  • 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
  • Generally Accepted Accounting Principles MCQs
  • Bills of Exchange MCQs
  • Business Environment MCQs
  • Sustainable Development MCQs
  • Marginal Costing and Absorption Costing MCQs
  • Globalisation MCQs
  • Indian Economy MCQs
  • Retained Earnings MCQs
  • Depreciation MCQs
  • Partnership MCQs
  • Sole Proprietorship 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

Write a function that counts the number of times a given int occurs in a Linked List

Given a singly linked list and a key, count the number of occurrences of the given key in the linked list. For example, if the given linked list is 1->2->1->2->1->3->1 and the given key is 1, then the output should be 4.

C++



// C++ program to count occurrences in a linked list
#include
using namespace std;
/* Link list node */
class Node
{
public:
int data;
Node* next;
};
/* Given a reference [pointer to pointer] to the head
of a list and an int, push a new node on the front
of the list. */
void push[Node** head_ref,int new_data]
{
/* allocate node */
Node* new_node =new Node[];
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = [*head_ref];
/* move the head to point to the new node */
[*head_ref] = new_node;
}
/* Counts the no. of occurences of a node
[search_for] in a linked list [head]*/
int count[Node* head,int search_for]
{
Node* current = head;
int count = 0;
while [current != NULL]
{
if [current->data == search_for]
count++;
current = current->next;
}
return count;
}
/* Drier program to test count function*/
int main[]
{
/* Start with the empty list */
Node* head = NULL;
/* Use push[] to construct below list
1->2->1->3->1 */
push[&head, 1];
push[&head, 3];
push[&head, 1];
push[&head, 2];
push[&head, 1];
/* Check the count function */
cout

Bài mới nhất

Chủ Đề