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

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

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