Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve using Java programming language. //************************** SLL.java ********************************* // a generic singly linked list class public class SLL { private class SLLNode { private T

Solve using Java programming language.

//************************** SLL.java *********************************

// a generic singly linked list class

public class SLL {

private class SLLNode {

private T info;

private SLLNode next;

public SLLNode() {

this(null,null);

}

public SLLNode(T el) {

this(el,null);

}

public SLLNode(T el, SLLNode ptr) {

info = el; next = ptr;

}

}

protected SLLNode head, tail;

public SLL() {

head = tail = null;

}

public boolean isEmpty() {

return head == null;

}

public void addToHead(T el) {

head = new SLLNode(el,head);

if (tail == null)

tail = head;

}

public void addToTail(T el) {

if (!isEmpty()) {

tail.next = new SLLNode(el);

tail = tail.next;

}

else head = tail = new SLLNode(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 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 void delete(T el) { // delete the node with an element el;

if (!isEmpty())

if (head == tail && el.equals(head.info)) // if only one

head = tail = null; // node on the list;

else if (el.equals(head.info)) // if more than one node on the list;

head = head.next; // and el is in the head node;

else { // if more than one node in the list

SLLNode 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) { // if el was found;

pred.next = tmp.next;

if (tmp == tail) // if el is in the last node;

tail = pred;

}

}

}

@Override

public String toString() {

if(head == null)

return "[ ]";

String str = "[ ";

SLLNode tmp = head;

while(tmp != null){

str += tmp.info + " ";

tmp = tmp.next;

}

return str+"]";

}

public boolean contains(T el) {

if(head == null)

return false;

SLLNode tmp = head;

while(tmp != null){

if(tmp.info.equals(el))

return true;

tmp = tmp.next;

}

return false;

}

public int size(){

if(head == null)

return 0;

int count = 0;

SLLNode p = head;

while(p != null) {

count++;

p = p.next;

}

return count;

}

}

Given the code above, solve the following tasks (please show the solution for every task clearly):

image text in transcribed

For part (c) this is the code for the test program:

public class SLL_Driver {

public static void main(String[] args) {

SLL myList = new SLL();

String[] cityNames = {"Jubail", "Riyadh", "Abha", "Dammam", "Taif"};

for(int i = 0; i

myList.addToHead(cityNames[i]);

System.out.println("The list is: " + myList);

System.out.println("It is " + myList.contains("Dammam") + " that the list contains Dammam.");

System.out.println("It is " + myList.contains("Jeddah") + " that the list contains Jeddah.");

}

}

1. (a) Comment the iterative public boolean contains(Element e) method of the given SLL T class then implement it as a recursive method. Use an appropriate helper method in your solution. (b) Comment the iterative public String toString( ) method of the given SLL T class then implement it as a recursive method. Use appropriate helper method in your solution. (c) Use the given test program to test your recursive methods. Sample program run: The list is: [ Taif Dammam Abha Riyadh Jubail ] It is true that the list contains Dammam. It is false that the list contains Jeddah

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions

Question

5 What are the main aims of talent management?

Answered: 1 week ago