I need help with todo line please
public class LinkedList {
private Node head;
public LinkedList() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
public int size() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.getNext();
}
return count;
}
public void add(int data) {
Node newNode = new Node(data);
newNode.setNext(head);
head = newNode;
}
public void append(int data) {
Node newNode = new Node(data);
if (isEmpty())
head = newNode;
else {
Node current = head;
while (current.getNext() != null)
current = current.getNext();
current.setNext(newNode);
}
}
public int get(int index) throws IndexOutOfBoundsException
{
if (index >= 0 && index < size()) {
Node current = head;
int i = 0;
while (i < index) {
current = current.getNext();
i++;
}
return current.getData();
}
throw new IndexOutOfBoundsException();
}
@Override
public String toString() {
String out = "";
Node current = head;
while (current != null) {
out += current.toString() + " ";
current = current.getNext();
}
return out;
}
// remove all references between nodes and set head to
null
void clear() {
Node current = head;
while (current != null) {
Node temp = current.getNext();
current.setNext(null);
current = temp;
}
head = null;
}
// TODO: implement the invert method
void invert() {
}
}
-----------------
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LinkedListTest {
@Test
void testInvertingEmptyList() {
LinkedList ll = new LinkedList();
ll.invert();
assertTrue(ll.isEmpty());
assertEquals(0, ll.size());
}
@Test
void testInvertingSingleElementList() {
LinkedList ll = new LinkedList();
ll.add(1);
ll.invert();
assertFalse(ll.isEmpty());
assertEquals(1, ll.size());
assertEquals(1, ll.get(0));
}
@Test
void testInvertingThreeElementList() {
LinkedList ll = new LinkedList();
ll.append(1);
ll.append(2);
ll.append(3);
ll.invert();
assertFalse(ll.isEmpty());
assertEquals(3, ll.size());
assertEquals(3, ll.get(0));
assertEquals(2, ll.get(1));
assertEquals(1, ll.get(2));
}
}
---------------------------
public class Node {
private int data;
private Node next;
public Node() {
data = 0;
next = null;
}
public Node(int data) {
this.data = data;
next = null;
}
public int getData() {
return data;
}
public Node getNext() {
return next;
}
public void setData(int data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return data + "";
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// LinkedList.java
public class LinkedList {
private Node head;
public LinkedList() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
public int size() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.getNext();
}
return count;
}
public void add(int data) {
Node newNode = new Node(data);
newNode.setNext(head);
head = newNode;
}
public void append(int data) {
Node newNode = new Node(data);
if (isEmpty())
head = newNode;
else {
Node current = head;
while (current.getNext() != null)
current = current.getNext();
current.setNext(newNode);
}
}
public int get(int index) throws IndexOutOfBoundsException {
if (index >= 0 && index < size()) {
Node current = head;
int i = 0;
while (i < index) {
current = current.getNext();
i++;
}
return current.getData();
}
throw new IndexOutOfBoundsException();
}
@Override
public String toString() {
String out = "";
Node current = head;
while (current != null) {
out += current.toString() + " ";
current = current.getNext();
}
return out;
}
// remove all references between nodes and set head to null
void clear() {
Node current = head;
while (current != null) {
Node temp = current.getNext();
current.setNext(null);
current = temp;
}
head = null;
}
// the invert method reverses the linked list
void invert() {
// declaring variables needed
Node prev = null, current = head, next = null;
// looping until current is null
while (current != null) {
// storing next of current (taking a backup)
next = current.getNext();
// setting prev as new next of current
current.setNext(prev);
// setting current as prev
prev = current;
// setting next as new current for next iteration
current = next;
}
// finally setting prev as new head, the list will be reversed by now.
head = prev;
}
}
I need help with todo line please public class LinkedList { private Node head; public LinkedList()...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> { Node<T> itsFirstNode; Node<T> itsLastNode; private int size; public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; } public Iterator<T> getIterator() { return new Iterator(this); } // THIS WILL NEED...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...
P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...