Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey, Wondering if anyone is offering tohelp… that would be great. For the follow

ID: 3614025 • Letter: H

Question

Hey,

Wondering if anyone is offering tohelp… that would be great. For the following I will give ageneral overview:

A program that generates a linked list ofrandomly generated Integer objects ranging from 0-99 and there is amenu option with certain linked list orders to execute. Oneof the requirements is a get(int index) function and it isbasically a find and return the found.

Singly linked list with dummy node

public Comparable get(int index)

{
}

I am not understanding how the layout willbe without having an item or data of some time to compare with.This is to get an item without removing it. I understand the logicperfectly fine with a parameter of Comparable item but with index Iam not sure of how to begin.

Also for an Add function with a specific index and data to place in single linked list with a dummynode i

Came up with this but I get an error forone of the lines --- Any suggestions:

public voidadd(int index, Comparable item)throws IndexOutOfBoundsException

      {

           if(index==0)

                       addFirst(item);

           else if (index == size)

                       add(item);

           else

           {

                 Node prev;

                 int i;

                 for(prev=head,i=0; i<index-1; i++, prev =prev.next)

                       ;

                 prev.next = new Node(item,prev.next);//errorline

                 size++;

           }//end else

      }//end ofadd(index,item)


for the addFirst - i am also facing a codeerror i am not seeing clearly and here is what i have:


    public voidaddFirst(Comparable item)
    {
        head = new Node ( item,head.next);
        if ( size == 0 )
           tail =head.next;
        size++;
    }//end of addFirst(item)


Explanation / Answer

Your addFirstmethod has an obvious issue: public voidaddFirst(Comparable item) { //the next item is head, not head.next head = newNode ( item,head); if(size == 0) { // tail must point to the new headthen tail = head; } size++; }//end ofaddFirst(item) I'm less sure about your add() method, but I think you may beincrementing prev one too many times by putting it in your for loopdeclaration. public void add(int index,Comparable item) throws IndexOutOfBoundsException { if(index==0) addFirst(item); else if (index == size) add(item); else { Node prev; int i; for(prev=head,i=0;i