Question: The goal for this part is to implement basic linked-list operations using Java. You are provided with 2 java files for this assignment: 1st file

The goal for this part is to implement basic linked-list operations using Java.

You are provided with 2 java files for this assignment:

1st file

/* This class define a linked list that stores integer values. */

public class LinkedList { private Node head, tail; private int size; //constructor to create a list object with head, tail, and size. public LinkedList() { head = null; tail = null; size = 0; } //method add node to end of list public void addLastNode(int data) { if (tail == null) head = tail = new Node(data);//empty list else { tail.next = new Node(data); //link new node as last node tail = tail.next; //make tail pointer points to last node } size++; //increase list size by one } //================= your part to complete ============== //method add first node public void addFirstNode(int data) { //complete this method } //method add node at index public void addAtIndex(int index, int data) { //complete this method } //method remove first node public void removeFirstNode() { //complete this method } //method remove last node public void removeLastNode() { //complete this method } //method remove node at index public void removeAtIndex(int index) { //complete this method }

//================= end of your part ==============

//method to print out the list public void printList() { Node temp; temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } //class to create nodes as objects private class Node { private int data; //data field private Node next; //link field public Node(int item) //constructor method { data = item; next = null; } } }

2nd file:

import java.util.Scanner;

public class myTest { public static void main (String[] args) { LinkedList myList = new LinkedList(); //create a list object for (int i=1; i <= 5; i++) //add 5 nodes to the list { myList.addLastNode(i); } Scanner scan = new Scanner (System.in); String s=""; System.out.print("Enter a string: "); s = scan.nextLine();

System.out.println(); System.out.println("Input String:\t\t This is a test string"); System.out.print("Reversed String:\t string test is a This");

//print out the list content // System.out.print("My test list values are: "); // myList.printList(); //Write code to tast each completed method in class LinkedList } }

Save the two files in one folder, compile both files and run file myTest. The code has no errors and should give you the following output:

----jGRASP exec: java -ea myTest My test list values are: 1 2 3 4 5 ----jGRASP: operation complete.

Study the provided code and understand how it works before you make any changes to it.

Class LinkedList has 5 incomplete methods, designated by the comment Your part to complete. Use and modify the code we studies in slides to complete the implementation of these 5 methods. Notice that the code in the slides will not work as is since it is designed to work with generic type. Complete one method at a time and test it in class myTest where indicated. Notice that you must test each method in class myTest and display its output with proper label. For example, the output of testing method addFirst() with value 25 would be like this:

Testing method addFirstNode()

Adding value 25 as first node.

List content before adding 25 is: 1 2 3 4 5

List content after adding 25 is: 25 1 2 3 4 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!