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(); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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();

}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY