Skip to main content

Linked List Data Structure

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:

  1. 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.

  1. 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).



Question: What is Linked List ?
Ans: In Java, a linked list is a linear data structure that consists of a sequence of elements where each element points to the next element in the sequence. Unlike arrays, linked lists do not have a fixed size, and elements are not stored in contiguous memory locations. Instead, each element (node) in a linked list contains a data element and a reference (or link) to the next node in the sequence.

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:

  1. Dynamic Size:

    • Linked lists can easily grow or shrink in size during the execution of a program. Unlike arrays, linked lists do not require specifying a fixed size in advance, making them more flexible for situations where the size of the data structure is not known beforehand.
  2. Efficient Insertion and Deletion:

    • Insertion and deletion of elements in a linked list can be more efficient compared to arrays. In a linked list, inserting or deleting an element involves updating only the pointers of adjacent nodes, whereas in an array, shifting elements is required.
  3. Constant-time Insertion and Deletion at the Head:

    • Adding or removing an element at the beginning of a linked list can be done in constant time, O(1), as it only involves updating the head pointer.
  4. No Pre-allocation of Memory:

    • Linked lists do not require contiguous memory allocation. Nodes can be dynamically allocated as needed, which can be particularly advantageous when memory is allocated and deallocated frequently.
  5. Memory Efficiency:

    • Linked lists can be more memory-efficient than arrays, especially when dealing with sparse data or situations where the actual number of elements is much smaller than the allocated size.
  6. No Wasted Memory:

    • In arrays, memory may be wasted if the size is allocated but not fully utilized. Linked lists allocate memory for each node only when needed, avoiding unnecessary memory consumption.
  7. Support for O(1) Concatenation:

    • Concatenating two linked lists can be done in O(1) time by updating the pointers, while concatenating arrays requires copying all the elements from one array to another.

However, it's important to note that linked lists also have some disadvantages, such as slower random access time compared to arrays and higher overhead due to the storage of pointers. The choice between linked lists and arrays depends on the specific requirements and characteristics of the problem at hand.








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:

  1. 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.

  2. 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.


next node ka address and Value Store karte hai aur ye pure structure ko Node kehte hai.

Head: Starting point of Linked List is called head.
Tail: Ending Node of linked List is called tail.

Har Node me Value and Reference store hoti hai .

Question: Create a node and link 2 objects.
Ans: 

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;
}
}

LinkedList Used Classs .. Where I will create 2 node object
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


}
}


Link with 3 nodes



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


}
}
yaha ham kya kar rhe hai hamne 3 nodes banaya aur use link kiya hai .next = agla node name se 
aur node3 ke bad koi node nhi hai to ye null hi rhega isliye ham ise link nhi kar rhe hai.




Question: How to Create head on LinkList
Ans: Node<Integer> head = node1; // we assign node1 to head variable


Question: How to triverse on LinkList using loop ?
Ans: 

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
}

while loop keh rha head mera not equal to null hai to run karo . 

 





Question: How to Take LinkedList Node from user in LinkedList.
Ans: import java.util.Scanner;
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;


}




Question: why we need to use temp variable ? (Node<Integer> temp = head;)
Ans: if we need to return node then only on that case we need to use temp variable ,
maan lo ham ek linkedlist me se remove kar rhe element to us case me agar ham temp use
nhi karenge to head ka address lost ho jayega aur aise me use return krna hamare liye
mushkil ho jayega isliye ham use karte hai.

Note: if we want to compare with integer type data with node then we need to
put Node_name.data to use.

example: Node<Integer> temp = head;
if(temp.data == n){



Circular Singly Linked List
Circular Doubly Linked List


Doubly Linked List : we can traversal reverse and forward both

Space increase from Array 


Comments

Popular posts from this blog

Polity

  1.    सन 1600 में ईस्ट इंडिया कंपनी भारत आई थी जिसका परमिशन ब्रिटिश की महारानी एलीजाबेथ ने दिया था 2.    परमिशन में चार्टर दिया गया था साथ ही मोनोपोली दी गयी थी अलीजाबेत के द्वारा 3.    बिटिश ईष्ट इंडिया कंपनी भारत शिप से आई थी जिस शिप का नाम था रेड ड्रैगन 4.    भारत में आने के बाद उन्होंने पहली फैक्ट्री 1611 मछलीपटनम में बनाई 5.    दूसरी फैक्ट्री 1612 में सूरत में बनाया था 6.    फैक्ट्री नियन्त्र के लिए तीन प्रेसीडेंसी बनायीं गयी जो थी बॉम्बे, बंगाल, मद्रास 7.    बंगाल का राजा था सिराजुदुल्ला और ब्रिटिश रोबर्ट clive युद्ध किया 1757 ऐसा जिसे battle of plasi कहा गया जिसमें रोबर्ट clive की जीत हुयी 8.    कंपनी का rule 1773 से 1858 तक चला था 9.    ताज का शाशन था 1858 से 1947 10.    Regulating act आया था 1773 में 11.    Act of settlement आया था 1781 में 12.    भारत परिषद् अधिनियम आया था 1861, 1892, 1909 13.    Govt of इंडिया act आया था 1858 में 14.                  ब्रिटिश सरकार ने 1773 में एक regulating act लाया गया जिसमें बंगाल को हेड बनाया गया जिसे गवर्नर जनरल कहा गया बंगा

Test 2

 Question: You have made a smartphone app and want to set its subscription price such that the profit earned is maximised. There are certain users who will subscribe to your app only if their budget is greater than or equal to your price. You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn. Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you can earn is : m*x Sample input 1: 30 20 53 14 Output 60 import   java . util .*; import   java . util . Scanner ; public   class   solution   {      public   static   int   maximumProfit ( int   budget [])   {      Arrays . sort ( budget );          // int maxProfit = 0;          // // Iterate through each possible subscription price and calculate profit          // for (int i = 0; i < budget.length; i++) {          //     int currentProfit = budget[i] * (budget.length - i);          //     maxProfit = Mat