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

You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
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++
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
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...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
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. }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT