Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list.
Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list?
Submit three files: LinkedList.java, Node.java, and linked_list.txt, a text file with your answers to the time complexity questions.
public class Node {
private String value;
private Node next;
private Node prev;
public Node(String value) { this.value = value; }
public String getValue() {
return value;
}
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; }
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
}
// Throw an IndexOutOfBoundsException if the index is invalid public String get(int index); // LOCATE(v) → p // Return the first index in the list with the given value, or -1 if it's not found public int find(String val); // APPEND(v) // Return true if the element is successfully added, false if the list is full public boolean add(String val); // INSERT(p,v) // Throw an IndexOutOfBoundsException if the index is invalid public void add(int index, String val); // DELETE(v) // This method should return true only if the value is in the list and removed, // false if the value isn't found in the list public boolean remove(String val); // DELETE(p) // return the value that's removed; throw an IndexOutOfBoundsException if the index is invalid public String remove(int index); // LENGTH() → int public int size(); // Return true if the list is empty, false if it's not public boolean isEmpty(); // Return true if the value is in the list, false if it's not public boolean contains(String value); // DELETE_ALL() public void clear(); // Convert the contents into a single string with values separated by commas. // It's OK to have a comma at the end of the String. public String toString();
Here is the completed code for this problem. Runtime complexity of each method is attached in comments before each method. No changed made to Node class. 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. Thanks
// LinkedList.java
public class LinkedList {
// pointers to head and tail nodes
private Node head;
private Node tail;
// current number of nodes
private int size;
// constructor initializing empty list
public LinkedList() {
head = tail = null;
size = 0;
}
// returns the element at index position, throws IndexOutOfBoundsException
// if index is invalid
// runtime complexity: O(n)
public String get(int index) {
if (index < 0 || index >= size) {
// invalid index
throw new IndexOutOfBoundsException();
}
int i = 0;
Node temp = head;
// looping until i equals index
while (i < index) {
temp = temp.getNext();
i++;
}
// returns value of current temp node
return temp.getValue();
}
// returns the first index of val if exists, else returns -1
// runtime complexity: O(n)
public int find(String val) {
int i = 0;
Node temp = head;
// looping and comparing each node's value with val
while (temp != null) {
if (temp.getValue().equals(val)) {
// found
return i;
}
temp = temp.getNext();
i++;
}
return -1;
}
// adds an element at the end
// runtime complexity: O(1)
public boolean add(String val) {
// creating a new node
Node n = new Node(val);
// if empty, adding as both head and tail
if (isEmpty()) {
head = n;
tail = n;
} else {
// otherwise appending to tail
tail.setNext(n);
tail = n;
}
// updating size and returning true
size++;
return true;
}
// adds an element at specified index, throws IndexOutOfBoundsException
// if index is invalid
// runtime complexity: O(n)
public void add(int index, String val) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
// if index==size, adding to end
if (index == size) {
add(val);
} else if (index == 0) {
// adding before head
Node n = new Node(val);
n.setNext(head);
head = n;
size++;
} else {
Node n = new Node(val);
Node temp = head;
// finding node at index-1 position
for (int i = 0; i < index - 1; i++) {
temp = temp.getNext();
}
// adding n between temp and temp.next
n.setNext(temp.getNext());
temp.setNext(n);
size++;
}
}
// removes first occurrance of an element if exists
// runtime complexity: O(n)
public boolean remove(String val) {
if (!isEmpty()) {
if (head.getValue().equals(val)) {
// removing head node
head = head.getNext();
// updating tail if head become null
if (head == null) {
tail = null;
}
size--;
return true; // success
}
Node temp = head;
while (temp.getNext() != null) {
// checking if next node is tha node to be deleted
if (temp.getNext().getValue().equals(val)) {
// removing temp.next
temp.setNext(temp.getNext().getNext());
size--;
if (temp.getNext() == null) {
// updating tail
tail = temp;
}
return true; // success
}
temp = temp.getNext();
}
}
return false; // not found
}
// removes and returns an element at specified index
// runtime complexity: O(n)
public String remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
if (index == 0) {
// removing head node
String str = head.getValue();
head = head.getNext();
if (head == null) {
tail = null;
}
size--;
return str;
} else {
Node temp = head;
// finding node at index-1
for (int i = 0; i < index - 1; i++) {
temp = temp.getNext();
}
// storing value, removing temp.next, updating size and returning
// removed value
String str = temp.getNext().getValue();
temp.setNext(temp.getNext().getNext());
size--;
return str;
}
}
// returns the size
// runtime complexity: O(1)
public int size() {
return size;
}
// returns true if list is empty
// runtime complexity: O(1)
public boolean isEmpty() {
return size == 0;
}
// returns true if value is present, else false
// runtime complexity: O(1)
public boolean contains(String value) {
return find(value) != -1;
}
// clears the list
// runtime complexity: O(1)
public void clear() {
head = tail = null;
size = 0;
}
// returns a string containing all elements separated by comma
// runtime complexity: O(n)
public String toString() {
String str = "";
// appending each node's value to str
for (Node n = head; n != null; n = n.getNext()) {
str += n.getValue() + ",";
}
return str;
}
// code for testing
public static void main(String[] args) {
// creating a LinkedList and testing all methods
LinkedList list = new LinkedList();
list.add("Oliver");
System.out.println(list);
list.add("John");
System.out.println(list);
list.add(0, "Barry");
System.out.println(list);
list.add(list.size, "Kevin");
System.out.println(list);
System.out.println("find(Barry): " + list.find("Barry"));
System.out.println("contains(Barry): " + list.contains("Barry"));
System.out.println("find(Jimmy): " + list.find("Jimmy"));
System.out.println("get(0): " + list.get(0));
list.remove(0);
System.out.println(list);
list.remove("Kevin");
System.out.println(list);
System.out.println("size: " + list.size());
System.out.println("isEmpty: " + list.isEmpty());
list.clear();
System.out.println("isEmpty: " + list.isEmpty());
}
}
/*OUTPUT*/
Oliver,
Oliver,John,
Barry,Oliver,John,
Barry,Oliver,John,Kevin,
find(Barry): 0
contains(Barry): true
find(Jimmy): -1
get(0): Barry
Oliver,John,Kevin,
Oliver,John,
size: 2
isEmpty: false
isEmpty: true
Create a complete LinkedList class which implements all of the methods listed below using dynamic memory...
Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
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);...
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;...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
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...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
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...