Code Restructuring: Methods for Linked List operations are given. Restructure the codes to improve performance of codes. Linked List is a part of the Collection framework present in java.util package, however, to be able to check the complexity of Linked List operations, we can recode the data structure based on Java Documentation https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html Instruction 1: Two (2) Java classes are provided, first, the Linked List class and second, the Linked List interface. On the Linked List class, check the comments, analyze then try to recode the said method/s. Post the changes in your code, if any. package com.linkedlist; public class LinkedList implements ListI{ //The Node Object signatures and constructor class Node{ E data; Node next; public Node(E obj) { data=obj; next=null; } } private Node head; private Node tail; private int currentSize; //The Linked List constructor public LinkedList() { head=null; tail=null; currentSize=0; } //Add Node at the Start of the List public void addFirst(E obj) { Node node = new Node(obj); node.next = head; head = node; currentSize++; } //Add Node at the End of the List with complexity O(n) public void addLast(E obj) { Node node = new Node(obj); Node tmp = head; if(head==null) { head = node; currentSize++; return; } while(tmp.next != null) { tmp = tmp.next; } tmp.next = node; currentSize++; } //Can we write an alternative version of addLast method to improve complexity to O(1). If yes, do so. //Remove Node at the Start of the List public E removeFirst() { if(head == null) { return null; } E temporary = head.data; if(head == tail) { head=tail=null; }else { head=head.next; } currentSize--; return temporary; } //Remove Node at the End of the List public E removeLast() { if(head == null) { return null; } if(head == tail) { return removeFirst(); } Node current = head,previous = null; while(current != tail) { previous = current; current = current.next; } previous.next = null; tail=previous; currentSize--; return current.data; } //Remove Node at the Middle of the List public E remove(E obj) { Node current = head, previous=null; while(current!= null) { if(((Comparable)current.data).compareTo(obj) == 0) { if(current == head) { return removeFirst(); } if(current == tail) { return removeLast(); } currentSize--; previous.next = current.next; return current.data; } previous = current; current = current.next; } return null; } //Ask whether a data element is present on the list public boolean contains(E obj) { Node current = head; while(current != null) { if(((Comparable)obj).compareTo(current.data) == 0) { return true; } current = current.next; } return false; } //Peek the data element at the beginning of the list public E peekFirst() { if(head == null) { return null; } return head.data; } //Peek the data element at the end of the list public E peekLast() { Node tmp = head; while(tmp.next != null) { tmp = tmp.next; } return tmp.data; } //Can we write an alternative version of peekLast method to improve complexity to O(1). If yes, do so. } package com.linkedlist; public interface ListI { public void addFirst(E obj); public void addLast(E obj); public E removeFirst(); public E removeLast(); public E remove(E obj); public boolean contains(E obj); public E peekFirst(); public E peekLast(); }
Code Restructuring: Methods for Linked List operations are given. Restructure the codes to improve performance of codes.
Linked List is a part of the Collection framework present in java.util package, however, to be able to check the complexity of Linked List operations, we can recode the data structure based on Java Documentation https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
Instruction 1: Two (2) Java classes are provided, first, the Linked List class and second, the Linked List interface. On the Linked List class, check the comments, analyze then try to recode the said method/s. Post the changes in your code, if any.
package com.linkedlist;
public class LinkedList<E> implements ListI<E>{
//The Node Object signatures and constructor
class Node<E>{
E data;
Node<E> next;
public Node(E obj) {
data=obj;
next=null;
}
}
private Node<E> head;
private Node<E> tail;
private int currentSize;
//The Linked List constructor
public LinkedList() {
head=null;
tail=null;
currentSize=0;
}
//Add Node at the Start of the List
public void addFirst(E obj) {
Node<E> node = new Node<E>(obj);
node.next = head;
head = node;
currentSize++;
}
//Add Node at the End of the List with complexity O(n)
public void addLast(E obj) {
Node<E> node = new Node<E>(obj);
Node<E> tmp = head;
if(head==null) {
head = node;
currentSize++;
return;
}
while(tmp.next != null) {
tmp = tmp.next;
}
tmp.next = node;
currentSize++;
}
//Can we write an alternative version of addLast method to improve complexity to O(1). If yes, do so.
//Remove Node at the Start of the List
public E removeFirst() {
if(head == null) {
return null;
}
E temporary = head.data;
if(head == tail) {
head=tail=null;
}else {
head=head.next;
}
currentSize--;
return temporary;
}
//Remove Node at the End of the List
public E removeLast() {
if(head == null) {
return null;
}
if(head == tail) {
return removeFirst();
}
Node<E> current = head,previous = null;
while(current != tail) {
previous = current;
current = current.next;
}
previous.next = null;
tail=previous;
currentSize--;
return current.data;
}
//Remove Node at the Middle of the List
public E remove(E obj) {
Node<E> current = head, previous=null;
while(current!= null) {
if(((Comparable<E>)current.data).compareTo(obj) == 0) {
if(current == head) {
return removeFirst();
}
if(current == tail) {
return removeLast();
}
currentSize--;
previous.next = current.next;
return current.data;
}
previous = current;
current = current.next;
}
return null;
}
//Ask whether a data element is present on the list
public boolean contains(E obj) {
Node<E> current = head;
while(current != null) {
if(((Comparable<E>)obj).compareTo(current.data) == 0) {
return true;
}
current = current.next;
}
return false;
}
//Peek the data element at the beginning of the list
public E peekFirst() {
if(head == null) {
return null;
}
return head.data;
}
//Peek the data element at the end of the list
public E peekLast() {
Node<E> tmp = head;
while(tmp.next != null) {
tmp = tmp.next;
}
return tmp.data;
}
//Can we write an alternative version of peekLast method to improve complexity to O(1). If yes, do so.
}
package com.linkedlist;
public interface ListI<E> {
public void addFirst(E obj);
public void addLast(E obj);
public E removeFirst();
public E removeLast();
public E remove(E obj);
public boolean contains(E obj);
public E peekFirst();
public E peekLast();
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps