In: Computer Science
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } Create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is a constant defined in the ManyLinkedLists class. The identifier of the constants should describe a type of linked list. When the createLinkedList method is called, it should return a linked list object of the type identified by the constant. For example: DoubleEndedList del = ManyLinkedLists.createLinkedList(ManyLinkedLists.DOUBLEENDEDLIST); Give the createLinkedList method the ability to return the linked lists described below: A double-ended linked list. A doubly linked list.
Node class
package ques9;
public class Node {
Integer data;
Node next;
//for doublyLL
Node previous;
public Node(Integer data) {
super();
this.data = data;
next = null;
previous=null;
}
public Node(Integer data, Node next, Node previous)
{
super();
this.data = data;
this.next = next;
this.previous = previous;
}
public Integer getData() {
return data;
}
public void setData(Integer data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public void display() {
System.out.println(data);;
}
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
}
DoublyLL class
package ques9;
public class DoublyLinkedList {
Node first;
Node last;
int size;
public DoublyLinkedList() {
super();
this.first = null;
this.last = null;
this.size = 0;
}
/* Function to check is list is empty */
public boolean isEmpty()
{
return(first==null);
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at beginning */
public void insertAtStart(int data)
{
Node newNode = new Node(data);
if(first == null)
{
first = newNode;
last = newNode;
}
else
{
first.setPrevious(newNode);
newNode.setNext(first);
first = newNode;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int data)
{
Node newNode = new Node(data);
if(first == null)
{
first = newNode;
last = first;
}
else
{
newNode.setPrevious(last);
last.setNext(newNode);
last = newNode;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int data , int pos)
{
Node newNode = new Node(data);
if (pos == 1)
{
insertAtStart(data);
return;
}
Node current = first;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node temp = current.getNext();
current.setNext(newNode);
newNode.setPrevious(current);
newNode.setNext(temp);
temp.setPrevious(newNode);
}
current = current.getNext();
}
size++ ;
}
/* Function to delete node at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
if (size == 1)
{
first = null;
last = null;
size = 0;
return;
}
first = first.getNext();
first.setPrevious(null);
size--;
return ;
}
if (pos == size)
{
last = last.getPrevious();
last.setNext(null);
size-- ;
}
Node current = first.getNext();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = current.getPrevious();
Node n = current.getNext();
p.setNext(n);
n.setPrevious(p);
size-- ;
return;
}
current = current.getNext();
}
}
/* Function to display status of list */
public void display()
{
System.out.print(" Doubly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (first.getNext() == null)
{
first.display();
return;
}
Node current = first;
System.out.print(first.getData()+ " <-> ");
current = first.getNext();
while (current.getNext() != null)
{
System.out.print(current.getData()+ " <-> ");
current = current.getNext();
}
current.display();
}
}
DoublEnded LL class
package ques9;
import ques7.Node;
public class DoubleEndedLL {
Node first;
Node last;
int size;
public DoubleEndedLL() {
super();
this.first = null;
this.last = null;
this.size = 0;
}
/* Function to check is list is empty */
public boolean isEmpty()
{
return(first==null);
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at beginning */
public void insertAtStart(int data)
{
Node newNode = new Node(data);
if(first == null)
{
first = newNode;
last = newNode;
}
else
{
newNode.setNext(first);
first = newNode;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int data)
{
Node newNode = new Node(data);
if(first == null)
{
first = newNode;
last = first;
}
else
{
last.setNext(newNode);
last = newNode;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int data , int pos)
{
Node newNode = new Node(data);
if (pos == 1)
{
insertAtStart(data);
return;
}
Node current = first;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node temp = current.getNext();
current.setNext(newNode);
newNode.setNext(temp);
}
current = current.getNext();
}
size++ ;
}
/* Function to delete node with value=key */
public void deletekey(int key)
{
Node current=first;
Node previous=null;
//if key is the first element of
the list
if(current!=null &&
current.getData()==key)
{first=current.getNext();
System.out.println(key+" is deleted");
}
else
{
//search if key
is present in the list
while(current!=null && current.getData()!=key)
{
previous=current;
current=current.getNext();
}
//if key is
absent
if(current==null)
System.out.println("Key not present");
else
{
previous.setNext(current.getNext());
System.out.println(key+" is deleted");
if(current==last)
last=previous;
}
}
}
/* Function to display status of list */
public void display()
{
System.out.println("Double Ended Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (first.getNext() == null)
{
first.display();
return;
}
Node current = first;
while (current.getNext() != null)
{
System.out.print(current.getData()+ " - ");
current = current.getNext();
}
current.display();
}
}
ManyLinkedList class
package ques9;
public class ManyLinkedLists {
public static final String DOUBLEENDEDLIST =
"doubleended";
public static final String DOUBLYlINKEDlIST =
"doubly";
public static Object createLinkedList(String
type)
{
if(type.equalsIgnoreCase(DOUBLEENDEDLIST))
{
DoubleEndedLL
list=new DoubleEndedLL();
return
list;
}
else
if(type.equalsIgnoreCase(DOUBLYlINKEDlIST))
{
DoublyLinkedList
list=new DoublyLinkedList();
return
list;
}
return null;
}
}
ManyLinkedListsDriver class
package ques9;
public class ManyLinkedListDriver {
public static void main(String[] args) {
// TODO Auto-generated method
stub
DoubleEndedLL del = (DoubleEndedLL)
ManyLinkedLists.createLinkedList(ManyLinkedLists.DOUBLEENDEDLIST);
System.out.println("Demostrating
Double ended linked list");
boolean empty=del.isEmpty();
if(empty)
System.out.println("Empty list");
else
System.out.println("not empty list");
del.insertAtStart(10);
del.insertAtStart(2);
del.insertAtStart(55);
del.insertAtStart(11);
del.insertAtStart(76);
del.display();
System.out.println("Inserting at
last");
del.insertAtEnd(89);
del.display();
System.out.println("Size of
list"+del.getSize());
System.out.println("Deleting
keys");
del.deletekey(89);
del.display();
del.deletekey(55);
del.display();
del.deletekey(76);
del.display();
empty=del.isEmpty();
if(empty)
System.out.println("Empty list");
else
System.out.println("not empty list");
System.out.println("Demostrating
Doubly linked list");
DoublyLinkedList obj=
(DoublyLinkedList)
ManyLinkedLists.createLinkedList(ManyLinkedLists.DOUBLYlINKEDlIST);
empty=obj.isEmpty();
if(empty)
System.out.println("Empty list");
else
System.out.println("not empty list");
obj.insertAtStart(24);
obj.insertAtStart(10);
obj.insertAtStart(76);
obj.insertAtStart(23);
obj.insertAtStart(55);
obj.display();
System.out.println("Inserting at
last");
obj.insertAtEnd(33);
obj.display();
System.out.println("Size of
list"+obj.getSize());
System.out.println("Deleting
keys");
obj.deleteAtPos(5);
obj.display();
obj.deleteAtPos(1);
obj.display();
obj.deleteAtPos(2);
obj.display();
empty=obj.isEmpty();
if(empty)
System.out.println("Empty list");
else
System.out.println("not empty list");
}
}
Output
The difference between both the LinkedList is that DoubleEnded LL do not have previous pointer. They have an additional attribute last Node pointer which is the main difference from the simple Linked List class, so someone is able to insert elements to list from both ends of it.
DoublyLinkedList has two pointers previous and next to point to previous and next element. It also has first and last pointer which points to first and last element respectively.