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