Question: How to create without generic Int type Node ?
Ans: public class Node { // this is Node class without Generic
int data; // this is for data like array Element
Node next; //Node ek class hai , usi class ka khud ka variable hai, This is Node(Class) Type variable for
//Node is basically refer to class , this is for next element
Node(int data){ // this is constructor bcse user will pass data value and int because we want to create int type data constructor
this.data = data; // this is refer data
next = null;
}
}
public class newNodeUse {
public static void main(String[] args){
Node node1 = new Node(30);
Node node2 = new Node(40);
Node node3 = new Node(48);
node1.next = node2;
System.out.println(node2.data); // this is basically node variable name
}
}
Question: What is the different between Node<Integer> and Node node1 ?
Ans: The difference between the two lines lies in the use of generics. Let's break down each line:
Node node1 = new Node(30);
This line creates a Node
object without specifying the type of data it will store. This is what's called using the raw type in Java. In this case, the type of data the Node
object will store is not explicitly specified. While Java allows this, it's generally discouraged because it bypasses compile-time type checking.
Node<Integer> node1 = new Node(40);
This line creates a Node
object with a specified generic type of Integer
. Generics allow you to specify the type of data that a class will work with. By using Node<Integer>
, you're specifying that this Node
object will store integers. This provides compile-time type safety, meaning that the compiler will catch type-related errors at compile time rather than at runtime. In this case, node1
will be a Node
object that stores integers, and attempting to add anything other than an Integer
to it will result in a compile-time error.
In summary, using generics (Node<Integer>
) provides type safety and makes your code more readable and maintainable by clearly indicating the type of data a class will work with. Using raw types (Node
) should generally be avoided in favor of using generics for better type safety and readability.
Note: LinkedList is dynamic , we dont need to put size
In terms of best practices and readability, using generics (Node<Integer>
) is typically preferred over using raw types (Node
).
The basic building block of a linked list is the node, and a linked list is formed by connecting these nodes together. The last node in the list typically points to null
, indicating the end of the list.
Why to use Linked List ?
Ans:Linked lists offer several advantages in certain scenarios, and the choice to use them depends on the specific requirements of a given problem. Here are some reasons to use linked lists:
What is Node In linked List ?
Ans: In the context of a linked list, a node is a fundamental building block that holds a piece of data and a reference (or link) to the next node in the sequence. Each node represents an element in the linked list. The structure of a node typically consists of two parts:
Data (or Payload): This is the actual information or value that the node is storing. It could be of any data type, depending on the requirements of the linked list.
Next (or Link/Pointer): This is a reference to the next node in the sequence. It points to the memory location/address of the subsequent node. In the last node of the linked list, this reference is usually set to
null
to indicate the end of the list.
public class Node<T> { // this is Node class with generic
//maine T isliye likha hai kyo ki ham generic bana rhe aur hamara data T type ka hoga
// isse ye hoga ki ham har tarh ka data input le sakte hai
T data; // this is for data like array Element
Node<T> next; //Node ek class hai , usi class ka khud ka variable hai, This is Node(Class) Type variable for
//Node is basically refer to class , this is for next element
Node(T data){ // this is constructor bcse user will pass data value
this.data = data; // this is refer data
next = null;
}
}
import java.sql.SQLOutput;
public class nodeUse {
public static void main(String[] args) {
Node<Integer> node1 = new Node<Integer>(10); // this is first node
System.out.println(node1.data);
Node<Integer> node2 = new Node<Integer>(20); // this is second node
System.out.println(node2.data);
//Now creating link from node1 to node2
node1.next = node2; // this will link node1's reference with node2 reference
System.out.println(node1.next); // this will print reference of next node
}
}
public class nodeUse {
public static void main(String[] args) {
Node<Integer> node1 = new Node<Integer>(10); // this is first node
System.out.println(node1.data);
Node<Integer> node2 = new Node<Integer>(20); // this is second node
System.out.println(node2.data);
Node<Integer> node3 = new Node<Integer>(30);
//Now creating link from node1 to node2
node1.next = node2; // this will link node1's reference with node2 reference
node2.next = node3;
System.out.println(node1.next); // this will print reference of next node
}
}
Node<Integer> head = node1; // we assign node1 to head variable
while(head != null){
System.out.println(head.data); // this will print head data// first node's data
head = head.next; // this is for change node address from 1st to second or so on
}
import java.sql.SQLOutput;
public class nodeUse {
public static Node<Integer> takeInput(){ // node<Integer> is basically return type
Node<Integer> head = null; // this is null because predefined or first node always null
Scanner scan = new Scanner(System.in);
int data = scan.nextInt();
while (data != -1) {
Node<Integer> newNode = new Node<Integer>(data); // this is for enter data and data will represetn for int data
if(head == null){
head = newNode;
} else {
Node<Integer> temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = newNode;
}
data = scan.nextInt();
}return head;
}
Comments
Post a Comment