Computing systems are concerned with the storage and retrieval of information.For systems to be economical the data must be organized (into data structures) in such a way as to support efficient manipulation (by algorithms).Choosing the wrong algorithms and data structures makes a program slow at best and unmaintainable and insecure at worst.
Lets take an example of purchase order of an e-commerce store. If purchase order implemented as custom linkedlist then we can take benefit of
a) constant-time insertions/deletions from the list
b) maintaining many versions of purchase order without any additional complexity.
c) to insert items in the middle of the list (such as a priority queue)
d) many priority queue based operation
e) In-memory operations
below is one such implementation
package com;
import java.util.Iterator;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList lnkls = new LinkedList();
lnkls.add(11);
lnkls.add(12);
lnkls.add(13);
lnkls.add(14);
lnkls.add(15);
lnkls.add(16);
lnkls.add(17);
lnkls.add(18);
System.out.println("Total added items :" + lnkls.size);
Iterator<LinkedList.Node> it = lnkls.iterator();
System.out.println("****** Items of LinkedList ******");
while (it.hasNext()) {
System.out.println(it.next().objValue);
}
}
}
class LinkedList implements Iterable<LinkedList.Node>, Iterator<LinkedList.Node> {
Node head;
Node lastNode;
Node visitorNode;
int size = 0;
int visited = 0;
public LinkedList() {
}
public void add(Object o) {
Node newNode = new Node(o);
if (this.head == null) {
this.lastNode = this.head = newNode;
} else {
this.lastNode.next = newNode;
this.lastNode = newNode;
}
size++;
System.out.println("Added " + newNode);
}
@Override
public Iterator<LinkedList.Node> iterator() {
visitorNode = lastNode = head;
return this;
}
@Override
public boolean hasNext() {
if (visited++ < size)
return true;
else
return false;
}
@Override
public Node next() {
Node visitedNode = lastNode;
lastNode = lastNode.next;
return visitedNode;
}
class Node {
Object objValue;
Node next;
public Node(Object o) {
this.objValue = o;
this.next = null;
}
@Override
public String toString() {
return objValue.toString();
}
}
// additional operational and supportive APIs for custom linkedlist
}