-
Notifications
You must be signed in to change notification settings - Fork 0
/
endnode.java
74 lines (65 loc) · 1.33 KB
/
endnode.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
//fetcing the nth node from the end
public class Endnode
{
Node head;
static class Node
{
Node next;
int data;
Node(int d)
{
data=d;
next=null;
}
}
//function to fetch the nth node
public void noden(int n)
{
int len=0;
Node temp=head;
//counting number of nodes in the linked list
while(temp!=null)
{
temp=temp.next;
len++;
}
if(len<n)
{
return;
}
temp=head;
for(int i=1;i<len-n+1;i++)
{
temp=temp.next;
}
System.out.print("The data is "+ temp.data);
}
public void insert(int element)
{
Node n = new Node(element);
n.next=head;
head=n;
}
void print()
{
Node no=head;
while(no!=null)
{
System.out.print(no.data+" ");
no=no.next;
}
System.out.println();
}
public static void main(String args[])
{
Endnode en = new Endnode();
en.insert(21);
en.insert(34);
en.insert(67);
en.insert(68);
en.insert(98);
en.insert(56);
en.print();
en.noden(2);
}
}