I did not add generics to my code because I am new to Java. I am on my way in learning data structures and algorithms. I just want to make sure that my code is clean and efficient.
package Double_LinkedList;
class Node{
int data;
Node prev;
Node next;
void setData(int data){
this.data = data;
}
int getData(){
return data;
}
void setPrev(Node prev){
this.prev = prev;
}
void setNext(Node next){
this.next = next;
}
Node getPrev(){
return prev;
}
Node getNext(){
return next;
}
Node(int data, Node prev, Node next){
this.data = data;
this.next = next;
this.prev = prev;
}
Node(int data){
this.data = data;
}
Node(){ }
}
public class DoubleLinkedList{
private Node head = null;
private Node tail;
private int length;
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public int getData(Node node){
return node.getData();
}
public int getLength(){
return length;
}
public void insertFront(int data){
Node newNode = new Node(data, null, head);
if(head != null){
head.setPrev(newNode);
}
head = newNode;
if(tail == null){
tail = newNode;
}
length++;
}
public void insertEnd(int data){
Node newNode = new Node(data);
newNode.setNext(null);
if(tail != null){
tail.setNext(newNode);
newNode.setPrev(tail);
tail = newNode;
}
if(head == null){
head = newNode;
}
length++;
}
public void removeFront(){
if(head != null){
head.getNext().setPrev(null);
head = head.getNext();
}else if(head == null){
System.out.println("List is empty");
return ;
}
length--;
}
public void removeEnd(){
if(tail != null){
System.out.println("Tail not null");
Node temp = tail.getPrev();
temp.setNext(null);
tail = temp;
}else{
System.out.println("Tail is null");
}
length--;
}
public void printList(){
Node temp = head;
while(temp != null){
System.out.print(temp.getData()+" ");
temp = temp.getNext();
}
System.out.println("");
}
}