Here's the link to the full question; it is for Java; I would appreciate for the full Java solution to the question: htt

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Here's the link to the full question; it is for Java; I would appreciate for the full Java solution to the question: htt

Post by answerhappygod »

Here's the link to the full question; it is for Java; I wouldappreciate for the full Java solution to the question:
https://docs.google.com/document/d/1Slt ... sp=sharing
Here's my current progress:
LinkedList.java (1 compiler error; how tofix??):
public class LinkedList <T> implements List<T> {
private class Node<T> { private T data; private Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public Node(T data) { this(data, null); } public T getData() { return data; } public Node<T> getNext() { return next; } public void setData(T data) { this.data = data; } public void setNext(Node<T> next) { this.next = next; }}private Node<T> head;private Node<T> tail;private int size = 0;
public LinkedList() { head = null; tail = null;}
public Node<T> getHead() { return head; }
public Node<T> getTail() { return tail;}
@Overridepublic void addAtIndex(T data, int index) { if (index == 1 ){ if(isEmpty()) { head = newNode(data); tail = head; return; } Node<T> newNode = newNode(data); newNode.next = head; head = newNode; return; } Node<T> current = head; int count = 1; while(count < index-1) { count++; current =current.next; } Node<T> newNode = newNode(data); newNode.next = current.next; current.next = newNode;}
@Overridepublic T getAtIndex(int index) {
/** * Retrieves the data of the node that'sspecified. * @param index the index of the node whose datawe are retrieving */
Node<T> current = head; int val = 0; while (current != null && val != index ){ current = current.next; val += 1; } return current.getData(); } @Override public T removeAtIndex(int index) { /** * Removes the data at the specified index andreturns the data that was removed. * @param index removes the Node at thisindex */ Node<T> current = head; Node<T> prev = null; int count = 1; while(count < index) { count++; prev = current; current =current.getNext(); } prev.next = current.getNext(); current.next = null; return current.getData(); } @Override public T remove(T data) { /** * Removes the Node with the data passed in if aNode containing the data exists. * @param data the data to remove from theList */ Node<T> current = head; while (current != null) { if (current.getData() == data) { return data; } current = current.next; } return data; }
@Override public void clear() { /** * Clears the LinkedList - garbage collection isyour friend here. * THIS SOLUTION SHOULD CAN BE O(1) */ this.size = 0; this.head.data = null; this.head.next = null; } @Override public boolean isEmpty() { /** * Checks whether the LinkedList is empty ornot. * @return boolean value indicating whether it'sempty or not */ return head == null; } @Override public int size() { /** * Returns the size of the List * @return integer size of the list */ return size; }
}
Node.java (no compiler error; is itright??)
public class Node<T> { private T data; private Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public Node(T data) { this(data, null); } public T getData() { return data; } public Node<T> getNext() { return next; } public void setData(T data) { this.data = data; } public void setNext(Node<T> next) { this.next = next; }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply