Skip to content

Instantly share code, notes, and snippets.

@so77id
Created September 4, 2020 19:47
Show Gist options
  • Save so77id/954bc693792b4e7f5baf7618ee592834 to your computer and use it in GitHub Desktop.
Save so77id/954bc693792b4e7f5baf7618ee592834 to your computer and use it in GitHub Desktop.
Ordered linked list in Java generic
class List<T extends Comparable<T>> {
private Node<T> head;
public List() {
this.head = null;
}
// Insert in Order
public void insert(T value) {
Node<T> n_node = new Node<T>(value);
Node tmp;
if(this.head == null) {
this.head = n_node;
return;
}
if(this.head.getValue().compareTo(n_node.getValue()) > 0) {
n_node.setNext(this.head);
this.head = n_node;
return;
}
for(tmp = this.head; tmp.getNext() != null; tmp = tmp.getNext()) {
if(tmp.getNext().getValue().compareTo(n_node.getValue()) > 0) {
n_node.setNext(tmp.getNext());
tmp.setNext(n_node);
return;
}
}
tmp.setNext(n_node);
}
// Remove specific element
public Node<T> remove(T value) {
Node<T> ret = null;
Node<T> tmp;
if (this.head == null) { return null; }
if (this.head.getValue().compareTo(value) == 0 ) {
ret = this.head;
this.head = this.head.getNext();
return ret;
}
for(tmp = this.head; tmp.getNext() != null; tmp = tmp.getNext()) {
if (tmp.getNext().getValue().compareTo(value) == 0){
ret = tmp.getNext();
tmp.setNext(tmp.getNext().getNext());
return ret;
}
}
return ret;
}
// Search if contain specific element
public boolean search(T value) {
if (this.head == null) { return false; }
for (Node<T> tmp = head; tmp != null; tmp = tmp.getNext()) {
if (tmp.getValue().compareTo(value) == 0) return true;
}
return false;
}
// is empty
public boolean empty() {
return this.head != null ? false : true;
}
// print list
public void print() {
for(Node<T> tmp = this.head; tmp != null; tmp = tmp = tmp.getNext()) {
System.out.print(tmp.getValue() + "-");
}
}
}
import java.util.Scanner; // Import the Scanner class
public class Main {
public static void main(String[] args){
int n, buff;
Scanner scanner = new Scanner(System.in); // Create a Scanner object
List<Integer> myList = new List<Integer>();
System.out.println("Ingrese al tamaño de la lista:");
n = scanner.nextInt();
System.out.println("Ingrese los " + n + " datos de la lista:");
for(int i=0; i < n; i++){
buff = scanner.nextInt();
myList.insert(buff);
}
myList.print();
System.out.println("Que valor quiere eliminar de la lista?: ");
buff = scanner.nextInt();
myList.remove(buff);
myList.print();
// int n;
// String buff;
// Scanner scanner = new Scanner(System.in); // Create a Scanner object
//
// List<String> myList = new List<String>();
// System.out.println("Ingrese al tamaño de la lista:");
// n = scanner.nextInt();
//
// System.out.println("Ingrese los " + n + " datos de la lista:");
// for(int i=0; i < n; i++){
// buff = scanner.next();
// myList.insert(buff);
// }
// myList.print();
//
// System.out.println("Que valor quiere eliminar de la lista?: ");
// buff = scanner.next();
//
// myList.remove(buff);
//
// myList.print();
// int n;
// float buff;
// Scanner scanner = new Scanner(System.in); // Create a Scanner object
//
// List<Float> myList = new List<Float>();
// System.out.println("Ingrese al tamaño de la lista:");
// n = scanner.nextInt();
//
// System.out.println("Ingrese los " + n + " datos de la lista:");
// for(int i=0; i < n; i++){
// buff = scanner.nextFloat();
// myList.insert(buff);
// }
// myList.print();
//
// System.out.println("Que valor quiere eliminar de la lista?: ");
// buff = scanner.nextFloat();
//
// myList.remove(buff);
//
// myList.print();
}
}
class Node<T extends Comparable<T>> {
private T value;
private Node next;
public Node(T value, Node next) {
this.value = value;
this.next = next;
}
public Node(T value) {
this.value = value;
this.next = null;
}
public Node() {
this.value = null;
this.next = null;
}
public T getValue() { return this.value; }
public Node getNext() { return this.next; }
public void setValue(T value) { this.value = value; }
public void setNext(Node next) { this.next = next; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment