/**
* This is fully executable java code. This provides the basic
* functionality of Singly linked list's in Java.
* I have performed following operations on the Singlylinked list
* 1. Add a node to the start of list
* 2. Add a node to the end of the list
* 3. Delete a node from the start of the list
* 4. Delete a node from the end of the list
*
* If you have any queries or improvements,
* please feel free to contact me at
* akashmahakode@yahoo.co.in
*
* ====================================================
* Modification History
* ====================================================
* Author Date Remarks
* ====================================================
* Akash Mahakode 8th February, 2012 Initial Creation
*
* ====================================================
*/
package linkedLists;
/**
*
* @author akashm
* @email akashmahakode@yahoo.co.in
*
* This is Node of Singly Linked List, A Node contains the info(data) and
* the reference to the next Node
*
*/
class SingleLinkedListNode {
Object info;
SingleLinkedListNode next;
public SingleLinkedListNode(Object obj) {
next = null;
info = obj;
}
public SingleLinkedListNode(Object obj, SingleLinkedListNode ptr) {
next = ptr;
info = obj;
}
}
/**
* @author akashm mahakode
*
*/
public class SingleLinkedListDemo {
SingleLinkedListNode head = null;
SingleLinkedListNode tail = null;
/**
* @param args
*/
public static void main(String[] args) {
SingleLinkedListDemo demo = new SingleLinkedListDemo();
demo.addToHead("10");
demo.addToHead("20");
demo.addToHead("30");
demo.addToHead("40");
demo.addToHead("50");
demo.addToHead("60");
demo.addToTail("60");
demo.printList();
demo.deleteFromHead();
System.out.println("\nDeleting the starting node");
demo.printList();
demo.addToTail("70");
demo.addToTail("80");
System.out.println("\nAdded, 70 and 80");
demo.printList();
demo.deleteFromHead();
System.out.println("\nDeleting the starting node");
demo.printList();
demo.deleteFromTail();
System.out.println("\nDeleting the last node");
demo.printList();
demo.deleteFromTail();
System.out.println("\nDeleting the last node");
demo.printList();
}
/**
* Deletes the last node from the linked list
*/
private void deleteFromTail() {
if (head != null) {
for (SingleLinkedListNode tmp = head;;) {
if (tmp.next == tail) {
tail = tmp;
tail.next = null;
break;
} else {
tmp = tmp.next;
}
}
} else {
try {
throw new Exception(
"Linked list is empty, cant delete anything");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Deletes the first node from the linked list
*/
private void deleteFromHead() {
if (head != null) {
head = head.next;
} else {
try {
throw new Exception(
"Linked list is empty, cant delete anything");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Adds the node at the end to the linked list
*/
private void addToTail(String string) {
if (head != null) {
tail.next = new SingleLinkedListNode(string);
tail = tail.next;
} else {
// there is no node in the linked list
head = new SingleLinkedListNode(string);
tail = head;
}
}
/**
* Prints the complete linked list
*/
private void printList() {
System.out.println("Linked list is : ");
for (SingleLinkedListNode tmp = head; tmp != null; tmp = tmp.next) {
System.out.print(tmp.info + " -->");
}
System.out.println();
}
/**
* Adds the node at the start of the linked list
*/
private void addToHead(String string) {
if (head != null) {
head = new SingleLinkedListNode(string, head);
} else {
// there is no node in the linked list
head = new SingleLinkedListNode(string);
tail = head;
}
}
}
Output is -
Linked list is :
60 -->50 -->40 -->30 -->20 -->10 -->60 -->
Deleting the starting node
Linked list is :
50 -->40 -->30 -->20 -->10 -->60 -->
Added, 70 and 80
Linked list is :
50 -->40 -->30 -->20 -->10 -->60 -->70 -->80 -->
Deleting the starting node
Linked list is :
40 -->30 -->20 -->10 -->60 -->70 -->80 -->
Deleting the last node
Linked list is :
40 -->30 -->20 -->10 -->60 -->70 -->
Deleting the last node
Linked list is :
40 -->30 -->20 -->10 -->60 -->