Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active November 9, 2016 21:29
Show Gist options
  • Save snarkbait/e4a6ee7a7ef89b28b688 to your computer and use it in GitHub Desktop.
Save snarkbait/e4a6ee7a7ef89b28b688 to your computer and use it in GitHub Desktop.
Sorted Doiubly-Linked List example for /r/javaexamples
public class Inventory implements Comparable<Inventory>
{
private String item;
private int qty;
private float price;
public Inventory(String item, int qty, float price)
{
this.item = item;
this.qty = qty;
this.price = price;
}
public String getItem()
{
return item;
}
public float getTotal()
{
return qty * price;
}
public String toString()
{
return "===================\nItem: " + item + "\n" + "Quantity: " + qty + "\n"
+ "Price: " + price + "\n====================\n";
}
public String toCSVString()
{
return item + "," + qty + "," + price;
}
@Override
public int compareTo(Inventory obj)
{
return this.item.compareTo(obj.getItem());
}
}
public class SortedDoubleLinkedList<E extends Comparable<E>>
{
// Nodes for the first and last element
Node head;
Node tail;
// list size
int length;
public SortedDoubleLinkedList()
{
this.length = 0;
}
// add function that inserts elements sorted
public void add(E value)
{
// first insert
if (length==0)
{
head = new Node(value, null, null);
tail = head;
length++;
return;
}
else
{
if (value.compareTo(head.getValue()) < 0)
{
// insert before head, make new head
// set next to head, previous to null
Node newNode = new Node(value, head, null);
// point old head previous to new node
head.setPrevious(newNode);
// make new node head
head = newNode;
length++;
return;
}
else
{
Node current = head.getNext();
while (current != null)
{
if (value.compareTo(current.getValue()) <= 0)
{
// insert before current
// make new nodes next = current, previous = current.previous
Node newNode = new Node(value, current, current.getPrevious());
// point node before insert's next to newnode
current.getPrevious().setNext(newNode);
// make new node previous to current
current.setPrevious(newNode);
length++;
return;
}
current = current.getNext();
}
// add to tail
Node newNode = new Node(value, null, tail);
tail.setNext(newNode);
tail = newNode;
length++;
return;
}
}
}
public void remove(E value)
{
Node found = find(value);
if (found != null)
// node exists
{
// check for head
if (found.getPrevious() == null)
{
found.getNext().setPrevious(null);
head = found.getNext();
}
else
{
// check for tail
if (found.getNext() == null)
{
found.getPrevious().setNext(null);
tail = found.getPrevious();
}
else
{
// fix pointers
found.getPrevious().setNext(found.getNext());
found.getNext().setPrevious(found.getPrevious());
}
}
length--;
}
}
// private method to find node based on value
// used by remove() and contains()
// If using complex objects, you will
// have to override equals to search by
// a single member field
private Node find(E value)
{
Node current = head;
while (current != null)
{
if (current.getValue().equals(value)) return current;
current = current.getNext();
}
return null;
}
public boolean contains(E value)
{
Node found = find(value);
if (found == null) return false;
return true;
}
public E get(int index)
{
if (index < length)
{
Node current = head;
for (int i = 0; i < length; i++)
{
if (i == index) return current.getValue();
current = current.getNext();
}
return null;
}
return null;
}
public E getFirst()
{
return head.getValue();
}
public E getLast()
{
return tail.getValue();
}
public String toString()
{
String result = "";
Node current = head;
result += "Count = " + length + "\n";
while (current != null)
{
result += "" + current.getValue().toString() + "\n";
current = current.getNext();
}
return result;
}
public String toStringDescending()
{
String result = "";
Node current = tail;
result += "Count = " + length + "\n";
while (current != null)
{
result += "" + current.getValue().toString() + "\n";
current = current.getPrevious();
}
return result;
}
// recursive display functions
public String toStringRecursive()
{
return "Count = " + length + "\n" + toStringRecursive(head);
}
public String toStringRecursive(Node node)
{
if (node==null) return "";
String retval = node.getValue().toString() + "\n";
return retval + toStringRecursive(node.getNext());
}
// inner class for nodes
class Node
{
private E value;
private Node next;
private Node previous;
public Node(E value, Node next, Node previous)
{
this.value = value;
this.next = next;
this.previous = previous;
}
public E getValue() { return value; }
public void setValue(E value) { this.value = value; }
public Node getNext() { return next; }
public void setNext(Node node) { this.next = node; }
public Node getPrevious() { return previous; }
public void setPrevious(Node node) { this.previous = node; }
}
}
public class TestDLL
{
public static void main(String[] args) {
// Integer test
SortedDoubleLinkedList<Integer> sdll = new SortedDoubleLinkedList<>();
sdll.add(234);
sdll.add(3455);
sdll.add(123);
sdll.add(5000);
sdll.add(234);
sdll.add(0);
System.out.println("List in order:");
System.out.println(sdll);
// test reverse order toString function
System.out.println("List in descending order:");
System.out.println(sdll.toStringDescending());
// test contains()
System.out.println("List contains item value of '3455' = " + sdll.contains(3455));
// test remove()
System.out.println("Removing an item");
sdll.remove(3455);
System.out.println("List contains item value of '3455' = " + sdll.contains(3455));
System.out.println("List in order:");
System.out.println(sdll);
// test getfirst() & getLast()
System.out.println("First:" + sdll.getFirst());
System.out.println("Last:" + sdll.getLast());
// test Strings
SortedDoubleLinkedList<String> list2 = new SortedDoubleLinkedList<>();
list2.add("Fish");
list2.add("Meat");
list2.add("Argue");
System.out.println("List of strings");
System.out.println(list2);
System.out.println("List contains 'Fish' = " + list2.contains("Fish"));
System.out.println();
// test Doubles
SortedDoubleLinkedList<Double> dub = new SortedDoubleLinkedList<>();
dub.add(120.003);
dub.add(3.1415967);
dub.add(27.3);
System.out.println("List of doubles:");
// test recursive display function
System.out.println(dub.toStringRecursive());
System.out.println();
// test object
SortedDoubleLinkedList<Inventory> objects = new SortedDoubleLinkedList<>();
objects.add(new Inventory("Quixotic Journey", 10, 20.00f));
objects.add(new Inventory("Purple Suit", 5, 199.99f));
objects.add(new Inventory("Alphabet Soup", 100, 0.99f));
objects.add(new Inventory("Maple Syrup",20, 5.99f));
System.out.println("Object list:");
System.out.println(objects);
System.out.println(objects.toStringDescending());
System.out.println("Object at index 0 = \n" + objects.get(0));
System.out.println("Remove object at index 1");
objects.remove(objects.get(1));
System.out.println(objects);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment