Answered step by step
Verified Expert Solution
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
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(); } }
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
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