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