LinkedList FILE: https://www.dropbox.com/s/htub433hqk4bupl/LinkedList.java.webar
ID: 3687143 • Letter: L
Question
LinkedList FILE:
https://www.dropbox.com/s/htub433hqk4bupl/LinkedList.java.webarchive?dl=0
To solve this question, you can use the doubly LinkedList Class from your book, or any other implementation. Add the following changes to the doubly LinkedList Class: 1. Override the toString() method. 2. Implement getSize() method, which returns the current size of the linked list (remember that you need to make core changes to the code) 3. Implement another toString method with the name reverseToString(), this method should return a string of the data within the linked list but in reverse order. 4. Implement a sort() method to sort LinkedList. You can use any sort algorithm or any trick to sort LinkedList (think outside the box). 5. The attached LinkedListTester class must work with your implementation. Here a screen shot for some output... myLis size = 10. myList before sorting: [41, 39, 42, 31, 90, 63, 64, 99, 33, 66] After sorting(ascending): [31, 33, 39, 41, 42, 63, 64, 66, 90, 99] After sorting(descending): [99, 90, 66, 64, 63, 42, 41, 39, 33, 31]Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class DoublyLinkList<T> {
private static class Node<T> {
private T data;
private Node next;
private Node prev;
public Node(T data) {
this.data = data;
}
public void displayNode() {
System.out.print(data + " ");
}
@Override
public String toString() {
return data.toString();
}
}
class linked {
public linkedlist fstnode, lastnode;
linked() {
fstnode = null;
lastnode = null;
}
/* Insert node at the beginning or create linked list */
void insert_front(int value) {
linkedlist node = new linkedlist(value);
if(fstnode == null) {
node.prev = node.next = null;
fstnode = lastnode = node;
System.out.println("Lined list created successfully!");
}
else {
node.prev = null;
node.next = fstnode;
fstnode.prev = node;
fstnode = node;
System.out.println("Node Inserted at the front of the Linked list!");
}
}
/* Insert node at the end or create linked list */
void insert_end(int value) {
linkedlist node = new linkedlist(value);
if(fstnode == null) {
node.prev = node.next = null;
fstnode = lastnode = node;
System.out.println("Lined list created successfully!");
}
else {
node.next = null;
node.prev = lastnode;
lastnode.next = node;
lastnode = node;
System.out.println("Node Inserted at the end of the Linked list!");
}
void display() {
linkedlist node = fstnode;
System.out.println("List of node in Ascending order!");
while(node != null) {
node.display();
node = node.next;
}
node = lastnode;
System.out.println("List of node in Descending order!");
while(node != null) {
node.display();
node = node.prev;
}
}
}
class doublylinkedlist {
public static void main(String args[ ]) {
linked list = new linked();
Scanner input = new Scanner(System.in);
int op = 0;
while(op != 4) {
System.out.println("1. Insert at front 2. Insert at back 3. Display 4. Exit");
System.out.println("Enter your choice:");
op = Integer.parseInt(input.nextLine());
switch(op) {
case 1:
System.out.println("Enter the positive value for Linked list:");
list.insert_front(Integer.parseInt(input.nextLine()));
break;
case 2:
System.out.println("Enter the positive value for Linked list:");
list.insert_end(Integer.parseInt(input.nextLine()));
break;
case 3:
list.display();
break;
case 4:
System.out.println("Bye Bye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.