In: Computer Science
c.
You are given the following Java files:
In SLL class add the following method:
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
------------------------------------------------------------------------------------------------------------------
.....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;
}
}
}
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.