-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountnodes1.java
More file actions
48 lines (44 loc) · 943 Bytes
/
countnodes1.java
File metadata and controls
48 lines (44 loc) · 943 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
//counting nodes by iterative approach
public class Iternodes
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
public void insert(int element)
{
Node n = new Node(element);
n.next=head;
head=n;
}
public int count()
{
Node temp=head;
int c=0; //initializing count variable
while(temp!=null)
{
c=c+1;
temp=temp.next;
}
return c;
}
public static void main(String args[])
{
Iternodes in = new Iternodes();
in.insert(98);
in.insert(54);
in.insert(46);
in.insert(64);
in.insert(74);
in.insert(63);
in.insert(9);
System.out.println("The number of nodes in the linked list are: "+ in.count());
}
}