In: Computer Science
myLinkedList.java>>>>>>>>
public class MyLinkedList implements
MiniList<Integer>{
/* Private member variables that you need to declare:
** The head pointer
** The tail pointer
*/
public class Node {
// declare member variables (data and next)
// finish these constructors
public Node(int data, Node next) {}
public Node(int data) {} // HINT: use this() with next = null
}
// Initialize the linked list (set head and tail pointers)
public MyLinkedList() {}
@Override
public boolean add(Integer item) {
return false;
}
@Override
public void add(int index, Integer element) {
}
@Override
public Integer remove() {
return null;
}
@Override
public Integer remove(int index) {
return null;
}
@Override
public boolean remove(Integer item) {
return false;
}
@Override
public void clear() {
}
@Override
public boolean contains(Integer item) {
return false;
}
@Override
public Integer get(int index) {
return null;
}
@Override
public int indexOf(Integer item) {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int size() {
return 0;
}
// Uncomment when ready to test
// @Override
// public String toString() {
// String ret = "";
// Node curr = head;
// while (curr != null) {
// ret += curr.data + " ";
// curr = curr.next;
// }
// return ret;
// }
}
------------------------------------------------------------------------------------
MiniList.java>>>>>>>>
public interface MiniList<E> {
public boolean add(E item);
public void add(int index, E element);
public E remove();
public E remove(int index);
public boolean remove(E item);
public void clear();
public boolean contains(E item);
public boolean equals(Object o);
public E get(int index);
public int indexOf(E item);
public boolean isEmpty();
public int size();
}
--------------------------------------------------------------------------------------------
LLTest.java>>>>>>>>>>>>>>>>>>>>>>>>
public class LLTest {
public static void main(String args[]) {
MyLinkedList ll = new MyLinkedList();
// Testing add methods
System.out.println("Testing Add");
ll.add(40);
ll.add(10);
ll.add(20);
ll.add(1, 30);
ll.add(3, 100);
ll.add(65);
ll.add(2);
System.out.println("Expected: 40 30 10 100 20 65 2");
System.out.println("Actual: " + ll);
System.out.println();
// Testing remove methods
System.out.println("Testing Remove");
ll.remove();
ll.remove(2);
ll.remove(3);
ll.remove((Integer)2);
System.out.println("Expected: 30 10 20");
System.out.println("Actual: " + ll);
System.out.println("Size should be 3 -> " + ll.size());
System.out.println();
// Testing Contains
System.out.println("Testing Contains");
ll.add(2); // to make it a little bigger
System.out.println("Should be true -> " + ll.contains(2));
System.out.println("Should be false -> " +
ll.contains(65));
System.out.println("Should be true -> " +
ll.contains(30));
System.out.println();
// Testing Get
System.out.println("Testing Get");
System.out.println("Actual list: " + ll);
System.out.print("List using get: ");
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println("\n");
// Testing indexOf
System.out.println("Testing indexOf");
System.out.println("Should be 2 -> " + ll.indexOf(20));
System.out.println("Should be 3 -> " + ll.indexOf(2));
System.out.println("Should be -1 -> " + ll.indexOf(65));
// You can write more tests (these are not all
encompassing)
// When should these methods fail/throw exceptions?
// Have all of the edge cases been tested?
// Not quite all of the methods have been tested here (e.g.
clear())
}
}
----------------------------------------------------------------------------------
directions-
complete all of the methods in the the given MiniList interface. The documentation for these methods match the ones in Oracle's documentation for a linked list
write the node class which should contain two member variables: data and next.
OUTPUT - I have added an own display function for testing. Feel
free to remove that.
CODE:
- Tried my best to
cover all the functions and appropriate return types. Kindly upvote
if this helped you.
- Define you own package at the top if needed.
public class MyLinkedList implements MiniList < Integer > {
/* Private member variables that you need to declare:
** The head pointer
** The tail pointer
*/
class Node {
Integer data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node h;
public Node t;
// Initialize the linked list (set head and tail pointers)
public MyLinkedList() {
h = null;
t = null;
}
@Override
public boolean add(Integer item) {
Node newNode = new Node(item);
if (h == null) {
h = newNode;
t = newNode;
} else {
t.next = newNode;
t = newNode;
}
return true;
}
@Override
public void add(int index, Integer element) {
Node current = h;
if (h == null) {
return;
}
int count = 0;
while (current != null) {
if (index - 1 == count) {
Node newNode = new Node(element);
newNode.next = current.next;
current.next = newNode;
break;
}
current = current.next;
count++;
}
}
@Override
public Integer remove() {
//Node current = h;
Integer ele = null;
ele = h.data;
h = h.next;
// if (h == null) {
// return null;
// }
// while (current != null) {
// if (current.next.next == null) {
// ele = current.next.data;
// current.next = current.next.next;
// break;
// }
// current = current.next;
// }
return ele;
}
@Override
public Integer remove(int index) {
Node current = h;
Integer ele = null;
int count = 0;
if (h == null) {
return null;
}
while (current != null) {
if (index - 1 == count && current.next != null) {
ele = current.next.data;
if (current.next.next == null) {
// t.next = current.next;
t = current;
}
current.next = current.next.next;
//current.next.next = null;
break;
}
current = current.next;
count++;
}
return ele;
}
@Override
public boolean remove(Integer item) {
Node current = h;
if (h == null) {
return false;
}
if (current.data == item) {
h = current.next;
return true;
}
while (current != null) {
if (current.next != null && current.next.data == item) {
if (current.next.next == null) {
// t.next = current.next;
t = current;
}
current.next = current.next.next;
//current.next.next = null;
break;
}
current = current.next;
}
return true;
}
@Override
public void clear() {
h = null;
}
@Override
public boolean contains(Integer item) {
Node current = h;
if (h == null) {
return false;
}
while (current != null) {
if (current.data == item) {
return true;
}
current = current.next;
}
return false;
}
@Override
public Integer get(int index) {
Node current = h;
int count = 0;
if (h == null) {
return null;
}
while (current != null) {
if (index == count) {
return current.data;
}
current = current.next;
count++;
}
return null;
}
@Override
public int indexOf(Integer item) {
Node current = h;
int count = 0;
if (h == null) {
return -1;
}
while (current != null) {
if (current.data == item) {
return count;
}
current = current.next;
count++;
}
return -1;
}
@Override
public boolean isEmpty() {
if (h == null) {
return true;
}
return false;
}
@Override
public int size() {
Node current = h;
int count = 0;
if (h == null) {
return 0;
}
while (current != null) {
current = current.next;
count++;
}
return count;
}
public void display() {
Node current = h;
if (h == null) {
System.out.println("No data in list");
return;
}
System.out.println("Nodes info are ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
// Uncomment when ready to test
// @Override
// public String toString() {
// String ret = "";
// Node curr = head;
// while (curr != null) {
// ret += curr.data + " ";
// curr = curr.next;
// }
// return ret;
// }
}
interface MiniList < E > {
public boolean add(E item);
public void add(int index, E element);
public E remove();
public E remove(int index);
public boolean remove(E item);
public void clear();
public boolean contains(E item);
public boolean equals(Object o);
public E get(int index);
public int indexOf(E item);
public boolean isEmpty();
public int size();
}
class LLTest {
public static void main(String args[]) {
MyLinkedList ll = new MyLinkedList();
// Testing add methods
System.out.println("Testing Add");
ll.add(40);
ll.add(10);
ll.add(20);
ll.add(1, 30);
ll.add(3, 100);
ll.add(65);
ll.add(2);
System.out.println("Expected: 40 30 10 100 20 65 2");
System.out.print("Actual: ");
ll.display();
System.out.println();
// Testing remove methods
System.out.println("Testing Remove");
ll.remove();
ll.remove(2);
ll.remove(3);
ll.remove((Integer) 2);
System.out.println("Expected: 30 10 20");
System.out.print("Actual: ");
ll.display();
System.out.println("Size should be 3 -> " + ll.size());
System.out.println();
// Testing Contains
System.out.println("Testing Contains");
ll.add(2); // to make it a little bigger
System.out.print("TESTING: ");
ll.display();
System.out.println("Should be true -> " + ll.contains(2));
System.out.println("Should be false -> " + ll.contains(65));
System.out.println("Should be true -> " + ll.contains(30));
System.out.println();
// Testing Get
System.out.println("Testing Get");
System.out.print("Actual: ");
ll.display();
System.out.print("List using get: ");
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println("\n");
// Testing indexOf
System.out.println("Testing indexOf");
System.out.println("Should be 2 -> " + ll.indexOf(20));
System.out.println("Should be 3 -> " + ll.indexOf(2));
System.out.println("Should be -1 -> " + ll.indexOf(65));
// You can write more tests (these are not all encompassing)
// When should these methods fail/throw exceptions?
// Have all of the edge cases been tested?
// Not quite all of the methods have been tested here (e.g. clear())
}
}