Question

In: Computer Science

Static methods can be called directly from the name of the class that contains the method....

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.

Solutions

Expert Solution

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.


Related Solutions

Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
Design an application with a single class and two static methods: method main and method isLeap....
Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 except if it is divisible by 400. Examples 2007 is not a leap year 2008 is leap year 1700...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT