-
Notifications
You must be signed in to change notification settings - Fork 342
/
FrequencyOfNumberInLinkedList.cpp
63 lines (49 loc) · 1.12 KB
/
FrequencyOfNumberInLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// C++ program to count occurrences in a linked list
#include <bits/stdc++.h>
#include <iostream>
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 occurrences 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;
}
int main()
{
/* Start with the empty list */
Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
cout << "count of 1 is " << count(head, 1);
return 0;
}