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 item) {
setHead(new Node<>(item,
getHead()));
size++;
}
/** Add a node after a given node
@param node The node preceding the new item
@param item The item to insert
*/
private void addAfter(Node<E> node, E item)
{
node.next = new Node<>(item,
node.next);
size++;
}
/** Remove the node after a given node
@param node The node before the one to be
removed
@return The data from the removed node, or null
if there is no node to remove
*/
@SuppressWarnings("unused")
private E removeAfter(Node<E> node) {
Node<E> temp =
node.next;
if (temp != null) {
node.next =
temp.next;
size--;
return
temp.data;
}
else {
return
null;
}
}
/** Remove the first node from the list
@return The removed node's data or null if the list is
empty
*/
@SuppressWarnings("unused")
private E removeFirst() {
Node<E> temp =
getHead();
if (getHead() != null) {
setHead(getHead().next);
}
// Return data at old head or null
if list is empty
if (temp != null) {
size--;
return
temp.data;
}
else {
return
null;
}
}
/** Find the node at a specified position
@param index The position of the node sought
@return The node at index or null if it does not
exist
*/
private Node<E> getNode(int index) {
Node<E> node =
getHead();
for (int i = 0; i < index
&& node != null; i++) {
node =
node.next;
}
return node;
}
/** Get the data at index
@param index The position of the data to return
@return The data at index
@throws IndexOutOfBoundsException if index is out of
range
*/
public E get(int index) {
if (index < 0 || index >=
size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node =
getNode(index);
return node.data;
}
/** Store a reference to anEntry in the element at
position index.
@param index The position of the item to change
@param newValue The new data
@return The data previously at index
@throws IndexOutOfBoundsException if index is out of
range
*/
public E set(int index, E newValue) {
if (index < 0 || index >=
size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node =
getNode(index);
E result = node.data;
node.data = newValue;
return result;
}
/** Insert the specified item at index
@param index The position where item is to be
inserted
@param item The item to be inserted
@throws IndexOutOfBoundsException if index is out of
range
*/
public void add(int index, E item) {
if (index < 0 || index >
size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
addFirst(item);
}
else {
Node<E>
node = getNode(index-1);
addAfter(node,
item);
}
}
/** Append item to the end of the list
@param item The item to be appended
@return true (as specified by the Collection
interface)
*/
public boolean add(E item) {
add(size, item);
return true;
}
public Node<E> getHead() {
return head;
}
public void setHead(Node<E> head)
{
this.head = head;
}
public String toString() {
@SuppressWarnings("unchecked")
Node<String> nodeRef =
(Node<String>) head;
StringBuilder result = new
StringBuilder();
while (nodeRef != null) {
result.append(nodeRef.data);
if (nodeRef.next != null) {
result.append(" ==> ");
}
nodeRef = nodeRef.next;
}
return result.toString();
}
public int remove(E item) {
if(head == null) {
return -1;
}
if(head.data.equals(item)) {
head =
head.next;
size--;
return 0;
}
int index = 1;
Node<E> node = head;
while(node.next != null) {
if(node.next.data.equals(item)) {
node.next = node.next.next;
size--;
return index;
}
}
return -1;
}
/** Return the size of the linked list.
*/
public int getSize() {
return size;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
I need help implementing the following method:
/** Insert a new item before the one at position
index,
starting at 0 for the list head. The new item is
inserted between the one
at position index-1 and the one formerly at position
index.
If the index is zero, add the new item to the
first
@param index The index where the new item is to be
inserted
@param item The item to be inserted
@throws IndexOutOfBoundsException if the index is out
of range
*/
// You should take a look at addAfter(Node<E>
node, E item)
// You need to consider two situations: index == 0 and
index > 0
// Hint:
// 1. Check whether the index is valid
// 2. if index == 0, create a new node with item and
refer to this.head
// and set the new node to this.head, using the
constructor: Node(E dataItem, Node<E> nodeRef):
// this.head = new Node<>(item,
this.head);
// 3. Otherwise, Node<E>node = getNode(index-1),
and use item and node.next to create a new node
// and give the new node to node.next.
// 4. Don't forget to update the size
public void addBefore(int index, E item) {
// Add your code here
}
Code
Code
You didn't provid the Node class so i have created my own class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author JOSEE
*/
public class Node <E> {
E data;
Node next;
public Node(E data, Node next) {
this.data = data;
this.next = next;
}
}
KWSingleLinkedList.java
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 item) {
setHead(new Node<>(item, getHead()));
size++;
}
/** Add a node after a given node
@param node The node preceding the new item
@param item The item to insert
*/
private void addAfter(Node<E> node, E item) {
node.next = new Node<>(item, node.next);
size++;
}
/** Remove the node after a given node
@param node The node before the one to be removed
@return The data from the removed node, or null
if there is no node to remove
*/
@SuppressWarnings("unused")
private E removeAfter(Node<E> node) {
Node<E> temp = node.next;
if (temp != null) {
node.next = temp.next;
size--;
return temp.data;
}
else {
return null;
}
}
/** Remove the first node from the list
@return The removed node's data or null if the list is empty
*/
@SuppressWarnings("unused")
private E removeFirst() {
Node<E> temp = getHead();
if (getHead() != null) {
setHead(getHead().next);
}
// Return data at old head or null if list is empty
if (temp != null) {
size--;
return temp.data;
}
else {
return null;
}
}
/** Find the node at a specified position
@param index The position of the node sought
@return The node at index or null if it does not exist
*/
private Node<E> getNode(int index) {
Node<E> node = getHead();
for (int i = 0; i < index && node != null; i++) {
node = node.next;
}
return node;
}
/** Get the data at index
@param index The position of the data to return
@return The data at index
@throws IndexOutOfBoundsException if index is out of range
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index);
return node.data;
}
/** Store a reference to anEntry in the element at position
index.
@param index The position of the item to change
@param newValue The new data
@return The data previously at index
@throws IndexOutOfBoundsException if index is out of range
*/
public E set(int index, E newValue) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index);
E result = node.data;
node.data = newValue;
return result;
}
/** Insert the specified item at index
@param index The position where item is to be inserted
@param item The item to be inserted
@throws IndexOutOfBoundsException if index is out of range
*/
public void add(int index, E item) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
addFirst(item);
}
else {
Node<E> node = getNode(index-1);
addAfter(node, item);
}
}
/** Append item to the end of the list
@param item The item to be appended
@return true (as specified by the Collection interface)
*/
public boolean add(E item) {
add(size, item);
return true;
}
public Node<E> getHead() {
return head;
}
public void setHead(Node<E> head) {
this.head = head;
}
public String toString() {
@SuppressWarnings("unchecked")
Node<String> nodeRef = (Node<String>) head;
StringBuilder result = new StringBuilder();
while (nodeRef != null) {
result.append(nodeRef.data);
if (nodeRef.next != null) {
result.append(" ==> ");
}
nodeRef = nodeRef.next;
}
return result.toString();
}
public int remove(E item) {
if(head == null) {
return -1;
}
if(head.data.equals(item)) {
head = head.next;
size--;
return 0;
}
int index = 1;
Node<E> node = head;
while(node.next != null) {
if(node.next.data.equals(item)) {
node.next = node.next.next;
size--;
return index;
}
}
return -1;
}
/** Return the size of the linked list.
*/
public int getSize() {
return size;
}
//I need help implementing the following method:
/** Insert a new item before the one at position index,
starting at 0 for the list head. The new item is inserted between
the one
at position index-1 and the one formerly at position index.
If the index is zero, add the new item to the first
@param index The index where the new item is to be inserted
@param item The item to be inserted
@throws IndexOutOfBoundsException if the index is out of
range
*/
// You should take a look at addAfter(Node<E> node, E
item)
// You need to consider two situations: index == 0 and index >
0
// Hint:
// 1. Check whether the index is valid
// 2. if index == 0, create a new node with item and refer to
this.head
// and set the new node to this.head, using the constructor: Node(E
dataItem, Node<E> nodeRef):
// this.head = new Node<>(item, this.head);
// 3. Otherwise, Node<E>node = getNode(index-1), and use item
and node.next to create a new node
// and give the new node to node.next.
// 4. Don't forget to update the size
public void addBefore(int index, E item) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
Node newNode=new Node(item,head);
this.head=newNode;
return;
}
Node nextNode=this.head;
Node prev=nextNode;
int i=0;
while(nextNode!=null)
{
if(i==index)
break;
prev=nextNode;
nextNode=nextNode.next;
i++;
}
Node newNode=new Node(item,null);
prev.next=newNode;
newNode.next=nextNode;
}
}
test.java
public class test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
KWSingleLinkedList<String> list=new
KWSingleLinkedList<>();
list.add("One");
list.add("Three");
list.addBefore(1, "Two");
System.out.println(list);
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size)...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
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...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
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;...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
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);...
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...
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...
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...