-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistfourcases.java
More file actions
213 lines (172 loc) · 4.37 KB
/
linkedlistfourcases.java
File metadata and controls
213 lines (172 loc) · 4.37 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void linkedListTraversal(Node* ptr)
{
while (ptr != nullptr)
{
cout << "Element: " << ptr->data << endl;
ptr = ptr->next;
}
}
// Case 1
Node* insertAtFirst(Node* head, int data){
Node* ptr = new Node;
ptr->data = data;
ptr->next = head;
return ptr;
}
// Case 2
Node* insertAtIndex(Node* head, int data, int index){
Node* ptr = new Node;
Node* p = head;
int i = 0;
while (i != index-1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
// Case 3
Node* insertAtEnd(Node* head, int data){
Node* ptr = new Node;
ptr->data = data;
Node* p = head;
while(p->next != nullptr){
p = p->next;
}
p->next = ptr;
ptr->next = nullptr;
return head;
}
// Case 4
Node* insertAfterNode(Node* head, Node* prevNode, int data){
Node* ptr = new Node;
ptr->data = data;
ptr->next = prevNode->next;
prevNode->next = ptr;
return head;
}
int main(){
Node* head;
Node* second;
Node* third;
Node* fourth;
// Allocate memory for nodes in the linked list in Heap
head = new Node;
second = new Node;
third = new Node;
fourth = new Node;
// Link first and second nodes
head->data = 7;
head->next = second;
// Link second and third nodes
second->data = 11;
second->next = third;
// Link third and fourth nodes
third->data = 41;
third->next = fourth;
// Terminate the list at the third node
fourth->data = 66;
fourth->next = nullptr;
cout << "Linked list before insertion\n";
linkedListTraversal(head);
// head = insertAtFirst(head, 56);
// head = insertAtIndex(head, 56, 1);
// head = insertAtEnd(head, 56);
head = insertAfterNode(head, third, 45);
cout << "\nLinked list after insertion\n";
linkedListTraversal(head);
return 0;
}
public class LinkedList {
private Node head;
private static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Insert at the beginning
public void insertAtFirst(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
// Insert at a specified index
public void insertAtIndex(int data, int index) {
if (index == 0) {
insertAtFirst(data);
return;
}
Node newNode = new Node(data);
Node current = head;
int i = 0;
while (current != null && i < index - 1) {
current = current.next;
i++;
}
if (current == null) {
System.out.println("Invalid index");
return;
}
newNode.next = current.next;
current.next = newNode;
}
// Insert at the end
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
// Insert after a specified node
public void insertAfterNode(Node prevNode, int data) {
if (prevNode == null) {
System.out.println("Invalid previous node");
return;
}
Node newNode = new Node(data);
newNode.next = prevNode.next;
prevNode.next = newNode;
}
// Print the linked list
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertAtEnd(7);
list.insertAtEnd(11);
list.insertAtEnd(41);
list.insertAtEnd(66);
System.out.println("Linked list before insertion:");
list.printList();
list.insertAtFirst(56);
list.insertAtIndex(56, 1);
list.insertAtEnd(56);
list.insertAfterNode(list.head.next.next, 45);
System.out.println("\nLinked list after insertion:");
list.printList();
}
}