mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-18 00:59:44 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,54 +1,65 @@
|
||||
public class CircleLinkedList<E>{
|
||||
private static class Node<E>{
|
||||
Node<E> next;
|
||||
E value;
|
||||
private Node(E value, Node<E> next){
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
//For better O.O design this should be private allows for better black box design
|
||||
private int size;
|
||||
//this will point to dummy node;
|
||||
private Node<E> head;
|
||||
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
|
||||
public CircleLinkedList(){
|
||||
//creation of the dummy node
|
||||
head = new Node<E>(null,head);
|
||||
size = 0;
|
||||
}
|
||||
// getter for the size... needed because size is private.
|
||||
public int getSize(){ return size;}
|
||||
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
|
||||
public void append(E value){
|
||||
if(value == null){
|
||||
// we do not want to add null elements to the list.
|
||||
throw new NullPointerException("Cannot add null element to the list");
|
||||
}
|
||||
//head.next points to the last element;
|
||||
head.next = new Node<E>(value,head);
|
||||
size++;}
|
||||
public E remove(int pos){
|
||||
if(pos>size || pos< 0){
|
||||
//catching errors
|
||||
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
|
||||
}
|
||||
Node<E> iterator = head.next;
|
||||
//we need to keep track of the element before the element we want to remove we can see why bellow.
|
||||
Node<E> before = head;
|
||||
for(int i = 1; i<=pos; i++){
|
||||
package DataStructures.Lists;
|
||||
|
||||
public class CircleLinkedList<E> {
|
||||
private static class Node<E> {
|
||||
Node<E> next;
|
||||
E value;
|
||||
|
||||
private Node(E value, Node<E> next) {
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
//For better O.O design this should be private allows for better black box design
|
||||
private int size;
|
||||
//this will point to dummy node;
|
||||
private Node<E> head;
|
||||
|
||||
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
|
||||
public CircleLinkedList() {
|
||||
//creation of the dummy node
|
||||
head = new Node<E>(null, head);
|
||||
size = 0;
|
||||
}
|
||||
|
||||
// getter for the size... needed because size is private.
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
|
||||
public void append(E value) {
|
||||
if (value == null) {
|
||||
// we do not want to add null elements to the list.
|
||||
throw new NullPointerException("Cannot add null element to the list");
|
||||
}
|
||||
//head.next points to the last element;
|
||||
head.next = new Node<E>(value, head);
|
||||
size++;
|
||||
}
|
||||
|
||||
public E remove(int pos) {
|
||||
if (pos > size || pos < 0) {
|
||||
//catching errors
|
||||
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
|
||||
}
|
||||
Node<E> iterator = head.next;
|
||||
//we need to keep track of the element before the element we want to remove we can see why bellow.
|
||||
Node<E> before = head;
|
||||
for (int i = 1; i <= pos; i++) {
|
||||
iterator = iterator.next;
|
||||
before = before.next;
|
||||
}
|
||||
E saved = iterator.value;
|
||||
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
|
||||
before.next = iterator.next;
|
||||
// scrubbing
|
||||
iterator.next = null;
|
||||
iterator.value = null;
|
||||
return saved;
|
||||
before = before.next;
|
||||
}
|
||||
E saved = iterator.value;
|
||||
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
|
||||
before.next = iterator.next;
|
||||
// scrubbing
|
||||
iterator.next = null;
|
||||
iterator.value = null;
|
||||
return saved;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CursorLinkedList<T> {
|
||||
|
||||
private static class Node<T> {
|
||||
@ -93,8 +97,8 @@ public class CursorLinkedList<T> {
|
||||
while (start != -1) {
|
||||
|
||||
T element = cursorSpace[start].element;
|
||||
if (counter == position){
|
||||
return element;
|
||||
if (counter == position) {
|
||||
return element;
|
||||
}
|
||||
|
||||
start = cursorSpace[start].next;
|
||||
@ -107,9 +111,9 @@ public class CursorLinkedList<T> {
|
||||
}
|
||||
|
||||
|
||||
public void removeByIndex(int index){
|
||||
public void removeByIndex(int index) {
|
||||
|
||||
if(index >= 0 && index < count){
|
||||
if (index >= 0 && index < count) {
|
||||
|
||||
T element = get(index);
|
||||
remove(element);
|
||||
@ -130,13 +134,13 @@ public class CursorLinkedList<T> {
|
||||
head = temp_next;
|
||||
} else { // otherwise cases
|
||||
|
||||
int prev_index = head;
|
||||
int prev_index = head;
|
||||
int current_index = cursorSpace[prev_index].next;
|
||||
|
||||
while (current_index != -1 ) {
|
||||
while (current_index != -1) {
|
||||
|
||||
T current_element = cursorSpace[current_index].element;
|
||||
if(current_element.equals(element)){
|
||||
if (current_element.equals(element)) {
|
||||
cursorSpace[prev_index].next = cursorSpace[current_index].next;
|
||||
free(current_index);
|
||||
break;
|
||||
|
@ -1,3 +1,4 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
/**
|
||||
* This class implements a DoublyLinkedList. This is done using the classes
|
||||
@ -13,7 +14,7 @@
|
||||
* @author Unknown
|
||||
*/
|
||||
|
||||
class DoublyLinkedList {
|
||||
public class DoublyLinkedList {
|
||||
/**
|
||||
* Head refers to the front of the list
|
||||
*/
|
||||
|
@ -1,3 +1,5 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.PriorityQueue;
|
||||
|
@ -1,3 +1,5 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
/**
|
||||
* This class implements a SinglyLinked List. This is done
|
||||
* using SinglyLinkedList class and a LinkForLinkedList Class.
|
||||
@ -8,10 +10,8 @@
|
||||
* it grows and shrinks as it is edited. This is an example of
|
||||
* a singly linked list. Elements can only be added/removed
|
||||
* at the head/front of the list.
|
||||
*
|
||||
* @author yanglbme
|
||||
*/
|
||||
class SinglyLinkedList {
|
||||
public class SinglyLinkedList {
|
||||
/**
|
||||
* Head refer to the front of the list
|
||||
*/
|
||||
@ -38,8 +38,7 @@ class SinglyLinkedList {
|
||||
public void insertNth(int data, int position) {
|
||||
if (position < 0 || position > getSize()) {
|
||||
throw new RuntimeException("position less than zero or position more than the count of list");
|
||||
}
|
||||
else if (position == 0)
|
||||
} else if (position == 0)
|
||||
insertHead(data);
|
||||
else {
|
||||
Node cur = head;
|
||||
@ -66,13 +65,12 @@ class SinglyLinkedList {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method deletes an element at Nth position
|
||||
*/
|
||||
* This method deletes an element at Nth position
|
||||
*/
|
||||
public void deleteNth(int position) {
|
||||
if (position < 0 || position > getSize()) {
|
||||
if (position < 0 || position > getSize()) {
|
||||
throw new RuntimeException("position less than zero or position more than the count of list");
|
||||
}
|
||||
else if (position == 0)
|
||||
} else if (position == 0)
|
||||
deleteHead();
|
||||
else {
|
||||
Node cur = head;
|
||||
@ -105,8 +103,8 @@ class SinglyLinkedList {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the linked list
|
||||
*/
|
||||
* Returns the size of the linked list
|
||||
*/
|
||||
public int getSize() {
|
||||
if (head == null)
|
||||
return 0;
|
||||
@ -156,8 +154,6 @@ class SinglyLinkedList {
|
||||
* This class is the nodes of the SinglyLinked List.
|
||||
* They consist of a value and a pointer to the node
|
||||
* after them.
|
||||
*
|
||||
* @author yanglbme
|
||||
*/
|
||||
class Node {
|
||||
/**
|
||||
|
Reference in New Issue
Block a user