Question

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 setNext(Node next) {
        this.next = next;
    }
    public Node getNext() {
        return next;
    }
}

*******************************************************************

package LinkedListLab;

public class LinkListLab {
    private Node top;
    public LinkListLab() {
        top = null;
    }
    /****************************************************************
     *
     * Determines the size, that is, the number of elements in the list
     *
     * @return the size of the list
     *
     ****************************************************************/
    public int getLen() {
        int size =0;
        Node current = top;
        while(current !=null) {
            current = current.getNext();
            size++;
        }
        return size;
        }

    /****************************************************************
     *
     * Inserts a node before a specific index. When the list is empty
     * that is, top = null, then the index must be 0. After the first
     * element is added, index must be: 0 <= index < size of list
     *
     * @param index a specific index into the list.
     *
     * @throws IllegalArgumentStringxception if index < 0 or
     * index >= size of the list
     ****************************************************************/
    public void insertBefore (int index, String data) {

// place your code here
    }
    /****************************************************************
     *
     * Inserts a node after a specific index. When the list is empty
     * that is, top = null, then the index must be 0. After the first
     * element is added, index must be: 0 <= index < size of list
     *
     * @param index a specific index into the list.
     *
     * @throws IllegalArgumentStringxception if index < 0 or
     * index >= size of the list
     ****************************************************************/
    public void insertAfter (int index, String data) {
// place your code here
    }
    /****************************************************************
     *
     * Removes the top element of the list
     *
     * @return returns the element that was removed.
     *
     * @throws RuntimeStringxception if top == null, that is,
     * there is no list.
     *
     ****************************************************************/
    public String removeTop () {
        return null;
    }
    /****************************************************************
     *
     * This Method removes a node at the specific index position. The
     * first node is index 0.
     *
     * @param index the position in the linked list that is to be
     * removed. The first position is zero.
     *
     * @throws IllegalArgumentStringxception if index < 0 or
     * index >= size of the list
     *
     ****************************************************************/
    public boolean delAt(int index) {
        return false;
    }
    // A simple testing program. Not complete but a good start.
    public static void main (String[] args){
        LinkListLab list = new LinkListLab();
        list.display();
        System.out.println ("Current length = " + list.getLen());
        list.insertBefore(0, "apple");
        list.insertBefore(0, "pear");
        list.insertBefore(1, "peach");
        list.insertBefore(1, "cherry");
        list.insertBefore(3, "donut");
        list.insertAfter(0, "apple");
        list.insertAfter(0, "pear");
        list.insertAfter(1, "peach");
        list.insertAfter(1, "cherry");
        list.insertAfter(3, "donut");
        list.display();
        list.removeTop();
        list.delAt(4);
        list.delAt(2);
        list.delAt(0);
        list.removeTop();
        list.removeTop();
        list.display();
    }
    public void display() {
        Node temp = top;
        System.out.println ("___________ List ________________________");
        while (temp != null) {
            System.out.println (temp.getData());
            temp = temp.getNext();
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public boolean delAt(int index) {
    if (index < 0 || index >= getLen())
        throw new IllegalArgumentException();
    if (index == 0) {
        removeTop();
    } else {
        Node temp = top;
        for (int i = 1; i < index; i++) {
            temp = temp.getNext();
        }
        temp.setNext(temp.getNext().getNext());
    }
    return true;
}
Add a comment
Know the answer?
Add Answer to:
Java - I need help creating a method that removes a node at the specific index...
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);...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

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

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

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

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

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