Skip to content

Commit 2488a2a

Browse files
authored
Code cleanup (TheAlgorithms#4246)
1 parent 3facb0d commit 2488a2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+50
-167
lines changed

src/main/java/com/thealgorithms/conversions/OctalToBinary.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.thealgorithms.conversions;
2-
import java.util.Scanner;
32

43
/**
54
* Converts any Octal Number to a Binary Number

src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.datastructures.graphs;
22

33
import java.util.ArrayList;
4-
import java.util.Iterator;
54
import java.util.List;
65
import java.util.Stack;
76

src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@ public class DoublyLinkedList {
2828
*/
2929
private LinkOperations linkOperations;
3030

31-
/**
32-
* Size refers to the number of elements present in the list
33-
*/
34-
private int size;
35-
3631
/**
3732
* Default Constructor
3833
*/
3934
public DoublyLinkedList() {
4035
head = null;
4136
tail = null;
42-
size = 0;
4337
}
4438

4539
/**
@@ -55,7 +49,6 @@ public DoublyLinkedList(int[] array) {
5549
for (int i : array) {
5650
linkOperations.insertTail(i, this);
5751
}
58-
size = array.length;
5952
}
6053

6154
/**

src/main/java/com/thealgorithms/datastructures/lists/Merge_K_SortedLinkedlist.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,5 @@ private class Node {
4747

4848
private int data;
4949
private Node next;
50-
51-
public Node(int d) {
52-
this.data = d;
53-
next = null;
54-
}
5550
}
5651
}

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ public Node reverseList(Node node) {
148148
public void clear() {
149149
Node cur = head;
150150
while (cur != null) {
151-
Node prev = cur;
152151
cur = cur.next;
153-
prev = null; // clear to let GC do its work
154152
}
155153
head = null;
156154
size = 0;
@@ -346,9 +344,7 @@ public void delete() {
346344
public void deleteNth(int position) {
347345
checkBounds(position, 0, size - 1);
348346
if (position == 0) {
349-
Node destroy = head;
350347
head = head.next;
351-
destroy = null;
352348
/* clear to let GC do its work */
353349
size--;
354350
return;
@@ -358,10 +354,7 @@ public void deleteNth(int position) {
358354
cur = cur.next;
359355
}
360356

361-
Node destroy = cur.next;
362357
cur.next = cur.next.next;
363-
destroy = null; // clear to let GC do its work
364-
365358
size--;
366359
}
367360

