-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
54 lines (46 loc) · 1 KB
/
Link.java
File metadata and controls
54 lines (46 loc) · 1 KB
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
class Node {
int data;
Node next;
public Node(int data){
/*initializing enetred value as data and
the next value as null */
this.data=data;
this.next=null;
}
}
class LinkedList {
Node head;
public LinkedList(){
/*initializing head as null*/
this.head=null;
}
public void append(int data){
/*if head is null then the list is empty and therefore
the entered value is added as an element else the value is added
as the next element in the list */
Node newnode=new Node(data);
if(head==null)
head=newnode;
else
{
Node temp;
for(temp = this.head;temp.next != null;temp = temp.next);
temp.next=newnode;
}
}
public void print(){
Node snode;
for(snode = this.head;snode != null;snode = snode.next){
System.out.print(snode.data);
}
}
}
public class Link {
public static void main(String[] args) {
LinkedList b=new LinkedList();
b.append(2);
b.append(4);
b.append(6);
b.print();
}
}