Question

I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

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 + "";
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

      }

}

Add a comment
Know the answer?
Add Answer to:
I need help with todo line please public class LinkedList { private Node head; public LinkedList()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class LinkedList {          // The LinkedList Node class    private class Node{...

    public class LinkedList {          // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)    {        this.head = new Node(gdata);    }...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    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...

    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;...

  • 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...

    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) {...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    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 =...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • 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 Nod...

    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...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    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...

  • Java - I need help creating a method that removes a node at the specific index...

    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...

  • (1) Implement the countKey(T element) method, which should return a count of the number of times...

    (1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT