Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a linkedlist with there interfaces: void add(E element) Appends the speci

ID: 3636591 • Letter: C

Question

Create a linkedlist with there interfaces:

void add(E element)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
void clear()
Removes all of the elements from this list.
E get(int index)
Returns the element at the specified position in this list.
int indexOf(E element)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
E remove(int index)
Removes the element at the specified position in this list.
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
int size()
Returns the number of elements in this list.
E[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element), or null if this list is empty.
java.lang.String toString()
Returns a string representation of this list, containing the String representation of each element, or an empty string if this list is empty

Explanation / Answer

public class LinkedList { // reference to the head node. private Node head; private int listCount; // LinkedList constructor public LinkedList() { // this is an empty list, so the reference to the head node // is set to a new node with no data head = new Node(null); listCount = 0; } public void add(Object data) // post: appends the specified element to the end of this list. { Node temp = new Node(data); Node current = head; // starting at the head node, crawl to the end of the list while(current.getNext() != null) { current = current.getNext(); } // the last node's "next" reference set to our new node current.setNext(temp); listCount++;// increment the number of elements variable } public void add(Object data, int index) // post: inserts the specified element at the specified position in this list. { Node temp = new Node(data); Node current = head; // crawl to the requested index or the last element in the list, // whichever comes first for(int i = 1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote