SlideShare a Scribd company logo
Solve using Java programming language.
//************************** SLL.java *********************************
// a generic singly linked list class
public class SLL<T> {
private class SLLNode<T> {
private T info;
private 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;
}
}
protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode<T>(el,head);
if (tail == null)
tail = head;
}
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 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<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) { // 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<T> tmp = head;
while(tmp != null){
str += tmp.info + " ";
tmp = tmp.next;
}
return str+"]";
}
public boolean contains(T el) {
if(head == null)
return false;
SLLNode<T> 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<T> 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):
For part (c) this is the code for the test program:
public class SLL_Driver {
public static void main(String[] args) {
SLL<String> myList = new SLL<String>();
String[] cityNames = {"Jubail", "Riyadh", "Abha", "Dammam", "Taif"};
for(int i = 0; i < cityNames.length; 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.

More Related Content

PDF
Using Java programming language solve the followingSLL CLASS.pdf
PDF
Submit1) Java Files2) Doc file with the following contents.pdf
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
PDF
Solve using java and using this Singly linked list classpublic cl.pdf
PDF
public class SLLT { protected SLLNodeT head, tail; pub.pdf
PDF
Help explain the code with line comments public class CompletedLis.pdf
DOCX
Table.java Huffman code frequency tableimport java.io.;im.docx
Using Java programming language solve the followingSLL CLASS.pdf
Submit1) Java Files2) Doc file with the following contents.pdf
-JAVA-provide a test class that do the required -you may add met.pdf
Use the singly linked list class introduced in the lab to implement .pdf
Solve using java and using this Singly linked list classpublic cl.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
Help explain the code with line comments public class CompletedLis.pdf
Table.java Huffman code frequency tableimport java.io.;im.docx

Similar to Solve using Java programming language- ----------------------------.pdf (20)

PDF
mainpublic class AssignmentThree {    public static void ma.pdf
PDF
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
PDF
So I have this code(StackInAllSocks) and I implemented the method but.pdf
DOCX
Linked lists
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Help I keep getting the same error when running a code. Below is the.pdf
PDF
Help please!!(Include your modified DList.java source code file in.pdf
PDF
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
PDF
You can list anything, it doesnt matter. I just want to see code f.pdf
PDF
137 Lab-2.2.pdf
DOCX
DS Code (CWH).docx
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
Keep getting a null pointer exception for some odd reasonim creati.pdf
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PDF
6. Generics. Collections. Streams
mainpublic class AssignmentThree {    public static void ma.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
Linked lists
import java-util--- public class MyLinkedList{ public static void.pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Help I keep getting the same error when running a code. Below is the.pdf
Help please!!(Include your modified DList.java source code file in.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
137 Lab-2.2.pdf
DS Code (CWH).docx
hi i have to write a java program involving link lists. i have a pro.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
6. Generics. Collections. Streams

More from aksahnan (20)

PDF
Skills drill 4-2 word Building- 72 Unit II Overview of the Human Bod.pdf
PDF
Sketch the region corresponding to the statement P(z-2-6) Left of a va.pdf
PDF
Sixty-year-old Philip has been having difficulty catching his breath f.pdf
PDF
Skeletal System- The Appendicular Skeleton and Joints- Complete skill.pdf
PDF
Sketch the region corresponding to the statement P(z-1-5) Shade v Left.pdf
PDF
Size of Pl- bits Size of P2 - bits Size of Offset - bits.pdf
PDF
Sketch each object- Label and indicate the total magnification- Things.pdf
PDF
Simple GUI using Swing Recreate the GUI using the Swing package (25 po.pdf
PDF
sin(4) in tili 10 sie 30.pdf
PDF
Simulate on a vertical time axis (with events labeled with the senders.pdf
PDF
Singay Copportion hes recorded the following costs for tho vi 101- Toa.pdf
PDF
Since the cell making cortisol has a high concentration of cortisol co.pdf
PDF
sinckA A nak-ereme inreiter atoud cheose kince it offere.pdf
PDF
Since dinosaur cannot swim- explain how a fossil can be found on all c.pdf
PDF
Simulate the SOP expression- f(W-X-Y-Z)-m(1-2-4-5-6-9-12-14).pdf
PDF
SIMs media is specially designed to detect three distinct metabolic pr.pdf
PDF
Simulate differential equation dt2d2y-12dtdy5y Simulate the SIR model.pdf
PDF
Simple Stains are an indirect staining process most widely used differ.pdf
PDF
Situation of facts- Management of conflict situations in the labor are.pdf
PDF
Sigma factors recognize the Pribnow Box and the consensus sequence in.pdf
Skills drill 4-2 word Building- 72 Unit II Overview of the Human Bod.pdf
Sketch the region corresponding to the statement P(z-2-6) Left of a va.pdf
Sixty-year-old Philip has been having difficulty catching his breath f.pdf
Skeletal System- The Appendicular Skeleton and Joints- Complete skill.pdf
Sketch the region corresponding to the statement P(z-1-5) Shade v Left.pdf
Size of Pl- bits Size of P2 - bits Size of Offset - bits.pdf
Sketch each object- Label and indicate the total magnification- Things.pdf
Simple GUI using Swing Recreate the GUI using the Swing package (25 po.pdf
sin(4) in tili 10 sie 30.pdf
Simulate on a vertical time axis (with events labeled with the senders.pdf
Singay Copportion hes recorded the following costs for tho vi 101- Toa.pdf
Since the cell making cortisol has a high concentration of cortisol co.pdf
sinckA A nak-ereme inreiter atoud cheose kince it offere.pdf
Since dinosaur cannot swim- explain how a fossil can be found on all c.pdf
Simulate the SOP expression- f(W-X-Y-Z)-m(1-2-4-5-6-9-12-14).pdf
SIMs media is specially designed to detect three distinct metabolic pr.pdf
Simulate differential equation dt2d2y-12dtdy5y Simulate the SIR model.pdf
Simple Stains are an indirect staining process most widely used differ.pdf
Situation of facts- Management of conflict situations in the labor are.pdf
Sigma factors recognize the Pribnow Box and the consensus sequence in.pdf

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
master seminar digital applications in india
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
master seminar digital applications in india
A systematic review of self-coping strategies used by university students to ...
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Solve using Java programming language- ----------------------------.pdf

  • 1. Solve using Java programming language. //************************** SLL.java ********************************* // a generic singly linked list class public class SLL<T> { private class SLLNode<T> { private T info; private 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; } } protected SLLNode<T> head, tail; public SLL() { head = tail = null; } public boolean isEmpty() { return head == null;
  • 2. } public void addToHead(T el) { head = new SLLNode<T>(el,head); if (tail == null) tail = head; } 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;
  • 3. 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 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<T> pred, tmp;// and el is in a nonhead node; for (pred = head, tmp = head.next; tmp != null && !tmp.info.equals(el);
  • 4. 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<T> tmp = head; while(tmp != null){ str += tmp.info + " "; tmp = tmp.next; } return str+"]"; } public boolean contains(T el) { if(head == null) return false;
  • 5. SLLNode<T> 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<T> 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): For part (c) this is the code for the test program: public class SLL_Driver {
  • 6. public static void main(String[] args) { SLL<String> myList = new SLL<String>(); String[] cityNames = {"Jubail", "Riyadh", "Abha", "Dammam", "Taif"}; for(int i = 0; i < cityNames.length; 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.