import java.util.NoSuchElementException;
public class LinkedStringList {
private Node first;
private Node currentNode;
private int length;
class Node
{
private String data;
private Node next;
public void printNodeData()
{
System.out.println("Node data: " + data);
}
public Node getNext()
{
return next;
}
}
public LinkedStringList()
{
first = null;
currentNode = null;
length = 0;
}
public void addFirst(String value)
{
// create the Node and set its value
Node newNode = new Node();
newNode.data = value;
// if newNode is the first node, this will be null
// otherwise it will point to the former "first" now
newNode.next = first;
// set our "first" Node to the Node we just created
first = newNode;
currentNode = newNode;
length++;
}
public void setFirstValue(String value)
{
first.data = value;
}
public void setCurrentValue(String value)
{
currentNode.data = value;
}
public void moveNext()
{
if (currentNode.next == null)
{
throw new NoSuchElementException();
}
else
{
currentNode = currentNode.next;
}
}
public void moveFirst()
{
currentNode = first;
}
public boolean isEmpty()
{
return (first == null);
}
public int getLength()
{
return length;
}
public String getFirstValue()
{
if (first == null)
{
throw new NoSuchElementException();
}
else
{
return first.data;
}
}
public String getCurrentValue()
{
if (currentNode == null)
{
throw new NoSuchElementException();
}
else
{
return currentNode.data;
}
}
public void displayList()
{
Node currentNode = first;
System.out.println("List contents:");
while (currentNode != null)
{
currentNode.printNodeData();
currentNode = currentNode.getNext();
}
}
public void add(String value) {
Node newNode = new Node();
newNode.data = value;
newNode.next = null;
if (first == null || currentNode == null) {
// create the Node and set its value
// if newNode is the first node, this will be null
// otherwise it will point to the former "first" now
newNode.next = first;
// set our "first" Node to the Node we just created
first = newNode;
} else {
Node curr = first;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
}
currentNode = newNode;
length++;
}
public void removeFirst() {
if (first == null)
{
throw new NoSuchElementException();
} else if (first == currentNode) {
first = first.next;
currentNode = first;
} else {
first = first.next;
}
}
public void remove() {
if (first == null)
{
throw new NoSuchElementException();
} else if (first.next == null) {
first = null;
currentNode = null;
} else {
Node curr = first, prev = null;
while (curr != currentNode) {
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
currentNode = prev;
}
length--;
}
public int indexOf(String value) {
Node curr = first;
int index = -1;
while (curr != null) {
++index;
if (curr.data.equals(value))
return index;
curr = curr.next;
}
return index;
}
public void sortAscending() {
if (length > 1) {
for (int i = 0; i < length; i++) {
Node currentNode = first;
Node next = first.next;
for (int j = 0; j < length - 1; j++) {
if (currentNode.data.compareTo(next.data) > 0) {
String temp = currentNode.data;
currentNode.data = next.data;
next.data = temp;
}
currentNode = next;
next = next.next;
}
}
}
}
public static void main(String[] args) {
LinkedStringList list = new LinkedStringList();
list.add("First");
list.add("Second");
list.add("Third");
list.displayList();
System.out.println("---------After Modification:--------");
list.moveFirst();
list.setCurrentValue("Red");
list.moveNext();
list.setCurrentValue("Green");
list.moveNext();
list.setCurrentValue("Blue");
list.displayList();
System.out.println("Index of: " + list.indexOf("Green"));
list.displayList();
list.sortAscending();
System.out.println("---------After Sort:--------");
list.displayList();
System.out.println("---------After Removing Twice:--------");
list.remove();
list.remove();
list.displayList();
}
}
Explanation: