Created
November 7, 2015 12:39
-
-
Save hailpam/b720b42f8ce7d5943e92 to your computer and use it in GitHub Desktop.
Abstract Generic Class defining the LinkedList ADT interface.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.pm.algorithms; | |
/** | |
* Abstract class defining a linked list interface. | |
* | |
* @param <T> | |
* Type to use for Nodes' values | |
* | |
* @author Paolo Maresca <plo.maresca@gmail.com> | |
*/ | |
public abstract class LinkedList<T extends Comparable<T>> | |
{ | |
protected Node head; | |
protected StringBuilder serialized; | |
public abstract void iterativeReverse(); | |
public abstract void recursiveReverse(); | |
public abstract void insert(T value); | |
public abstract Node search(T value); | |
public abstract Node delete(T value); | |
public abstract T min(); | |
public abstract T max(); | |
public LinkedList() { serialized = new StringBuilder(); } | |
public String printRecursive() | |
{ | |
serialized.delete(0, serialized.toString().length()); | |
if(head == null) | |
return serialized.toString(); | |
else | |
return _print(head); | |
} | |
private String _print(Node elem) | |
{ | |
serialized.append(elem.value); | |
serialized.append(" "); | |
if(elem.next == null) | |
return serialized.toString(); | |
else | |
return _print(elem.next); | |
} | |
public String printIterative() | |
{ | |
serialized.delete(0, serialized.toString().length()); | |
if(head == null) | |
return serialized.toString(); | |
Node successor = head; | |
do { | |
serialized.append(successor.value); | |
serialized.append(" "); | |
} while((successor = successor.next) != null); | |
return serialized.toString(); | |
} | |
public static class Node<T> | |
{ | |
public T value; | |
public Node next; | |
public Node(T value) { this.value = value; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment