Question

In: Computer Science

c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...

c.

You are given the following Java files:

  • SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class.
  • TestIntegerSLL.java that tests the SLL class by using a linked list of Integer.

In SLL class add the following method:                                                                   

  1. public void moveToEnd (int i)

It will move the element at the i -th position to the end of the list.

You can assume i to be within the list, and that the first element has the position 0.

Suppose the list contains: 1,3,6,7,8,9

When calling the method with (2) as passed prameter, the return value will be 1,3,7,8,9,6

           ------------------------------------------------------------------------------------------------------------------

  1. Update the main method inside TestIntegerSLL class, to have additional options to test your methods above similar to the following:                                  

.....other options..

3. Move element in given index to the end of List.

4. Quit

----------------------------------------

This file SLL.java:

public class SLL<T> {

protected SLLNode<T> head, tail;

public SLL() {
head = tail = null;
}

public boolean isEmpty() {
return head == null;
}

public void addToHead(T el) {
if (!isEmpty()) {
head = new SLLNode<T>(el, head);
} else {
head = tail = new SLLNode<T>(el);
}
}

public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
} else {
head = tail = new SLLNode<T>(el);
}
}

public T deleteFromHead() { // delete the head and return its info;
if (isEmpty()) {
return null;
}
T el = head.info;
if (head == tail) // if only one node on the list;
{
head = tail = null;
} else {
head = head.next;
}
return el;
}

public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty()) {
return null;
}
T el = tail.info;
if (head == tail) // if only one node in the list;
{
head = tail = null;
} else { // if more than one node in the list,
SLLNode<T> tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next);
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}

public T delete(T el) {
if (isEmpty()) {
return null;
} else if (head.info.equals(el)) {
return deleteFromHead();
} else if (tail.info.equals(el)) {
return deleteFromTail();
} else {
SLLNode<T> pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next;
tmp != null && !tmp.info.equals(el);
pred = pred.next, tmp = tmp.next);
if (tmp != null) {
pred.next = tmp.next;
return tmp.info;
} else {
return null;
}
}
}

public void printAll() {
for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next) {
System.out.print(tmp.info + " ");
}
}

public T find(T el) {
SLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
if (tmp != null) // if found
{
return tmp.info;
} else {
return null;
}
}

//This is an inner class impleming the SLLNode
public class SLLNode<T> {

public T info;
public SLLNode<T> next;

public SLLNode() {
this(null, null);
}

public SLLNode(T el) {
this(el, null);
}

public SLLNode(T el, SLLNode<T> ptr) {
info = el;
next = ptr;
}
}
}

Solutions

Expert Solution

Code


public class SLL<T>
{
   protected SLLNode<T> head, tail;

   public SLL()
   {
       head = tail = null;
   }

   public boolean isEmpty()
   {
       return head == null;
   }

   public void addToHead(T el)
   {
       if (!isEmpty()) {
           head = new SLLNode<T>(el, head);
       } else {
           head = tail = new SLLNode<T>(el);
       }
   }

   public void addToTail(T el) {
       if (!isEmpty()) {
           tail.next = new SLLNode<T>(el);
           tail = tail.next;
       } else {
           head = tail = new SLLNode<T>(el);
       }
   }

   public T deleteFromHead() { // delete the head and return its info;
       if (isEmpty()) {
           return null;
       }
       T el = head.info;
       if (head == tail) // if only one node on the list;
       {
           head = tail = null;
       } else {
           head = head.next;
       }
       return el;
   }

   public T deleteFromTail() { // delete the tail and return its info;
       if (isEmpty()) {
           return null;
       }
       T el = tail.info;
       if (head == tail) // if only one node in the list;
       {
           head = tail = null;
       } else { // if more than one node in the list,
           SLLNode<T> tmp; // find the predecessor of tail;
           for (tmp = head; tmp.next != tail; tmp = tmp.next);
               tail = tmp; // the predecessor of tail becomes tail;
           tail.next = null;
       }
       return el;
   }

   public T delete(T el)
   {
       if (isEmpty()) {
           return null;
       } else if (head.info.equals(el)) {
           return deleteFromHead();
       } else if (tail.info.equals(el)) {
           return deleteFromTail();
       } else {
           SLLNode<T> pred, tmp;// and el is in a nonhead node;
           for (pred = head, tmp = head.next;tmp != null && !tmp.info.equals(el);
               pred = pred.next, tmp = tmp.next);
           if (tmp != null) {
               pred.next = tmp.next;
               return tmp.info;
           } else {
               return null;
           }
       }
   }

   public void printAll()
   {
       for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next) {
           System.out.print(tmp.info + " ");
       }
   }

   public T find(T el)
   {
       SLLNode<T> tmp;
           for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
           if (tmp != null) // if found
           {
                   return tmp.info;
           } else {
               return null;
           }
       }

   //This is an inner class impleming the SLLNode
   public class SLLNode<T> {
       public T info;
       public SLLNode<T> next;

       public SLLNode() {
           this(null, null);
       }

       public SLLNode(T el) {
           this(el, null);
       }

       public SLLNode(T el, SLLNode<T> ptr) {
           info = el;
           next = ptr;
       }
   }
   public void moveToEnd (int i)
   {
       T info=null;
       int j=0;
       for (SLLNode<T> tmp = head ; j<=i; tmp = tmp.next,j++) {
           info=tmp.info;
       }
       this.delete(info);
       this.addToTail(info);
   }

}

As you not provided he code for TestIntegerSLL.java so for testing purpose i have made a code

TestIntegerSLL.java


public class TestIntegerSLL {

   public static void main(String[] args) {
      
       SLL<Integer> list=new SLL<>();
       list.addToHead(1);
       list.addToTail(3);
       list.addToTail(6);
       list.addToTail(7);
       list.addToTail(8);
       list.addToTail(9);
       list.printAll();
       System.out.println("\nAfter moving element at index 2 to end");
      
       list.moveToEnd(2);
       list.printAll();
   }

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT