SlideShare a Scribd company logo
1 of 6
Download to read offline
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

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

mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdffathimafancyjeweller
 
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdfPlease finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdffortmdu
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfaksahnan
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdfasarudheen07
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdffeelinggift
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdffootstatus
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfjyothimuppasani1
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfStewart29UReesa
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfalphaagenciesindia
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfarccreation001
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdffashionbigchennai
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfAroraRajinder1
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 

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

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

More from aksahnan

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

More from aksahnan (20)

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

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

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.