mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 14:04:17 +08:00
Code cleanup (#4246)
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
|
@ -28,18 +28,12 @@ public class DoublyLinkedList {
|
||||
*/
|
||||
private LinkOperations linkOperations;
|
||||
|
||||
/**
|
||||
* Size refers to the number of elements present in the list
|
||||
*/
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*/
|
||||
public DoublyLinkedList() {
|
||||
head = null;
|
||||
tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +49,6 @@ public class DoublyLinkedList {
|
||||
for (int i : array) {
|
||||
linkOperations.insertTail(i, this);
|
||||
}
|
||||
size = array.length;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,10 +47,5 @@ public class Merge_K_SortedLinkedlist {
|
||||
|
||||
private int data;
|
||||
private Node next;
|
||||
|
||||
public Node(int d) {
|
||||
this.data = d;
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -148,9 +148,7 @@ public class SinglyLinkedList extends Node {
|
||||
public void clear() {
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
Node prev = cur;
|
||||
cur = cur.next;
|
||||
prev = null; // clear to let GC do its work
|
||||
}
|
||||
head = null;
|
||||
size = 0;
|
||||
@ -346,9 +344,7 @@ public class SinglyLinkedList extends Node {
|
||||
public void deleteNth(int position) {
|
||||
checkBounds(position, 0, size - 1);
|
||||
if (position == 0) {
|
||||
Node destroy = head;
|
||||
head = head.next;
|
||||
destroy = null;
|
||||
/* clear to let GC do its work */
|
||||
size--;
|
||||
return;
|
||||
@ -358,10 +354,7 @@ public class SinglyLinkedList extends Node {
|
||||
cur = cur.next;
|
||||
}
|
||||
|
||||
Node destroy = cur.next;
|
||||
cur.next = cur.next.next;
|
||||
destroy = null; // clear to let GC do its work
|
||||
|
||||
size--;
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,6 @@ public class NodeStack<Item> {
|
||||
private Item data;
|
||||
|
||||
private static NodeStack<?> head;
|
||||
private NodeStack<?> next;
|
||||
private NodeStack<?> previous;
|
||||
private static int size = 0;
|
||||
|
||||
@ -72,7 +71,7 @@ public class NodeStack<Item> {
|
||||
} else {
|
||||
newNs.setPrevious(NodeStack.head);
|
||||
NodeStack.head.setNext(newNs);
|
||||
NodeStack.head.setHead(newNs);
|
||||
NodeStack.setHead(newNs);
|
||||
}
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() + 1);
|
||||
@ -86,7 +85,7 @@ public class NodeStack<Item> {
|
||||
public Item pop() {
|
||||
Item item = (Item) NodeStack.head.getData();
|
||||
|
||||
NodeStack.head.setHead(NodeStack.head.getPrevious());
|
||||
NodeStack.setHead(NodeStack.head.getPrevious());
|
||||
NodeStack.head.setNext(null);
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() - 1);
|
||||
@ -133,23 +132,11 @@ public class NodeStack<Item> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getters and setters (private)
|
||||
*/
|
||||
private NodeStack<?> getHead() {
|
||||
return NodeStack.head;
|
||||
}
|
||||
|
||||
private static void setHead(NodeStack<?> ns) {
|
||||
NodeStack.head = ns;
|
||||
}
|
||||
|
||||
private NodeStack<?> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
private void setNext(NodeStack<?> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
private NodeStack<?> getPrevious() {
|
||||
@ -171,8 +158,4 @@ public class NodeStack<Item> {
|
||||
private Item getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
private void setData(Item item) {
|
||||
this.data = item;
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,6 @@ public class GenericTree {
|
||||
}
|
||||
|
||||
private Node root;
|
||||
private int size;
|
||||
|
||||
public GenericTree() { // Constructor
|
||||
Scanner scn = new Scanner(System.in);
|
||||
root = create_treeG(null, 0, scn);
|
||||
@ -44,7 +42,6 @@ public class GenericTree {
|
||||
int number = scn.nextInt();
|
||||
for (int i = 0; i < number; i++) {
|
||||
Node child = create_treeG(node, i, scn);
|
||||
size++;
|
||||
node.child.add(child);
|
||||
}
|
||||
return node;
|
||||
|
@ -30,11 +30,6 @@ public class TreeRandomNode {
|
||||
|
||||
int item;
|
||||
Node left, right;
|
||||
|
||||
public Node(int key) {
|
||||
item = key;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Using an arraylist to store the inorder traversal of the given binary tree
|
||||
|
@ -12,6 +12,7 @@ class Main {
|
||||
int inputX0 = sc.nextInt();
|
||||
int toPrint = nearestRightKey(root, inputX0);
|
||||
System.out.println("Key: " + toPrint);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
public static NRKTree BuildTree() {
|
||||
|
Reference in New Issue
Block a user