-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneric_class_LinkedList_implementation.java
More file actions
30 lines (30 loc) · 1.26 KB
/
Copy pathGeneric_class_LinkedList_implementation.java
File metadata and controls
30 lines (30 loc) · 1.26 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
/*The code defines a generic singly linked list implementation using a class Generic_class_LinkedList_implementation<T> that supports any data type.
It includes an inner class Node that represents the nodes in the linked list, where each node stores a value (data) and a reference to the next node (next). */
public class Generic_class_LinkedList_implementation<T>{
public class Node{
T data;
Node next;
public Node(T data){
this.data=data;
}
}
Node head;
public Generic_class_LinkedList_implementation(T data){
this.head=new Node(data);
}
public static void main(String args[]){
Generic_class_LinkedList_implementation<Integer> obj=new Generic_class_LinkedList_implementation<>(10);
obj.head.next=obj.new Node(20);
while(obj.head!=null){
System.out.print(obj.head.data+" ");
obj.head=obj.head.next;
}
System.out.println();
Generic_class_LinkedList_implementation<String> obj_string=new Generic_class_LinkedList_implementation<>("Anuj");
obj_string.head.next=obj_string.new Node("Lovely");
while(obj_string.head!=null){
System.out.print(obj_string.head.data+" ");
obj_string.head=obj_string.head.next;
}
}
}