-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeycount.java
More file actions
51 lines (49 loc) · 833 Bytes
/
Keycount.java
File metadata and controls
51 lines (49 loc) · 833 Bytes
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
//counting the number of keys in the linked list
class Keycount
{ Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d; //using this pointer
next=null;
}
}
public void insert(int element)
{
Node n = new Node(element);
n.next=head;
head=n;
}
int countkey(int key)
{
Node present=head;
int c=0; //initializing variable for count
while(present!=null)
{
if(present.data==key)
{
c++;
}
present=present.next;
}
return c;
}
public static void main(String args[])
{
Keycount kn = new Keycount();
kn.insert(100);
kn.insert(99);
kn.insert(98);
kn.insert(97);
kn.insert(100);
kn.insert(99);
kn.insert(100);
kn.insert(96);
kn.insert(100);
kn.insert(98);
System.out.println("Count of 100 in the key list is "+kn.countkey(100));
}
}