Question

Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

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
      
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT