Question
Java Data Structures Linked List help. Guaranteed thumbs up! Classes: import java.util.NoSuchElementException; public class LinkedList { private Node first; public LinkedList() { first = null;
Java Data Structures Linked List help. Guaranteed thumbs up!
Classes:
import java.util.NoSuchElementException; public class LinkedList { private Node first; public LinkedList() { first = null; } public Object getFirst() { if (first == null) { throw new NoSuchElementException(); } return first.element; } public void addFirst(Object element) { // put your code here } public Object removeFirst() { // put your code here } public String toString() { String temp = ""; Node current = first; while (current != null) { temp = temp + current.element.toString() + ' '; current = current.next; } return temp; } class Node { public Object element; public Node next; } }
public class LinkedListTester { public static void main(String[] args) { LinkedList myList = new LinkedList(); myList.addFirst("aaa"); myList.addFirst("bbb"); myList.addFirst("ccc"); myList.addFirst("ddd"); System.out.println(myList); } }
Lab 2 - Linked Lists 2.1) Start this lab with the code listed below. The LinkedList class defines the rudiments of the code needed to build a linked list of Node objects. You will first complete the code for its addFirst method. This method is passed an object that is to be added to the beginning of the list. Write code that links the passed object to the list by completing the following tasks in order: 1. Create a new Node object. 2. Make the element variable in the new Node object reference the object that was passed to addFirst. 3. Make the next variable in the new Node object reference the object that is currently referenced in variable first, 4. Make variable first reference the new Node. 5. DO NOT modify the Node class. Test your code by running the main method in the LinkedListTester class below Why are the string objects in the reverse order from the way they were added? import java.util. NoSuchelement Exception; public class LinkedList { private Node first; public LinkedList() { first - null; } public Object getFirst() { if (first = null) { throw new NoSuchElement Exception(); } return first.element; public void addFirst(Object element) { 11 put your code here public Object removeFirst() { // put your code here public String toString() { String temp - ""; Node current - first; while (current !- null) temp - tomp + current.element.toString() + ' '; current -current.next; return tamp; class Node { public Object element; public Node next; public class LinkedListTester { public static void main(String[] args) { Linkedlist myList - new LinkedList(); myList.addFirst("aaa"); myList.addPirst("bbb"); myList.addFirst("ccc"); myList.addFirst("ddd"); System.out.println(myList); 2.2) Try rewriting addFirst in Lab 2.1, by following the same four steps in a slightly different order 1. Create a new Node object. 2. Make the next variable in the new Node object reference the object that is currently referenced in variable first. 3. Make variable first reference the new Node. 4. Make the element variable in the new Node object reference the object that was passed to addFirst, What is wrong with this modified version of the addFirst method? 2.3) Start this lab by modifying the LinkedListTester class's main method as listed below. The Node class employs public instance variables. Change their visibility to private and add the appropriate accessor and mutator methods. Make corresponding changes to the LinkedList class. Next, complete the code for the removeFirst method, which should remove and return the first element in the linked list. Throw a NoSuchelementException if the method is invoked on an empty list. Use the LinkedList Tester class's main method to test your code. public class LinkedListTester { public static void main(String[] args) { Linkedlist myList = new LinkedList(); myList.addFirst("aaa"); myList.addFirst( "bbb"); myList.addFirst "ccc")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