SlideShare a Scribd company logo
1 of 4
Download to read offline
Java problem. The followingJavaimplementation of a classNodeis given:
private class Node { Node() {
this(null, null);
}
Node(Comparable d) { this(d, null);
}
Node(Comparable d, Node n) { data = d;
next = n;
}
Comparable data; Node next;
}
Assume that a singly linked list is implemented with a header node, but no tail node, and that it
maintains only a reference to the header node.
Using the classNodedescribed above, write aMySortedSingleListclass in Java includes methods
to:
boolean contains( Object x ) - test if a value x is contained in the linked list.
boolean add( Object x ) - add a value x if it is not already contained in the linked list.
boolean remove(Object x) - remove a value x if it is contained in the linked list.
maintaining the singly linked list in sorted order.
Solution
package mysortedsinglelist;
public class MySortedSingleList {
private Node head;
//test if a value x is contained in the linked list.
public boolean contains(Comparable x){
/*Node tmp = head;
while(tmp!=null)
{
if(tmp.data!=x){
tmp = tmp.next;
}
else{
return true;
}
}
return false; */
Node cursor = head;
while(cursor != null)
{
if(cursor.data.equals(x))
{
return true;
}
cursor = cursor.next;
}
return false;
}
public boolean add(Comparable x)
{
Node nd = new Node();
nd.data = x;
//System.out.println("Adding: "+x);
boolean flag = contains(x);
/* check if the list is empty*/
if(head == null){
//since there is only one element, head
head = nd;
return true;
}
if(flag==false)
{
head.next=nd;
return true;
}
else
{
return false;
}
}
public boolean remove(Comparable x)
{
boolean flag = contains(x);
if(flag)
{
if(head == null)
{
System.out.println("Underflow...");
return false;
}
Node tmp = head;
head = tmp.next;
if(head == null){
head.next = null;
}
System.out.println("Deleted: "+tmp.data);
return true;
}
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MySortedSingleList sl = new MySortedSingleList();
sl.add(3);
boolean c1= sl.contains(32);
System.out.print(c1);
sl.add(54);
sl.remove(32);
}
}
class Node
{
Node()
{
this(null, null);
}
Node(Comparable d)
{
this(d, null);
}
Node(Comparable d, Node n)
{
data = d;
next = n;
}
Comparable data; Node next;
}

More Related Content

Similar to Java problem. The followingJavaimplementation of a classNodeis given.pdf

This assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfThis assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfadislifestyle
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfambersushil
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Sagar Verma
 
This is a homework assignment that has to be done in JAVA.Objectiv.pdf
This is a homework assignment that has to be done in JAVA.Objectiv.pdfThis is a homework assignment that has to be done in JAVA.Objectiv.pdf
This is a homework assignment that has to be done in JAVA.Objectiv.pdffeelingcomputors
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdffantasiatheoutofthef
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Edureka!
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfsauravmanwanicp
 

Similar to Java problem. The followingJavaimplementation of a classNodeis given.pdf (12)

This assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfThis assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdf
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
 
This is a homework assignment that has to be done in JAVA.Objectiv.pdf
This is a homework assignment that has to be done in JAVA.Objectiv.pdfThis is a homework assignment that has to be done in JAVA.Objectiv.pdf
This is a homework assignment that has to be done in JAVA.Objectiv.pdf
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Java collections
Java collectionsJava collections
Java collections
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdf
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 

More from PRATIKSINHA7304

Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfPRATIKSINHA7304
 
Find the sum of the infinite geometric series if it exists. (If an .pdf
Find the sum of the infinite geometric series if it exists. (If an .pdfFind the sum of the infinite geometric series if it exists. (If an .pdf
Find the sum of the infinite geometric series if it exists. (If an .pdfPRATIKSINHA7304
 
A new class will be added to the startup package to start up the eve.pdf
A new class will be added to the startup package to start up the eve.pdfA new class will be added to the startup package to start up the eve.pdf
A new class will be added to the startup package to start up the eve.pdfPRATIKSINHA7304
 
Are these statements regarding Rhizobacteria true Plant Growth P.pdf
Are these statements regarding Rhizobacteria true Plant Growth P.pdfAre these statements regarding Rhizobacteria true Plant Growth P.pdf
Are these statements regarding Rhizobacteria true Plant Growth P.pdfPRATIKSINHA7304
 
1. Prices may be different between two countries whenA. Both count.pdf
1. Prices may be different between two countries whenA. Both count.pdf1. Prices may be different between two countries whenA. Both count.pdf
1. Prices may be different between two countries whenA. Both count.pdfPRATIKSINHA7304
 
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdfPRATIKSINHA7304
 
You are requested to write the SQL commands to perform the following.pdf
You are requested to write the SQL commands to perform the following.pdfYou are requested to write the SQL commands to perform the following.pdf
You are requested to write the SQL commands to perform the following.pdfPRATIKSINHA7304
 
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfwrite a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfPRATIKSINHA7304
 
Wiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdfWiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdfPRATIKSINHA7304
 
Which of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdfWhich of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdfPRATIKSINHA7304
 
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdfPRATIKSINHA7304
 
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdfWhat are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdfPRATIKSINHA7304
 
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdfWhat led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdfPRATIKSINHA7304
 
What is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdfWhat is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdfPRATIKSINHA7304
 
What factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdfWhat factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdfPRATIKSINHA7304
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfPRATIKSINHA7304
 
this is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdfthis is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdfPRATIKSINHA7304
 
This fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdfThis fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdfPRATIKSINHA7304
 
The term refers to a. Global Analysis of the proteins produced in .pdf
The term  refers to  a. Global Analysis of the proteins produced in .pdfThe term  refers to  a. Global Analysis of the proteins produced in .pdf
The term refers to a. Global Analysis of the proteins produced in .pdfPRATIKSINHA7304
 

More from PRATIKSINHA7304 (20)

Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 
Find the sum of the infinite geometric series if it exists. (If an .pdf
Find the sum of the infinite geometric series if it exists. (If an .pdfFind the sum of the infinite geometric series if it exists. (If an .pdf
Find the sum of the infinite geometric series if it exists. (If an .pdf
 
A new class will be added to the startup package to start up the eve.pdf
A new class will be added to the startup package to start up the eve.pdfA new class will be added to the startup package to start up the eve.pdf
A new class will be added to the startup package to start up the eve.pdf
 
Are these statements regarding Rhizobacteria true Plant Growth P.pdf
Are these statements regarding Rhizobacteria true Plant Growth P.pdfAre these statements regarding Rhizobacteria true Plant Growth P.pdf
Are these statements regarding Rhizobacteria true Plant Growth P.pdf
 
1. Prices may be different between two countries whenA. Both count.pdf
1. Prices may be different between two countries whenA. Both count.pdf1. Prices may be different between two countries whenA. Both count.pdf
1. Prices may be different between two countries whenA. Both count.pdf
 
[10 points]Add the.pdf
[10 points]Add the.pdf[10 points]Add the.pdf
[10 points]Add the.pdf
 
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf
§ 7.3 7.6 Which member of the following pairs is likely to decompose .pdf
 
You are requested to write the SQL commands to perform the following.pdf
You are requested to write the SQL commands to perform the following.pdfYou are requested to write the SQL commands to perform the following.pdf
You are requested to write the SQL commands to perform the following.pdf
 
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfwrite a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
 
Wiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdfWiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdf
 
Which of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdfWhich of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdf
 
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
 
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdfWhat are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
 
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdfWhat led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
 
What is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdfWhat is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdf
 
What factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdfWhat factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdf
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
 
this is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdfthis is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdf
 
This fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdfThis fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdf
 
The term refers to a. Global Analysis of the proteins produced in .pdf
The term  refers to  a. Global Analysis of the proteins produced in .pdfThe term  refers to  a. Global Analysis of the proteins produced in .pdf
The term refers to a. Global Analysis of the proteins produced in .pdf
 

Recently uploaded

An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 

Recently uploaded (20)

Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

Java problem. The followingJavaimplementation of a classNodeis given.pdf

  • 1. Java problem. The followingJavaimplementation of a classNodeis given: private class Node { Node() { this(null, null); } Node(Comparable d) { this(d, null); } Node(Comparable d, Node n) { data = d; next = n; } Comparable data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the classNodedescribed above, write aMySortedSingleListclass in Java includes methods to: boolean contains( Object x ) - test if a value x is contained in the linked list. boolean add( Object x ) - add a value x if it is not already contained in the linked list. boolean remove(Object x) - remove a value x if it is contained in the linked list. maintaining the singly linked list in sorted order. Solution package mysortedsinglelist; public class MySortedSingleList { private Node head; //test if a value x is contained in the linked list. public boolean contains(Comparable x){ /*Node tmp = head; while(tmp!=null) { if(tmp.data!=x){ tmp = tmp.next; } else{
  • 2. return true; } } return false; */ Node cursor = head; while(cursor != null) { if(cursor.data.equals(x)) { return true; } cursor = cursor.next; } return false; } public boolean add(Comparable x) { Node nd = new Node(); nd.data = x; //System.out.println("Adding: "+x); boolean flag = contains(x); /* check if the list is empty*/ if(head == null){ //since there is only one element, head head = nd; return true; } if(flag==false) { head.next=nd; return true; } else { return false;
  • 3. } } public boolean remove(Comparable x) { boolean flag = contains(x); if(flag) { if(head == null) { System.out.println("Underflow..."); return false; } Node tmp = head; head = tmp.next; if(head == null){ head.next = null; } System.out.println("Deleted: "+tmp.data); return true; } return true; } /** * @param args the command line arguments */ public static void main(String[] args) { MySortedSingleList sl = new MySortedSingleList(); sl.add(3); boolean c1= sl.contains(32); System.out.print(c1); sl.add(54); sl.remove(32); }
  • 4. } class Node { Node() { this(null, null); } Node(Comparable d) { this(d, null); } Node(Comparable d, Node n) { data = d; next = n; } Comparable data; Node next; }