Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Basic LinkedList in Java (rewritten using for-loops)
/* 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. */ import java.util.*; import java.lang.*; import java.io.*; class Node<Thing> { 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 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) { if(head == null) { head = new Node<Thing>(data); return; } Node<Thing> h; for(h = head; !h.nextIsNull(); h = h.next){} if(h.next == null) h.next = new Node<Thing>(data); else if(h.next.data == null) h.next.data = data; size++; } public void remove(Thing data) { if(head.data == data){ head = head.next; return; } Node<Thing> h, toRemove; for(h = head; !h.nextIsNull(); h = h.next) { if(h.next.data.toString().equals(data.toString())) { h.next = h.next.next; size --; break; } } } public void addFirst(Thing data) { if(this.head == null) this.head = new Node<Thing>(data); else { Node<Thing> temp = new Node<Thing>(data); temp.next = 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
JAVA # Rastgele 15 sayıdan oluşan bir dizinin EK-EB ve Ortalamasını bulan Java Kodu
Dynamic Array
LeetCode 121 - Best time to buy and sell stock - O(n) solution
LCM of 2 numbers
program to count number of lines
right swastik
Java
Java Array List
1.5
Triangle