-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCustomLinkedList.java
More file actions
137 lines (115 loc) · 3.72 KB
/
MyCustomLinkedList.java
File metadata and controls
137 lines (115 loc) · 3.72 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
import java.util.Scanner;
public class MyCustomLinkedList{
public static void main(String []args){
SinglyLinkedList myLinkedlist = new SinglyLinkedList();
Scanner in = new Scanner(System.in);
System.out.println("Enter length of LinkedList:");
int a = in.nextInt();
for (int i = 1; i <= a; i++) {
System.out.println("Enter LinkList's Element number " + i + ": ");
myLinkedlist.insertFirst(in.nextInt());
}
System.out.println("Your LInkedList :");
myLinkedlist.printLinkedList();
}
}
class Node {
public int data;
public Node next;
public void displayNodeData() {
System.out.println("{ " + data + " } ");
}
}
class SinglyLinkedList {
private Node head;
private Node tail;
public boolean isEmpty() {
return (head == null);
}
// used to insert a node at the start of linked list
public void insertFirst(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
}
// used to delete a last node from linkedlist
public void deleteLastElement() {
Node current = head;
Node pervious = head;
Node temp = head;
while (current.next != null) {
pervious = current;
}
while (temp.next != null && temp.data != pervious.data) {
temp = temp.next;
}
if (temp.next != null)
temp.next = temp.next.next;
}
// used to delete element which value is smaller than value
public void delLesserNodes(int value) {
Node current = head;
/* Initialise max */
Node maxnode = head;
Node temp;
while (current != null && current.next != null) {
/* If current is smaller than max, then delete
current */
if (current.next.data < value) {
temp = current.next;
current.next = temp.next;
temp = null;
}
/* If current is greater than max, then update
max and move current */
else {
current = current.next;
maxnode = current;
}
}
}
// used to delete element which value is greater than value
public void deleteGreaterNode(int value) {
Node current = head;
/* Initialise max */
Node maxnode = head;
Node temp;
while (current != null && current.next != null) {
if (current.next.data > value) {
temp = current.next;
current.next = temp.next;
temp = null;
} else {
current = current.next;
maxnode = current;
}
}
}
// used to delete node from start of linked list
public Node deleteFirst() {
Node temp = head;
head = head.next;
return temp;
}
// used to insert a node at the start of linked list
public void insertLast(int data) {
Node current = head;
while (current.next != null) {
current = current.next; // we'll loop until current.next is null
}
Node newNode = new Node();
newNode.data = data;
current.next = newNode;
}
// For printing Linked List
public void printLinkedList() {
System.out.println("Printing LinkedList (head --> last) ");
Node current = head;
while (current != null) {
current.displayNodeData();
current = current.next;
}
System.out.println();
}
}