-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
179 lines (165 loc) · 3.78 KB
/
LinkedList.java
File metadata and controls
179 lines (165 loc) · 3.78 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
// A java implementation of Singly Linked List for integer value
public class LinkedList {
// By default head is initialized to null
Node head;
static class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
// Print all the data in the LinkedList
public void printall(){
Node n = head;
while(n != null){
System.out.print(n.data + " ");
n = n.next;
}
}
//Insert element at the front of Linked List
public void insertfront(int new_data){
if(head == null)
head = new Node(new_data);
else{
Node n = new Node(new_data);
n.next = head;
head = n;
}
}
// Insert the data as the last element
public void insertlast(int new_data){
if(head == null){
head = new Node(new_data);}
else{
Node n = new Node(new_data);
Node temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = n;
}
}
//Deletes the first element with key
public void deletefirst(int key){
if(head != null && head.data == key){
head = head.next;
}else{
Node temp = head;
Node prev = head;
while(temp != null && temp.data != key){
prev = temp;
temp = temp.next;
}
if(temp == null){
System.out.println("Element with "+ key + " not found");}
else{
prev.next = temp.next;
}
}
}
//Deleting element at specific location
public void deletespecific(int position){
if(position < 1)
System.out.println("Location Error");
else if(head==null){
System.out.println("Linked List is NULL");}
else if(position == 1){
head = head.next;
}else{
Node temp = head;
Node prev = head;
for(int i = position; i>1 && temp!= null; i--){
prev = temp;
temp = temp.next;
}
if(temp==null){
System.out.println("Position out of bound");
}else{
prev.next = temp.next;
}
}
}
// Counts the number of elements
public int countall(){
if(head == null){
return 0;
}else{
int count = 0;
Node n = head;
while(n!=null){
count++;
n = n.next;
}
return count;
}
}
// Shows nth node data; -1 if n is below 0; -2 if n is more than the elements in the Linked List
public int NthNode(int n){
if(n<1) return -1;
else{
Node nth = head;
for(int i=n; i>1 && nth!=null; i--){
nth = nth.next;
}
if(nth == null) return -2;
else return nth.data;
}
}
// Prints the middle element's data
public int printmiddle(){
if(head == null || head.next == null) return -1;
else{
Node slow = head;
Node fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow.data;
}
}
// reversing the Linked List - Recursive function
public Node reversecall(Node curr, Node prev){
if(curr.next == null){
head = curr;
curr.next = prev;
return null;
}
Node nextnode = curr.next;
curr.next = prev;
reversecall(nextnode, curr);
return head;
}
public void reverse(){
if(head!=null || head.next!=null){
head = reversecall(head, null);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList l = new LinkedList();
LinkedList l1 = new LinkedList();
l.head = new Node(1);
l.insertfront(234);
l.insertfront(24);
l.insertfront(4);
l.insertlast(78);
l.printall();
l.deletefirst(4);
System.out.println();
l.printall();
l.deletespecific(1);
System.out.println();
l.printall();
System.out.println("Number of elements: "+l.countall());
System.out.println("Nth Element is : "+l.NthNode(3));
l.insertlast(56);
l.insertlast(98);
l.printall();
System.out.println("Middle Element is : "+l.printmiddle());
l.reverse();
l.printall();
}
}