SlideShare a Scribd company logo
In this assignment you will implement insert() method for a singly linked Link list. The code
MyLinkedlist.java is given , it uses generic type as the linked list can be created with any object
type. Some methods like add() and toString() are already implemented. The MyLinkedList class
has a private nested class called Node.
The objective of this assignment is to implement the insert() method that takes two inputs an
element of generic type to be inserted and an index (value of integer type) at which it should be
inserted. For this assignment we will assume the first element has the index 0. if the given index
is less than zero do nothing but if it is greater than the size of the list, insert the element at the
end of the list. if the index is between 0 and size , insert it in the appropriate location. You insert
code should correctly update the theSize data member of the linked list. Return type for insert()
method is boolean (true or false) depending on whether the insertion was successful or not. Use
the following method header.
}
Main.java
//add necessary libraries
public class Main
{
//add your function here
public static void main (String [] args)
{
}
}
MyLinkedList.java
public class MyLinkedList {
// A private nested Node class
private static class Node{
public AnyType data; // Data members are public, this is fine as the Node class itself is
private
public Node next;
public Node(AnyType d, Node n)
{
data = d;
next=n;
}
@Override
public String toString() {
return data.toString();
}
}
// Data Members
private Node head; // A node pointer to the first item in the List
private int theSize; // a integer variable to hold the size of the List
public MyLinkedList() {
doClear();
}
public void clear() {
doClear();
}
private void doClear() {
head = null;
theSize =0 ;
}
// An add method that will always add at the beginning of the List
public boolean add(AnyType x) {
if(head == null) // List is empty
{
head = new Node(x, null);
theSize++;
return true;
}
else // List has at least one item lets get to the end of it
{
Node newnode = new Node(x, head);
head = newnode;
theSize++;
return true;
}
}
@Override
public String toString() {
// This to String method will print the List from head to the end.
String theList = "";
if(head==null)
return theList;
Node cur = head;
while(cur.next!=null) {
theList = theList + cur.toString() + " ";
cur= cur.next;
}
return theList + cur.toString();
}
public int getTheSize() {
return theSize;
}
}
/*****************************************************************************
*
*
* SOLUTION CODE BELOW THIS HEADER
*
*****************************************************************************/
/*****************************************************************************
*
*
* SOLUTION CODE ABOVE THIS HEADER
*
*****************************************************************************/
}
Current file: Main.java Load default template... //add necessary Libraries public class Main {
//add your function here public static void main (string [] args) { } }
// An add method that will always add at the beginning of the List public boolean add(AnyType
x ) { if (head == null) // List is empty { head = new Node AnyType >(x, null ); thesize++;
return true; } else // List has at least one item lets get to the end of it { Node AnyType >
newnode = new Node AnyType >(x, head ); head = newnode; thesize++; return true; } }
@override public string tostring() { // This to string method will print the List from head to the
end. string thelist = " "; if (head==null) return theList; Node AnyType > cur = head; while(cur.
next!=null) { theList = theList + cur.tostring ()+" "; cur = cur. next; } return theList +
cur.tostring(); } public int getThesize() { return thesize; ?
In this assignment you will implement insert() method for a singly linked Link list. The code
MyLinkedlist.java is given, it uses generic type as the linked list can be created with any object
type. Some methods like add() and toString() are already implemented. The MyLinkedList class
has a private nested class called Node. The objective of this assignment is to implement the
insert() method that takes two inputs an element of generic type to be inserted and an index
(value of integer type) at which it should be inserted. For this assignment we will assume the first
element has the index 0 . if the given index is less than zero do nothing but if it is greater than the
size of the list, insert the element at the end of the list. if the index is between 0 and size, insert it
in the appropriate location. You insert code should correctly update the theSize data member of
the linked list. Return type for insert() method is boolean (true or false) depending on whether
the insertion was successful or not. Use the following method header.
publicbooleaninsert(AnyTypex,intindex){
// This to string meth will print the List from head to the end. string thelist =" "; if (head==null)
return thelist; Node AnyType > cur = head; while (cur. next!=null) { thelist = thelist +
cur.tostring ()+" "; cur= cur. next; } return thelist + cur.tostring(); } public int getThesize() {
return thesize; } } / SOLUTION CODE BELOW THIS HEADER / SOLUTION CODE
ABOVE THIS HEADER
public class MyLinkedList { // A private nested Node class private static class Node>{ public
AnyType data; // Data members are public, this is fine as the Node class itself is private public
Node next; public Node(AnyType d, Noden ) { data =d; next =n; } @override public string
tostring() { return data.tostring (); } } // Data Members private Node head; // A node pointer to
the first item in the List private int thesize; // a integer variable to hold the size of the List public
MyLinkedList() { doclear (); } public void clear() { doclear(); } private void doclear() { head
= null; thesize =0; }

More Related Content

Similar to In this assignment you will implement insert() method for a singly l.pdf

This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
arshin9
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
Waf1231
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
ssuser0be977
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
alanfhall8953
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
fashiongallery1
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
EvanpZjSandersony
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
info335653
 

Similar to In this assignment you will implement insert() method for a singly l.pdf (20)

This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
List
ListList
List
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked list
Linked list Linked list
Linked list
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 

More from fantasiatheoutofthef

Introduction Pervasive computing demands the all-encompassing exploi.pdf
Introduction Pervasive computing demands the all-encompassing exploi.pdfIntroduction Pervasive computing demands the all-encompassing exploi.pdf
Introduction Pervasive computing demands the all-encompassing exploi.pdf
fantasiatheoutofthef
 
International projects are on the rise, so the need for project mana.pdf
International projects are on the rise, so the need for project mana.pdfInternational projects are on the rise, so the need for project mana.pdf
International projects are on the rise, so the need for project mana.pdf
fantasiatheoutofthef
 
Instructions On July 1, a petty cash fund was established for $100. .pdf
Instructions On July 1, a petty cash fund was established for $100. .pdfInstructions On July 1, a petty cash fund was established for $100. .pdf
Instructions On July 1, a petty cash fund was established for $100. .pdf
fantasiatheoutofthef
 
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdfJust C, D, E 7-1 Hoare partition correctness The version of PART.pdf
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
fantasiatheoutofthef
 
Integral ICreate a Python function for I-Importancedef monteca.pdf
Integral ICreate a Python function for I-Importancedef monteca.pdfIntegral ICreate a Python function for I-Importancedef monteca.pdf
Integral ICreate a Python function for I-Importancedef monteca.pdf
fantasiatheoutofthef
 
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
IntroductionThe capstone project is a �structured walkthrough� pen.pdfIntroductionThe capstone project is a �structured walkthrough� pen.pdf
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
fantasiatheoutofthef
 
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdfIndustry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
fantasiatheoutofthef
 
Jefferson City has several capital projects that the mayor wants to .pdf
Jefferson City has several capital projects that the mayor wants to .pdfJefferson City has several capital projects that the mayor wants to .pdf
Jefferson City has several capital projects that the mayor wants to .pdf
fantasiatheoutofthef
 
Instructions Create an Android app according to the requirements below.pdf
Instructions Create an Android app according to the requirements below.pdfInstructions Create an Android app according to the requirements below.pdf
Instructions Create an Android app according to the requirements below.pdf
fantasiatheoutofthef
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
 
In this case study, we will explore the exercise of power in leaders.pdf
In this case study, we will explore the exercise of power in leaders.pdfIn this case study, we will explore the exercise of power in leaders.pdf
In this case study, we will explore the exercise of power in leaders.pdf
fantasiatheoutofthef
 
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdfJames Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
fantasiatheoutofthef
 
Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
fantasiatheoutofthef
 
Introduction Pervasive computing demands the all-encompassing exploita.pdf
Introduction Pervasive computing demands the all-encompassing exploita.pdfIntroduction Pervasive computing demands the all-encompassing exploita.pdf
Introduction Pervasive computing demands the all-encompassing exploita.pdf
fantasiatheoutofthef
 
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdfIntroduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
fantasiatheoutofthef
 
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdfIntegral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
fantasiatheoutofthef
 
Indicate and provide a brief explanation for whether the following i.pdf
Indicate and provide a brief explanation for whether the following i.pdfIndicate and provide a brief explanation for whether the following i.pdf
Indicate and provide a brief explanation for whether the following i.pdf
fantasiatheoutofthef
 
Lindsay was leaving a local department store when an armed security .pdf
Lindsay was leaving a local department store when an armed security .pdfLindsay was leaving a local department store when an armed security .pdf
Lindsay was leaving a local department store when an armed security .pdf
fantasiatheoutofthef
 
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
Learning Team � Ethical Challenges Worksheet Your team of internat.pdfLearning Team � Ethical Challenges Worksheet Your team of internat.pdf
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
fantasiatheoutofthef
 
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdfLecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
fantasiatheoutofthef
 

More from fantasiatheoutofthef (20)

Introduction Pervasive computing demands the all-encompassing exploi.pdf
Introduction Pervasive computing demands the all-encompassing exploi.pdfIntroduction Pervasive computing demands the all-encompassing exploi.pdf
Introduction Pervasive computing demands the all-encompassing exploi.pdf
 
International projects are on the rise, so the need for project mana.pdf
International projects are on the rise, so the need for project mana.pdfInternational projects are on the rise, so the need for project mana.pdf
International projects are on the rise, so the need for project mana.pdf
 
Instructions On July 1, a petty cash fund was established for $100. .pdf
Instructions On July 1, a petty cash fund was established for $100. .pdfInstructions On July 1, a petty cash fund was established for $100. .pdf
Instructions On July 1, a petty cash fund was established for $100. .pdf
 
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdfJust C, D, E 7-1 Hoare partition correctness The version of PART.pdf
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
 
Integral ICreate a Python function for I-Importancedef monteca.pdf
Integral ICreate a Python function for I-Importancedef monteca.pdfIntegral ICreate a Python function for I-Importancedef monteca.pdf
Integral ICreate a Python function for I-Importancedef monteca.pdf
 
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
IntroductionThe capstone project is a �structured walkthrough� pen.pdfIntroductionThe capstone project is a �structured walkthrough� pen.pdf
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
 
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdfIndustry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
 
Jefferson City has several capital projects that the mayor wants to .pdf
Jefferson City has several capital projects that the mayor wants to .pdfJefferson City has several capital projects that the mayor wants to .pdf
Jefferson City has several capital projects that the mayor wants to .pdf
 
Instructions Create an Android app according to the requirements below.pdf
Instructions Create an Android app according to the requirements below.pdfInstructions Create an Android app according to the requirements below.pdf
Instructions Create an Android app according to the requirements below.pdf
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
In this case study, we will explore the exercise of power in leaders.pdf
In this case study, we will explore the exercise of power in leaders.pdfIn this case study, we will explore the exercise of power in leaders.pdf
In this case study, we will explore the exercise of power in leaders.pdf
 
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdfJames Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
 
Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
 
Introduction Pervasive computing demands the all-encompassing exploita.pdf
Introduction Pervasive computing demands the all-encompassing exploita.pdfIntroduction Pervasive computing demands the all-encompassing exploita.pdf
Introduction Pervasive computing demands the all-encompassing exploita.pdf
 
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdfIntroduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
 
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdfIntegral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
 
Indicate and provide a brief explanation for whether the following i.pdf
Indicate and provide a brief explanation for whether the following i.pdfIndicate and provide a brief explanation for whether the following i.pdf
Indicate and provide a brief explanation for whether the following i.pdf
 
Lindsay was leaving a local department store when an armed security .pdf
Lindsay was leaving a local department store when an armed security .pdfLindsay was leaving a local department store when an armed security .pdf
Lindsay was leaving a local department store when an armed security .pdf
 
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
Learning Team � Ethical Challenges Worksheet Your team of internat.pdfLearning Team � Ethical Challenges Worksheet Your team of internat.pdf
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
 
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdfLecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
Lecture Activity 7.1 Evaluating Evidence on Wikipedia Scenario You.pdf
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 

In this assignment you will implement insert() method for a singly l.pdf

  • 1. In this assignment you will implement insert() method for a singly linked Link list. The code MyLinkedlist.java is given , it uses generic type as the linked list can be created with any object type. Some methods like add() and toString() are already implemented. The MyLinkedList class has a private nested class called Node. The objective of this assignment is to implement the insert() method that takes two inputs an element of generic type to be inserted and an index (value of integer type) at which it should be inserted. For this assignment we will assume the first element has the index 0. if the given index is less than zero do nothing but if it is greater than the size of the list, insert the element at the end of the list. if the index is between 0 and size , insert it in the appropriate location. You insert code should correctly update the theSize data member of the linked list. Return type for insert() method is boolean (true or false) depending on whether the insertion was successful or not. Use the following method header. } Main.java //add necessary libraries public class Main { //add your function here public static void main (String [] args) { } } MyLinkedList.java public class MyLinkedList { // A private nested Node class private static class Node{ public AnyType data; // Data members are public, this is fine as the Node class itself is
  • 2. private public Node next; public Node(AnyType d, Node n) { data = d; next=n; } @Override public String toString() { return data.toString(); } } // Data Members private Node head; // A node pointer to the first item in the List private int theSize; // a integer variable to hold the size of the List public MyLinkedList() { doClear(); } public void clear() { doClear(); } private void doClear() { head = null; theSize =0 ; } // An add method that will always add at the beginning of the List public boolean add(AnyType x) { if(head == null) // List is empty { head = new Node(x, null);
  • 3. theSize++; return true; } else // List has at least one item lets get to the end of it { Node newnode = new Node(x, head); head = newnode; theSize++; return true; } } @Override public String toString() { // This to String method will print the List from head to the end. String theList = ""; if(head==null) return theList; Node cur = head; while(cur.next!=null) { theList = theList + cur.toString() + " "; cur= cur.next; } return theList + cur.toString(); } public int getTheSize() { return theSize; } } /***************************************************************************** * *
  • 4. * SOLUTION CODE BELOW THIS HEADER * *****************************************************************************/ /***************************************************************************** * * * SOLUTION CODE ABOVE THIS HEADER * *****************************************************************************/ } Current file: Main.java Load default template... //add necessary Libraries public class Main { //add your function here public static void main (string [] args) { } } // An add method that will always add at the beginning of the List public boolean add(AnyType x ) { if (head == null) // List is empty { head = new Node AnyType >(x, null ); thesize++; return true; } else // List has at least one item lets get to the end of it { Node AnyType > newnode = new Node AnyType >(x, head ); head = newnode; thesize++; return true; } } @override public string tostring() { // This to string method will print the List from head to the end. string thelist = " "; if (head==null) return theList; Node AnyType > cur = head; while(cur. next!=null) { theList = theList + cur.tostring ()+" "; cur = cur. next; } return theList + cur.tostring(); } public int getThesize() { return thesize; ? In this assignment you will implement insert() method for a singly linked Link list. The code MyLinkedlist.java is given, it uses generic type as the linked list can be created with any object
  • 5. type. Some methods like add() and toString() are already implemented. The MyLinkedList class has a private nested class called Node. The objective of this assignment is to implement the insert() method that takes two inputs an element of generic type to be inserted and an index (value of integer type) at which it should be inserted. For this assignment we will assume the first element has the index 0 . if the given index is less than zero do nothing but if it is greater than the size of the list, insert the element at the end of the list. if the index is between 0 and size, insert it in the appropriate location. You insert code should correctly update the theSize data member of the linked list. Return type for insert() method is boolean (true or false) depending on whether the insertion was successful or not. Use the following method header. publicbooleaninsert(AnyTypex,intindex){ // This to string meth will print the List from head to the end. string thelist =" "; if (head==null) return thelist; Node AnyType > cur = head; while (cur. next!=null) { thelist = thelist + cur.tostring ()+" "; cur= cur. next; } return thelist + cur.tostring(); } public int getThesize() { return thesize; } } / SOLUTION CODE BELOW THIS HEADER / SOLUTION CODE ABOVE THIS HEADER public class MyLinkedList { // A private nested Node class private static class Node>{ public AnyType data; // Data members are public, this is fine as the Node class itself is private public Node next; public Node(AnyType d, Noden ) { data =d; next =n; } @override public string tostring() { return data.tostring (); } } // Data Members private Node head; // A node pointer to the first item in the List private int thesize; // a integer variable to hold the size of the List public MyLinkedList() { doclear (); } public void clear() { doclear(); } private void doclear() { head = null; thesize =0; }