Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Exercise public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); list.add(A); list.add(B); list.add(C); list.add(D); list.printLinkedList(); System.out.println(List size: + list.size()); System.out.println(------------); list.add(4,

Java Exercise

image text in transcribed

public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.add(4, "Z"); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.add(0, "1"); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.add(3, "?"); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.remove(7); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.remove(0); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.remove(5); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); list.remove(2); list.printLinkedList(); System.out.println("List size: " + list.size()); System.out.println("------------"); }

LinkedList Class:

// We keep the list non-generic for simplicity. In fact, we create a Linkedlist of Strings class MyLinkedList { // This is the node inner class private class Node { String str; // Note this is a double linked list (as is the Java LinkedList implementation) // Nodes are aware of both the node before AND after them Node next; Node previous; Node(String str){ this.str = str; } } //----------------------------------------------- private Node head, tail; private int size; // Basic constructor public MyLinkedList() { head = tail = null; size = 0; } // Code to add a node to the LinkedList (and thus add a data element, i.e., here a String) public void add(String str) { if(head == null) { head = new Node(str); tail = head; } else { Node temp = tail; tail.next = new Node(str); tail = tail.next; tail.previous = temp; } size++; } // Traverse the List public void printLinkedList() { Node temp = head; // count is just used for output beautification int count = 0; // While the node is not null (we have not reached past the last node) while(temp != null) { // Should I print a comm or not? if (count != 0) { System.out.print(", "); } System.out.print(temp.str); // Move to the next node temp = temp.next; count++; } System.out.println(); } } 

image text in transcribed

You have seen how to construct a very simple LinkedList (of Strings) class. Beside a default constructor and an ad-hoc method to display the content of our list, this class only allowed the addition of elements at the end of the list. In this assignment, you are asked to build four more methods for this class: 1. addFirst(String str) 2. add(int index, String str) 3. remove(int index) 4. size) The method signatures should be self-explanatory but, if you need more details, these methods should do the same thing as their namesakes in the LinkedList class (see the Java API) Please use the main() method below to check your results. Output: A, B, C, D List size: 4 A, B, C, D, Z List size: 5 1, A, B, C, D, Z List size: 6 1, A, B, ?, C, D, Z List size: 7 Invalid deletion point 1, A, B, ?, C, D, Z List size: 7 A, B, ?, C, D, Z List size: 6 A, B, ?, C, D List size: 5 A, B, C, D List size: 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

KEY QUESTION Refer to Figure 3.6, page

Answered: 1 week ago