Friday, January 19, 2018

[Program]Generic Implementation of custom linked list in java

Data structure is one of the most important in computer science . There are so many data structure are there
if you talk about most important from all the important one is Linked List.

LinkedList is a data structure where there are two block of node,one is data block and second one is for address block , the address block store the address of next node.



Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.(source geekforgeeks )
source : wikipedia
Now let see if we want to write our own linked list using generic.


package javatest;
/**
* Interface containing the important methods
* @author Rajendra
*
* @param <T>
*/
interface List<T>{
boolean add(T data);
boolean remove(T data);
T get(int index);
int size();
void printNodes();
}
/**
* This class implements the list interface and it handles the linked list functionality
* @author Rajendra
*
* @param <T>
*/
class LinkedList<T> implements List<T> {
/**
* This class is private as this is not exposed outside the linked list class
* @author Rajendra
*
*/
private class Node{
private final T data;
private Node next;
public Node(T data) {
super();
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public T getData() {
return data;
}
}
//Represent the first element of the linked list
private Node head;
//Represent the last element of the linked list
private Node tail;
//Represent the list size
private int nodeCounter;
@Override
public boolean add(T data) {
boolean addFlag = false;
//Creating new node with given data
Node node = new Node(data);
//Linked list is empty, adding first element
if(this.head==null) {
head = node;
//Head and tail pointing to the same node
tail=head;
nodeCounter++;
addFlag = true;
}else {
//Linked list is not empty, adding new element to the list
tail.setNext(node);
tail = node;
nodeCounter++;
addFlag = true;
}
return addFlag;
}
@Override
public boolean remove(T data) {
boolean removeFlag = false;
//Checking whether list is empty or not
if(head==null) {
throw new IndexOutOfBoundsException("List is empty");
}
//Deleting head element
if(head.getData().equals(data)) {
Node temp = head;
head = temp.getNext();
temp = null;
this.nodeCounter--;
removeFlag = true;
}else {
Node temp = head.getNext();
Node prev = head;
// Iterating over the list
while(temp!=null) {
// Checking the data for node deletion
if(temp.getData().equals(data)) {
prev.setNext(temp.getNext());
temp = null;
this.nodeCounter--;
removeFlag = true;
break;
}
prev = temp;
temp = temp.getNext();
}
}
return removeFlag;
}
@Override
public void printNodes() {
Node temp = head;
while(temp!=null) {
System.out.print(temp.getData()+" ");
temp= temp.getNext();
}
System.out.println();
}
@Override
public int size() {
return this.nodeCounter;
}
@Override
public T get(int index) {
T data = null;
Node temp = head;
int cntr = 0;
if(head==null)
throw new IndexOutOfBoundsException("List is empty");
if(index>=this.nodeCounter)
throw new IndexOutOfBoundsException("Index greater than list size");
//If given index is of head node then return the data
if(index==cntr)
data = head.getData();
//If given index is of tail node then return data
else if(index==(nodeCounter-1))
data = tail.getData();
else {
//Checking the given index with the node index
while(temp!=null) {
if(cntr==index) {
data = temp.getData();
break;
}
temp= temp.getNext();
cntr++;
}
}
return data;
}
}
/**
* This the test class for linked list implementation
* @author Rajendra
*
*/
public class OwnLinkedListTest {
public static void main(String[] args) {
List<Integer> l1 = new LinkedList<Integer>();
l1.add(1);
l1.add(2);
l1.add(3);
l1.add(4);
l1.add(5);
l1.add(6);
System.out.println("Get 0th element : "+l1.get(0));
System.out.println("Get inbetween index element : "+l1.get(3));
System.out.println("Get last element : "+l1.get(5));
System.out.println("Int list size : "+l1.size());
l1.printNodes();
System.out.println("Remove head : "+l1.remove(1));
System.out.println("Int list size : "+l1.size());
l1.printNodes();
System.out.println("Remove tail : "+l1.remove(6));
System.out.println("Int list size : "+l1.size());
l1.printNodes();
System.out.println("Remove inbetween node : "+l1.remove(3));
System.out.println("Int list size : "+l1.size());
l1.printNodes();
System.out.println("Get element index greater than size : "+l1.get(5));
}
}
If you have any problem you can leave us a comment ,
This program is contributed by Rajendra Tighage

Thanks for Reading
Noeik