Would someone be able to help me with this please? It has to do
with Linked List Arrays, and I'm very confused on what to do. Any
help along with steps would be very much appreciated. Cheers!
You are hired by a company to implement a singly-linked list that
will hold integers, using Java programming language. However, the
boss of the company insists that you don’t use any objects or
structures in your implementation. You may use only integer arrays
and individual integer variables.
Your implementaion should support the following operations:
Insertion of the first integer element as the head of the list that will contain at most n elements throughout its lifetime.
Keeping track of the ‘current’ element in the list (initially should be the head of the list)
Advance ‘current’ to the next element in the list
Reset ‘current’ to the head of the list
Return the integer value of the ‘current’ element
Insert a new value following the ‘current’ element in the list (should have no effect if n elements already had been inserted into the list)
Delete the element following the ‘current’ element in the list (should have no effect if the ‘current’ element is the last one in the list)
The boss also insists that your implementation is efficient and every operation should run in O(1)time, no matter what sequence of operations is performed. This is very important to the success of the company and they will not accept any code that is less efficient!
Fill in the missing code in the following definitions and explain why every one of your operations runs inO(1) time:
1
public class List { private int ... private int[] ...
/********************************************************************* * Initializes a linked list with integer ’val’ as the head of the list * and identifies it as the current element * Ensures that no more than ’n’ elements will be inserted into this list *********************************************************************/
public List(int n, int val) { ...
}
/********************************************************************* * Advances the current element to the next element in the linked list *********************************************************************/
public void advance() { ...
}
/********************************************************************* * Resets the current element to be the head of the list *********************************************************************/
public void reset() { ...
}
/********************************************************************* * Returns the integer value of the current element *********************************************************************/
public int value() { ...
}
/********************************************************************* * Deletes the element that follows the current element in the list * Has no effect if the current element is the last one in the list *********************************************************************/
public void deleteNext() { ...
}
/********************************************************************* * Inserts ’val’ into the list as the element that follows the current * one. Has no effect if n elements had already been inserted into the list * (not counting deletions) **********************************************************************/
public void insertNext(int val) { ...
} }
public class List {
private int val;
private int[] elements;
/*********************************************************************
* Initializes a linked list with integer ’val’ as the head of the
list
* and identifies it as the current element
* Ensures that no more than ’n’ elements will be inserted into this
list
*********************************************************************/
public List(int n, int val) {
this.elements = new int[n];
this.val=0;
}
/*********************************************************************
* Advances the current element to the next element in the linked
list
*********************************************************************/
public void advance() {
if(this.val<elements.length - 1)
this.val++;
}
/*********************************************************************
* Resets the current element to be the head of the list
*********************************************************************/
public void reset() {
this.val=0;
}
/*********************************************************************
* Returns the integer value of the current element
*********************************************************************/
public int value() {
return elements[this.val];
}
/*********************************************************************
* Deletes the element that follows the current element in the
list
* Has no effect if the current element is the last one in the
list
*********************************************************************/
public void deleteNext() {
if(this.val<elements.length - 1)
elements[++this.val]=0;
}
/*********************************************************************
* Inserts ’val’ into the list as the element that follows the
current
* one. Has no effect if n elements had already been inserted into
the list
* (not counting deletions)
**********************************************************************/
public void insertNext(int val) {
if(this.val<elements.length - 1)
elements[++this.val]=val;
}
}
Would someone be able to help me with this please? It has to do with Linked...
Parts B is about our linked list implementation. avvut our linked list implementation. Recall that the declaration of a list node is. class DListNode { public int key; public DistNode previous; public DListNode next; de ollos Salon You can assume that this is an inner class within the full MyLinked List class. B. Write a method that will add a new element to the front of a doubly linked list. Assume that head and tail point to the head and...
iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...
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...
n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...
*********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...