src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static void main(String[] args) {
4343
private Item data;
4444

4545
private static NodeStack<?> head;
46-
private NodeStack<?> next;
4746
private NodeStack<?> previous;
4847
private static int size = 0;
4948

@@ -72,7 +71,7 @@ public void push(Item item) {
7271
} else {
7372
newNs.setPrevious(NodeStack.head);
7473
NodeStack.head.setNext(newNs);
75-
NodeStack.head.setHead(newNs);
74+
NodeStack.setHead(newNs);
7675
}
7776

7877
NodeStack.setSize(NodeStack.getSize() + 1);
@@ -86,7 +85,7 @@ public void push(Item item) {
8685
public Item pop() {
8786
Item item = (Item) NodeStack.head.getData();
8887

89-
NodeStack.head.setHead(NodeStack.head.getPrevious());
88+
NodeStack.setHead(NodeStack.head.getPrevious());
9089
NodeStack.head.setNext(null);
9190

9291
NodeStack.setSize(NodeStack.getSize() - 1);
@@ -133,23 +132,11 @@ public void print() {
133132
}
134133
}
135134

136-
/**
137-
* Getters and setters (private)
138-
*/
139-
private NodeStack<?> getHead() {
140-
return NodeStack.head;
141-
}
142-
143135
private static void setHead(NodeStack<?> ns) {
144136
NodeStack.head = ns;
145137
}
146138

147-
private NodeStack<?> getNext() {
148-
return next;
149-
}
150-
151139
private void setNext(NodeStack<?> next) {
152-
this.next = next;
153140
}
154141

155142
private NodeStack<?> getPrevious() {
@@ -171,8 +158,4 @@ private static void setSize(int size) {
171158
private Item getData() {
172159
return this.data;
173160
}
174-
175-
private void setData(Item item) {
176-
this.data = item;
177-
}
178161
}

src/main/java/com/thealgorithms/datastructures/trees/GenericTree.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ private class Node {
2323
}
2424

2525
private Node root;
26-
private int size;
27-
2826
public GenericTree() { // Constructor
2927
Scanner scn = new Scanner(System.in);
3028
root = create_treeG(null, 0, scn);
@@ -44,7 +42,6 @@ private Node create_treeG(Node node, int childindx, Scanner scn) {
4442
int number = scn.nextInt();
4543
for (int i = 0; i < number; i++) {
4644
Node child = create_treeG(node, i, scn);
47-
size++;
4845
node.child.add(child);
4946
}
5047
return node;

src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ private class Node {
3030

3131
int item;
3232
Node left, right;
33-
34-
public Node(int key) {
35-
item = key;
36-
left = right = null;
37-
}
3833
}
3934

4035
// Using an arraylist to store the inorder traversal of the given binary tree

src/main/java/com/thealgorithms/datastructures/trees/nearestRightKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void main(String[] args) {
1212
int inputX0 = sc.nextInt();
1313
int toPrint = nearestRightKey(root, inputX0);
1414
System.out.println("Key: " + toPrint);
15+
sc.close();
1516
}
1617

1718
public static NRKTree BuildTree() {

src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Given a string containing just the characters '(' and ')', find the length of
75
* the longest valid (well-formed) parentheses substring.

src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ public static void main(String[] args) {
5151
}
5252
}
5353
System.out.println(determinant(a, n));
54+
in.close();
5455
}
5556
}

src/main/java/com/thealgorithms/maths/DudeneyNumber.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77
package com.thealgorithms.maths;
88

9-
import java.io.*;
10-
119
public class DudeneyNumber {
1210

1311
// returns True if the number is a Dudeney number and False if it is not a Dudeney number.

src/main/java/com/thealgorithms/maths/KeithNumber.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public static void main(String[] args) {
4949
} else {
5050
System.out.println("No, the given number is not a Keith number.");
5151
}
52+
in.close();
5253
}
5354
}

src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static void main(String[] args) {
2121
System.out.println("Please enter second number >> ");
2222
int num2 = input.nextInt();
2323
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
24+
input.close();
2425
}
2526

2627
/*

src/main/java/com/thealgorithms/maths/MagicSquare.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ public static void main(String[] args) {
4545
}
4646
System.out.println();
4747
}
48+
sc.close();
4849
}
4950
}

src/main/java/com/thealgorithms/maths/NonRepeatingElement.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void main(String[] args) {
5050
}
5151

5252
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
53+
sc.close();
5354
}
5455
/*
5556
Explanation of the code:

src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Scanner;
4-
53
/*
64
*To learn about the method, visit the link below :
75
* https://en.wikipedia.org/wiki/Newton%27s_method

src/main/java/com/thealgorithms/minimizinglateness/MinimizingLateness.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ private static class Schedule { // Schedule class
1111

1212
int t = 0; // Time required for the operation to be performed
1313
int d = 0; // Time the job should be completed
14-
int s = 0; // Start time of the task
15-
int f = 0; // End time of the operation
16-
1714
public Schedule(int t, int d) {
1815
this.t = t;
1916
this.d = d;
@@ -46,8 +43,6 @@ public static void main(String[] args) throws IOException {
4643
int tryTime = 0; // Total time worked
4744
int lateness = 0; // Lateness
4845
for (int j = 0; j < indexCount - 1; j++) {
49-
array[j].s = tryTime; // Start time of the task
50-
array[j].f = tryTime + array[j].t; // Time finished
5146
tryTime = tryTime + array[j].t; // Add total work time
5247
// Lateness
5348
lateness = lateness + Math.max(0, tryTime - array[j].d);

src/main/java/com/thealgorithms/misc/Sort012D.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static void main(String[] args) {
2121
a[i] = np.nextInt();
2222
}
2323
sort012(a);
24+
np.close();
2425
}
2526

2627
public static void sort012(int[] a) {

src/main/java/com/thealgorithms/misc/Sparcity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public static void main(String[] args) {
4949
}
5050
}
5151
System.out.println("Sparcity of matrix is: " + sparcity(mat));
52+
in.close();
5253
}
5354
}

src/main/java/com/thealgorithms/misc/ThreeSumProblem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n");
2020
System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n");
2121
System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts)));
22+
scan.close();
2223
}
2324

2425
public List<List<Integer>> BruteForce(int[] nums, int target) {

src/main/java/com/thealgorithms/others/BoyerMoore.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ public static void main(String[] args) {
4444
a[i] = input.nextInt();
4545
}
4646
System.out.println("the majority element is " + findmajor(a));
47+
input.close();
4748
}
4849
}

src/main/java/com/thealgorithms/others/CountWords.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Scanner;
4-
53
/**
64
* @author Marcus
75
*/

src/main/java/com/thealgorithms/others/HappyNumbersSeq.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
}
2020
String res = n == 1 ? "1 Happy number" : "Sad number";
2121
System.out.println(res);
22+
in.close();
2223
}
2324

2425
private static int sumSquares(int n) {

src/main/java/com/thealgorithms/others/Huffman.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@ public static void main(String[] args) {
118118

119119
// print the codes by traversing the tree
120120
printCode(root, "");
121+
s.close();
121122
}
122123
}

src/main/java/com/thealgorithms/others/RotateMatriceBy90Degree.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public static void main(String[] args) {
2222
}
2323
}
2424

25-
Rotate g = new Rotate();
26-
g.rotate(arr);
25+
Rotate.rotate(arr);
2726
printMatrix(arr);
2827
}
2928
sc.close();

src/main/java/com/thealgorithms/others/cn/HammingDistance.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.thealgorithms.others.cn;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
final public class HammingDistance {
74
private HammingDistance() {
85
}

src/main/java/com/thealgorithms/searches/LinearSearchThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void main(String[] args) {
3333
}
3434
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
3535
System.out.println("Found = " + found);
36+
in.close();
3637
}
3738
}
3839

src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ static int binarySearch(int[] arr, int target) {
2121
}
2222

2323
public static void main(String[] args) {
24-
PerfectBinarySearch BinarySearch = new PerfectBinarySearch();
2524
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
26-
assert BinarySearch.binarySearch(array, -1) == -1;
27-
assert BinarySearch.binarySearch(array, 11) == -1;
25+
assert PerfectBinarySearch.binarySearch(array, -1) == -1;
26+
assert PerfectBinarySearch.binarySearch(array, 11) == -1;
2827
}
2928
}

src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Arrays;
4-
53
public class SearchInARowAndColWiseSortedMatrix {
64
/**
75
* Search a key in row and column wise sorted matrix

src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void main(String[] args) {
2727
int num = sc.nextInt();
2828
long ans = squareRoot(num);
2929
System.out.println("The square root is : " + ans);
30+
sc.close();
3031
}
3132

3233
/**

src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.thealgorithms.searches;
2-
import java.util.*;
32
public class sortOrderAgnosticBinarySearch {
43
public static int find(int[] arr, int key) {
54
int start = 0;

src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ public static void main(String[] args) {
6666
for (int i = 0; i < a.length; i++) {
6767
System.out.print(a[i] + " ");
6868
}
69+
inp.close();
6970
}
7071
}

0 commit comments

Comments
 (0)