Created
November 3, 2012 13:45
-
-
Save yssharma/4007421 to your computer and use it in GitHub Desktop.
A linked list implementation in java providing few common utility methods (Confused Coders)
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
class Node { | |
int data; | |
Node next; | |
public Node(int data) { | |
this.data = data; | |
} | |
} | |
public class LinkedList { | |
private Node root; | |
public void addAtEnd(int data) { | |
if (root == null) { | |
root = new Node(data); | |
return; | |
} | |
Node tmp = root; | |
while (tmp.next != null) { | |
tmp = tmp.next; | |
} | |
tmp.next = new Node(data); | |
} | |
public void addAtFront(int data) { | |
Node newRoot = new Node(data); | |
newRoot.next = root; | |
root = newRoot; | |
} | |
public void addAtIndex(int data, int index) { | |
if (root == null) | |
return; | |
Node tmp = root; | |
while (index > 1) { | |
if (tmp == null) | |
return; | |
tmp = tmp.next; | |
index--; | |
} | |
Node nextNode = tmp.next; | |
tmp.next = new Node(data); | |
tmp.next.next = nextNode; | |
} | |
public int removeFromFront() { | |
int data = root.data; | |
root = root.next; | |
return data; | |
} | |
public int removeFromEnd() { | |
Node tmp = root; | |
while (tmp.next.next != null) { | |
tmp = tmp.next; | |
} | |
int data = tmp.next.data; | |
tmp.next = null; | |
return data; | |
} | |
public int removeFromIndex(int index) { | |
int data = 0; | |
Node tmp = root; | |
if (index < 0) | |
return -1; | |
if (index == 0) { | |
data = root.data; | |
root = root.next; | |
return data; | |
} | |
while (index > 0) { | |
if (tmp == null) { | |
return -1; | |
} | |
tmp = tmp.next; | |
index--; | |
} | |
data = tmp.data; | |
tmp.data = tmp.next.data; | |
tmp.next = tmp.next.next; | |
return data; | |
} | |
public void printList() { | |
if (root == null) { | |
System.out.println("List is Empty !!"); | |
return; | |
} | |
Node tmp = root; | |
System.out.println(); | |
while (tmp != null) { | |
System.out.print(">" + tmp.data); | |
tmp = tmp.next; | |
} | |
} | |
public static void main(String[] args) { | |
LinkedList list = new LinkedList(); | |
list.printList(); | |
list.addAtEnd(1); | |
list.printList(); | |
list.addAtFront(2); | |
list.printList(); | |
list.addAtEnd(3); | |
list.printList(); | |
list.addAtIndex(4, 2); | |
list.printList(); | |
list.addAtIndex(5, 4); | |
list.printList(); | |
list.addAtEnd(6); | |
list.printList(); | |
list.addAtFront(7); | |
list.printList(); | |
list.addAtEnd(8); | |
list.printList(); | |
list.removeFromEnd(); | |
list.printList(); | |
list.removeFromEnd(); | |
list.printList(); | |
list.removeFromFront(); | |
list.printList(); | |
list.removeFromIndex(0); | |
list.printList(); | |
list.removeFromIndex(2); | |
list.printList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment