Question: Please see the problem and my attempt at the code below and correct it/tell me what I am doing wrong. I am getting too many

Please see the problem and my attempt at the code below and correct it/tell me what I am doing wrong. I am getting too many error messages in my IDE and I doin't know where to begin with fixing it. Thanks!

Problem 1. [70 points] Implement a linked list of integers as a class LinkedList. Build the following methods:

  • print that prints the content of the linked list;

  • addFirst that adds a new node to the beginning (the head) of the

    linked list;

  • addLast that adds a new node to the end (the tail) of the linked list;

  • indexOf that finds a specific node by its value, and returns nodes index (nodes position from the left in the linked list); if the value is not present in the linked list, it returns 1;

  • deleteFirst that deletes the first node in the linked list;

  • deleteLast that deletes the last node in the linked list.

    Test your class creating a list in the main and

  1. adding one by one nodes 2, 4, 8 to the tail;

  2. adding nodes -2, -8 to the head;

  3. adding a node 9 to the tail;

  4. printing the list;

  5. printing indexOf(4);

  6. printing contains(9);

  7. deleting one by one all the nodes in the list either from the tail or from the head and printing the result after each deletion.

My code:

public class LinkedListFromScratch { public static void main(String[] args) { LinkedList myList = new LinkedList(); myList.addLast(2); myList.addLast(4); myList.addLast(8); myList.addFirst(-2); myList.addFirst(-4); myList.addLast(9); myList.print(); System.out.println(myList.indexOf(4)); System.out.println(myList.contains(9)); for (int i = 0; i < 6; ++i) { myList.deleteLast(); myList.print(); } public class LinkedList { private Node head; private Node tail; private class Node { private int value; private Node next; public Node(int value) { this.value = value; } } private boolean isEmpty() { return (head == null); } private boolean hasNext(Node node) { return (node.next != null); } public void print() { Node current = head; System.out.print("["); while (current != null) { if (hasNext(current)) { System.out.print(current.value + "' "); } else { System.out.print(current.value); } current = current.next; ; } public int addFirst(int value) { Node node = new Node(value); if (isEmpty()) { head = tail = node; } else { node.next = head; head = node; } } public int addLast(int value) { Node node = new Node(value); if (isEmpty()) { head = tail = node; } else { tail.next = node; tail = node; } } public int indexOf(int value) { int i = 0; Node current = head; while (current != null) { if (current.value == value) { return i; } i++; current = current.next; } return -1; } public boolean contains(int value) { return (indexOf(value) != -1); } public void deleteFirst() { if (isEmpty()) { throw new NoSuchElementException(); } if (head == tail) { head = tail = null; return; } Node formerHeadNext = head.next; head.next = null; head = formerHeadNext; } public Node previous(Node node) { if (isEmpty()) throw new NoSuchElementException(); Node current = head; while (current.next != node) { if (!hasNext(current)) { throw new NoSuchElementException(); } current = current.next; } return current; } public void deleteLast() { if (isEmpty()) throw new NoSuchElementException(); if (head == tail) { head = tail = null; return; } Node lastButOne = previous(tail); lastButOne.next = null; tail = lastButOne; } } } } } 

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!