Question: Your task is to write a concrete class (a data structure) called MySinglyLinkedList that will implement this interface. Note that this class will need to
| Your task is to write a concrete class (a data structure) called MySinglyLinkedList that will implement this interface. Note that this class will need to use Java generics as well. Below you can find skeleton code for it. |
| MySinglyLinkedList class |
|
public class MySinglyLinkedList protected Node
public MySinglyLinkedList(){ this.head = null; }
// Your code goes here } |
The code for the Node class is provided below
| Node class [CANNOT be modified!] |
|
public class Node protected T payload; protected Node
public Node(){ this.payload = null; this.next = null; }
public Node(T initialPayload, Node this.payload = initialPayload; this.next = nextNode; }
public T getPayload(){ return this.payload; }
public Node return this.next; }
public void setPayload(T newPayload){ this.payload = newPayload; }
public void setNext(Node this.next = newNext; }
public String toString(){ return "Payload: " + this.payload + " | next: (" + this.next + ")"; } } |
If your implementation is correct, you should obtain the following output:
| ListTest1 class output |
|
This list is empty Element at the front: null Element at the tail: null List size: 0 Index of element 100 (if exists): -1 Index of element 200 (if exists): -1
Element at Node 0: 100 Element at Node 1: 3 Element at Node 2: 4 Element at Node 3: 5 Element at Node 4: 6 Element at Node 5: 7 Index of element 100 (if exists): 0 Index of element 200 (if exists): -1
Element at Node 0: 100 Element at Node 1: 3 Element at Node 2: 4 Element at Node 3: 5 Element at Node 4: 6 Element at Node 5: 7
Element at Node 0: 100 Element at Node 1: 3 Element at Node 2: 4 Element at Node 3: 5 Element at Node 4: 6 Element at Node 5: 7 Element at Node 6: 8
Element at Node 0: 9 Element at Node 1: 100 Element at Node 2: 3 Element at Node 3: 4 Element at Node 4: 5 Element at Node 5: 6 Element at Node 6: 7 Element at Node 7: 8 Index of element 100 (if exists): 1 Index of element 200 (if exists): -1
Element at Node 0: 10 Element at Node 1: 9 Element at Node 2: 100 Element at Node 3: 3 Element at Node 4: 4 Element at Node 5: 5 Element at Node 6: 6 Element at Node 7: 7 Element at Node 8: 8
Element at Node 0: 10 Element at Node 1: 9 Element at Node 2: 0 Element at Node 3: 100 Element at Node 4: 3 Element at Node 5: 4 Element at Node 6: 5 Element at Node 7: 6 Element at Node 8: 7 Element at Node 9: 8
Element at index -1: null Element at index 2: 0
Element at the front: 10 Element at the tail: 8 List size: 10
Element at Node 0: 10 Element at Node 1: 9 Element at Node 2: 0 Element at Node 3: 4 Element at Node 4: 5 Element at Node 5: 6 Element at Node 6: 7 Element at Node 7: 8
Element at the front: 10 Element at the tail: 8 List size: 8
Element at Node 0: 9 Element at Node 1: 0 Element at Node 2: 4 Element at Node 3: 5 Element at Node 4: 6 Element at Node 5: 7 Element at Node 6: 8
Element at the front: 9 Element at the tail: 8 List size: 7
Element at Node 0: 9 Element at Node 1: 0 Element at Node 2: 4 Element at Node 3: 5 Element at Node 4: 6 Element at Node 5: 7
Element at the front: 9 Element at the tail: 7 List size: 6
Element at Node 0: 0 Element at Node 1: 4 Element at Node 2: 5 Element at Node 3: 6 Element at Node 4: 7
Element at the front: 0 Element at the tail: 7 List size: 5
Element at Node 0: 4 Element at Node 1: 5 Element at Node 2: 6 Element at Node 3: 7
Element at the front: 4 Element at the tail: 7 List size: 4
Element at Node 0: 5 Element at Node 1: 6 Element at Node 2: 7
Element at the front: 5 Element at the tail: 7 List size: 3
Element at Node 0: 6 Element at Node 1: 7
Element at the front: 6 Element at the tail: 7 List size: 2
Element at Node 0: 7
Element at the front: 7 Element at the tail: 7 List size: 1
This list is empty
Element at the front: null Element at the tail: null List size: 0
|
MY CODE:
public class MySinglyLinkedList
protected Node
public MySinglyLinkedList(){
this.head = null;
}
/**
* Appends the specified element to the end (tail) of this List.
*
* @param element new element
*
*/
private Object current;
@Override
public void add(E element) {
Node
if(this.head == null) {
this.head = newnode;
}
else {
Node
while (last != null) {
if(last.getNext() == null) {
break;
}
last = last.getNext();
}
last.setNext(newnode);
}
}
/**
* Inserts the specified element at the specified position index in this List.
*
* @param index new element index
* @param element new element
*/
@Override
public void add(int index, E element) {
Node
if (index == 0 ) {
newNode.setNext(this.head);
this.head = newNode;
}
else {
int counter = 0;
Node
while (current != null) {
if (index == counter) {
newNode.setNext(current.getNext());
current.setNext(newNode);
}
}
}
current = current.getNext();
counter ++;
}
/**
* Appends the specified element to the front of this List.
*
* @param element new element
*/
@Override
public void addFront(E element) {
Node
if(this.head == null) {
this.head = newnode;
}
else {
newnode.next = this.head;
this.head = newnode
}
}
/**
* Returns the element at the specified position (index) in this List.
*
* @param index index of the element to be returned
* @return int element at position index. null if index >= size
*/
@Override
public E get(int index) {
if (index >= size()) {
return null;
}
return this.get(index);
}
/**
* Returns the first element in this List.
*
* @param index index of the element to be returned
* @return int element at position index. null if List is empty
*/
@Override
public E getFront() {
Node
if(current == null) {
return null;
}
return current.getPayload();
}
/**
* Returns the last element in this List.
*
* @param index index of the element to be returned
* @return int element at position index. null if List is empty
*/
@Override
public E getTail() {
Node
while (current.getNext() != null) {
if(current.getNext() == null) {
return current.getPayload();
}
current = current.getNext();
}
return current.getPayload();
}
/**
* Returns the index in this List of the first occurrence of the specified key, or -1 if List does not contain this key.
*
* @param key specified key to be found in List
* @return int index of the specified key in List or -1 if not found
*/
@Override
public int indexOf(E key) {
Node
int index = 0;
while (current != null) {
if (current.getPayload() == key ) {
return index;
}
current = current.getNext();
index ++;
}
return -1;
}
/**
* Removes the element at the specified position in this List.
*
* @param index index of the element to be removed
*/
@Override
public void remove(int index) {
Node
if (index == 0 && currentNode != null) {
this.head = currentNode.getNext();
System.out.println(index + "position element deleted");
return;
}
int counter = 0;
while (currentNode != null) {
previous = currentNode.getNext();
System.out.println(index + "position element deleted");
break;
}
}
/**
* Removes the last element of the List.
*
*/
@Override
public void removeTail() {
if (this.head == null || head.next == null) {
return;
}
Node
while(head.getNext() != null){
temp = temp.getNext();
}
temp.next = null;
return;
}
/**
* Returns the number of elements in this List.
*
* @return int number of elements in List
*/
@Override
public int size() {
Node
int count = 0;
while (temp != null) {
count ++;
temp = temp.next;
}
return count++;
}
/**
* Lists/displays all elements in this List.
*
*/
@Override
public void listAll() {
Node
System.out.println("LinkedList : ");
while (currentNode != null) {
System.out.println(this.current + " ");
currentNode = currentNode.getNext();
}
return;
}
}
HOW can I fix my code:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
