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 (20)

PDF
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
PDF
please i need help Im writing a program to test the merge sort alg.pdf
ezonesolutions
 
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
seoagam1
 
PDF
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
PDF
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
PDF
Fix my codeCode.pdf
Conint29
 
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Komlin1
 
PDF
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
babitasingh698417
 
PDF
Linked List Operations
Gail Carmichael
 
PDF
Use the singly linked list class introduced in the lab to implement .pdf
sales87
 
PDF
There are a couple of new methods that you will be writing for this pr.pdf
aamousnowov
 
PDF
Describe an algorithm for concatenating two singly linked lists L and.pdf
deepak596396
 
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
alphawheels007
 
PDF
practice of using and manipulating a doubly linked list and (2) prac.pdf
rd1742
 
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
rishabjain5053
 
PDF
Solve using java and using this Singly linked list classpublic cl.pdf
aloeplusint
 
PPTX
القوائم المترابطة Linked List باستخدام لغة جافا
Mahmoud Alfarra
 
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
please i need help Im writing a program to test the merge sort alg.pdf
ezonesolutions
 
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
Please help me to make a programming project I have to sue them today- (1).pdf
seoagam1
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
Fix my codeCode.pdf
Conint29
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Komlin1
 
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
babitasingh698417
 
Linked List Operations
Gail Carmichael
 
Use the singly linked list class introduced in the lab to implement .pdf
sales87
 
There are a couple of new methods that you will be writing for this pr.pdf
aamousnowov
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
deepak596396
 
-JAVA-provide a test class that do the required -you may add met.pdf
alphawheels007
 
practice of using and manipulating a doubly linked list and (2) prac.pdf
rd1742
 
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
rishabjain5053
 
Solve using java and using this Singly linked list classpublic cl.pdf
aloeplusint
 
القوائم المترابطة Linked List باستخدام لغة جافا
Mahmoud Alfarra
 

More from fantasiatheoutofthef (20)

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

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Horarios de distribución de agua en julio
pegazohn1978
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Ad

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; }