Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Basic LinkedList in Java
/* This program is just a simple demonstration of how to create a LinekdList in java. The good thing about making your own "LinkedList" is that you have control over how the class works. Even if this works properly, I would highly suggest you to use the already existing "LinkedList" class present in the Java Libraries. It better to use something that has been made by professionals rather than making it again on your own. So, let answer 2 questions that might come into your mind: --------------------------- How to import it? statement: import java.util.LinkedList; I don't know anything about this LinkedList. How do I use it? Ans: Use this documentation to understand how to use this class. It's the official documentation: Link ---> https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html Thanks for reading. */ class Node<Thing> { private Node<Thing> next; Thing data; public Node(Thing data) { this.data = data; this.next = null; } public boolean nextIsNull() { return (this.next == null || this.next.data == null); } public void setNext(Node<Thing> node) { this.next = node; } public void add(Thing data) { if(this.next == null) this.next = new Node<Thing>(data); else if(this.next.data == null) this.next.data = data; else this.next.add(data); } public boolean remove(Thing data) { if(String.valueOf(this.next.data).equals(String.valueOf(data))) { this.next = this.next.next; return true; } else if(!this.next.nextIsNull()) return this.next.remove(data); return false; } public String nodePrint() { if(nextIsNull()) return String.valueOf(this.data); else return String.valueOf(this.data) + (nextIsNull() ? "" : " -> ") + this.next.nodePrint(); } } class LinkedList<Thing> { private Node<Thing> head; private int size; public LinkedList() { this.size = 0; this.head = null; } public LinkedList(Thing data) { this.size = 1; this.head = new Node<Thing>(data); } public int size() { return size; } public void listPrint() { System.out.println(this.head.nodePrint()); } public String stringRepresentation() { return this.head.nodePrint(); } public void add(Thing data) { size++; if(this.head == null) head = new Node<Thing>(data); else this.head.add(data); } public void remove(Thing data) { if(this.head.remove(data)) this.size--; } public void addFirst(Thing data) { if(this.head == null) this.head = new Node<Thing>(data); else { Node<Thing> temp = new Node<Thing>(data); temp.setNext(this.head); this.head = temp; } } } class Rextester { public static void main (String[] args) throws java.lang.Exception { LinkedList<Integer> list = new LinkedList<Integer>(); for(int i = 1; i < 20; i++) { if(i * 10 == 50){ list.add(600); } list.add(i * 10); } list.addFirst(500); list.remove(50); list.remove(600); list.listPrint(); } } // PROGRAM BY 'JAMES COLLINS'
run
|
edit
|
history
|
help
0
jb12.0 threads.enums
Integer to English Words
single_digit
exp5
Coding Numbers - Duplicate - Array
parameter const
ArrayOperation
Frequency
javaSuperSub
Preference List