Question

Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

Need help completing my instance method for deleteFront() as specified below

To the class IntegerLinkedList, add an instance method deleteFront such that …

  • Method deleteFront has no input.
  • Method deleteFront returns …
    • an empty Optional instance if the list is empty
    • an Optional instance whose value is the integer that was deleted from the front of the list, otherwise

Hints

  • https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html
import java.util.Optional;

    public class IntegerLinkedList
    {

        private IntegerNode head ;
        private int numberOfItems ;

        public int getNumberOfItems() { return numberOfItems; }

        public IntegerLinkedList() { head = null; numberOfItems = 0; }

        @Override
        public String toString()
        {
            String returnString = "[" ;
            IntegerNode node = head ;
            for ( int i = 0; i < numberOfItems; i ++ )
            {
                returnString +=  ( i == 0 ? "" : "," ) ;
                returnString += node.getData() ;
                node = node.getNext() ;
            }
            returnString += "]" ;
            return returnString ;
        } // end toString

        public void insertFront(int a)
        {
            IntegerNode nodeA = new IntegerNode(a);
            nodeA.setNext(head);
            head = nodeA;
            numberOfItems++;
        }

        public Optional deleteFront()
        {
            Optional opt = Optional.empty();
            if(head == null)
            {

            }
            else
            {
                IntegerNode temp = head.getNext();
                head = temp;

            }
            return opt;
        }
    } // end IntegerLinkedList

public class IntegerNode
{
private int data;
private IntegerNode next;
public IntegerNode () { data = 0; next = null; }
public IntegerNode ( int data ) { setData( data ); next = null; }
public int getData () { return data; }
public IntegerNode getNext () { return next; }
public IntegerNode setData ( int data ) { this.data = data; return this; }
public IntegerNode setNext( IntegerNode next ) { this.next = next; return this; }
}

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

//Everything remains same except the deleteFront function , so I am posting this function , Please make this changes to IntegerLinkedList.java file

public Optional deleteFront()

{

Optional opt = Optional.empty();

if(head == null)

{

return opt;

}

else

{

IntegerNode temp = head.getNext();

opt=Optional.of(head.getData());

head = temp;

}

return opt;

}

===========================

//I am providing Main.java method to test the code

class Main {

public static void main(String[] args) {

//System.out.println("Hello world!");

IntegerLinkedList list=new IntegerLinkedList();

list.insertFront(1);

list.insertFront(2);

list.insertFront(3);

list.insertFront(4);

list.insertFront(5);

System.out.println("Integer list contant: ");

System.out.println(list);

//Testing if deleteFront returns values when not empty otherwise prints empty

System.out.println("Testing deleteFront function");

System.out.println(list.deleteFront());

System.out.println(list.deleteFront());

System.out.println(list.deleteFront());

System.out.println(list.deleteFront());

System.out.println(list.deleteFront());

System.out.println(list.deleteFront());

}

}

=================================

//Output

Integer list contant:
[5,4,3,2,1]
Testing deleteFront function
Optional[5]
Optional[4]
Optional[3]
Optional[2]
Optional[1]
Optional.empty

Add a comment
Know the answer?
Add Answer to:
Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...
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
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    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);...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    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; /**...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • We have written the following Node class: class Node { // instance variables private int value;...

    We have written the following Node class: class Node { // instance variables private int value; private Node next; // constructor public Node(int value) { this.value = value; } // get the 'next' variable public Node getNext() { return this.next; } // get the 'value' variable public int getValue() { return this.value; } // set the 'next' variable public void setNext(Node next) { this.next = next; } } TASK: Create a class called List that has the following properties: It...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • 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 Nod...

    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...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    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;...

  • Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,In...

    Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix)    {        LinkedStack stack2 = new LinkedStack();        int val1;        int val2;        int result;        for(int i = 0; i < postFix.length(); i++)        {            char m = postFix.charAt(i);            if(Character.isDigit(m))            {                stack2.push(m);            }            else            {               ...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The 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...

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