-
Notifications
You must be signed in to change notification settings - Fork 14
/
Java.Linkedlist
136 lines (101 loc) · 5.69 KB
/
Java.Linkedlist
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
A **linked list** is a linear data structure in computer science that consists of a sequence of elements, each of which points to the next element in the sequence. It's a dynamic data structure, meaning it can grow or shrink in size during program execution. Linked lists are used to store and manage collections of data where the order of elements matters. They provide efficient insertion and deletion of elements but have slower random access times compared to arrays.
Here's a detailed explanation of linked lists and their types:
**Basic Components of a Linked List:**
1. **Node:** Each element in a linked list is called a "node." A node contains two parts:
- Data: The actual data or value you want to store.
- Reference (or link): A reference or pointer to the next node in the sequence.
2. **Head:** The first node in the linked list is called the "head." It serves as the starting point for accessing the list.
3. **Tail:** The last node in the linked list is called the "tail." The tail's reference points to null, indicating the end of the list.
**Types of Linked Lists:**
1. **Singly Linked List:** In a singly linked list, each node has a reference to the next node in the sequence, forming a unidirectional chain. The last node's reference points to null. It's the simplest type of linked list.
```
[Node1] -> [Node2] -> [Node3] -> ... -> [NodeN] -> null
```
- Advantages: Efficient insertion and deletion at the beginning, easy to implement.
- Disadvantages: Inefficient random access (O(n)), as you must traverse the list from the head to reach a specific node.
2. **Doubly Linked List:** In a doubly linked list, each node has references to both the next and the previous nodes in the sequence, forming a bidirectional chain.
```
null <- [Node1] <-> [Node2] <-> [Node3] <-> ... <-> [NodeN] -> null
```
- Advantages: Efficient insertion and deletion at both the beginning and end of the list, and better for backward traversal.
- Disadvantages: Increased memory usage due to the additional previous pointers.
3. **Circular Linked List:** A circular linked list is a variation where the tail's reference points back to the head, creating a loop.
```
[Node1] -> [Node2] -> [Node3] -> ... -> [NodeN] -> [Node1]
```
- Advantages: Useful for applications requiring continuous looping through elements.
- Disadvantages: It can be challenging to determine the end of the list without additional information.
4. **Singly Linked List with a Tail Pointer:** In this variation of a singly linked list, you maintain a reference to both the head and the tail nodes. This allows for efficient insertion and deletion at both ends.
```
[Node1] -> [Node2] -> [Node3] -> ... -> [NodeN] -> null
^ ^
| |
head tail
```
- Advantages: Efficient for insertion and deletion at both ends.
- Disadvantages: Still inefficient for random access, and requires extra memory to store the tail reference.
Certainly! Let's create a simple singly linked list in Java and provide step-by-step code explanations for each part.
1. **Node Class**: We'll start by creating a class for the individual nodes in the linked list. Each node will contain two parts: the data it holds and a reference to the next node.
```java
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
```
2. **Creating a Linked List**: We'll create a simple linked list with a few nodes and add them one by one.
```java
class LinkedList {
Node head; // Reference to the first node
// Constructor to initialize an empty linked list
public LinkedList() {
this.head = null;
}
// Method to add a node at the end of the linked list
public void append(int data) {
Node newNode = new Node(data);
// If the list is empty, set the new node as the head
if (head == null) {
head = newNode;
return;
}
// Traverse the list to find the last node
Node current = head;
while (current.next != null) {
current = current.next;
}
// Set the next of the last node to the new node
current.next = newNode;
}
// Method to display the linked list
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
}
public class Main {
public static void main(String[] args) {
LinkedList myList = new LinkedList();
// Add elements to the linked list
myList.append(10);
myList.append(20);
myList.append(30);
// Display the linked list
myList.display();
}
}
```
**Explanation:**
- We create a `Node` class to represent the elements of the linked list. Each node contains an integer `data` and a reference (`next`) to the next node.
- We create a `LinkedList` class to manage the linked list. It has a `head` reference initially set to `null`.
- The `append` method adds a new node with the given data at the end of the list. If the list is empty, it sets the new node as the head.
- The `display` method traverses the list from the head and prints its elements.
When you run this code, it will create a linked list with elements 10, 20, and 30, and display the list as: `10 -> 20 -> 30 -> null`.
Linked lists are fundamental data structures used in many algorithms and data manipulation tasks. The choice of which type to use depends on the specific requirements of the problem you are solving.