Question
In this lab, we have to create methods for a SimpleLinkedList.java file. I have created about half of them but the remaining few are giving
In this lab, we have to create methods for a SimpleLinkedList.java file. I have created about half of them but the remaining few are giving me such a difficult time. I have started over so many times, I have lost count. My professor add this note that makes it more difficult than what it needs to be. Please help in any way possible. I have about had it with this lab
Note: do NOT add or remove data fields to SimpleLinkedList 2 class. Do not change the definition of Node class.
Add the following methods::
1. public int get((int index))
Get the integer at the specified position:: index and return the integer value. If the index is not valid, print an error message.
2. public boolean add((int index, int item))
Try to insert the specified integer:: item at the specified position:: index in the current linked list. If there is an integer currently at that position, do not replace it and this is the case of failure to insert. If the specified position is greater than the current size of the linked list, display an error message and do not insert the item. Returns true if the insertion is successful, false otherwise.
3. public void copy((SSimpleLinkedList 2 list 2 )
Make the current linked list an n EXACT COPY of the specified list list2. Discard all existing integer s in the current linked list.
Note: after the copy method, the current linked list must be an EXACT COPY of the specified list, NOT the SAME single list. So the implementation is not just a simple assignment operation.
4. public boolean equals(( Object list 2 )
Check if the list of integers stored in the current linked list equals to the list of integers stored in the specified list 2. Returns true if yes, false otherwise. Note:: this overrides whats in the class Object.
5. (BBonus)) public boolean removeByValue((int item))
Remove the specified integer item from the current linked list. If success, return true. Otherwise, return false. Assume that there are no duplicate values in the list.
6. (BBonus)) public boolean removeyByIndex((int index))
Remove the integer at the specified position index from the current linked list. Return true if success, false otherwise. If the index is not valid, print an error message.
public class SimpleLinkedList2 { /** the head pointer to point to the first node in the list */ private Node head;
/**constructor to create an empty list */ public SimpleLinkedList2() { head = null; }
/**insert the new integer value stored in newItem to the end of the list */ public void add(int newItem) { //create a new node for the integer value in newItem Node newNode = new Node(newItem); //the link to current node in the list Node current;
//search for the last node in the list if (head == null) { //if the list is empty head = newNode; } else { //search for the last node in the list current = head; while (current.next != null) { current = current.next; } //make the new node as the last in the list current.next = newNode; } }
/**Insert the new integer value stored in variable newItem to the list. * The new value will be the immediate following neighbor after the node referenced by here. */ private void add(int newItem, Node here) { Node temp = new Node(newItem); temp.next = here.next; here.next = temp;
}
@Override public String toString() { String result = ""; Node current = head;
while (current != null) { result += current.dataItem + "==>"; current = current.next; } return result; }
private static class Node { /**The data item (an integers) stored in the node */ private int dataItem; /**the link to the immediate following neighbor */ private Node next;
/**Create a node for the item (an integer) */ public Node(int item) { dataItem = item; next = null; } }
//INSERT METHODS DOWN HERE. }
---TEST/MAIN METHOD---
public class SimpleLinkedList2Test {
public static void main(String[] args) { //create an empty list SimpleLinkedList2 myList = new SimpleLinkedList2(); //add three integers 10, 20, 30 to the list myList.add(10); myList.add(20); myList.add(30);
//print the integer values in the list System.out.println(myList.toString()); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started