0

Trying to insert an object at a specified index in a linked list java class. Not too sure how too implement this though.

Here is an example of the parameters for the method and what i have so far(doesn't work):

  void insertAtIndex(int idx, Shape data){
    if (idx == 0) {
        //insert the new Shape at the beginning of the list
        insertAtBeginning(data);
    }else{

        Node temp = head;
        for(int i = 0; i < idx - 1; i++)
            temp = temp.getNext();
        Node next = new Node(data);
        next = temp.getNext();
        temp = next;

    }
}

subclass for Node:

public Node(Shape data){
    //Make next point to null
    next = null;
    this.data = data;
}

// another Node constructor if we want to specify the node to point to.
public Node(Shape dataVal, Node nextVal){
    next = nextVal;
    data = dataVal;
}

//Getter for data
public Shape getData(){
    return data;
}

//Setter for data
public void setData(Shape data){
    this.data = data;
}

//Getter for next
public Node getNext() {
    return next;
}

//Setter for next
public void setNext(Node next) {
    this.next = next;
}

linked list class:

public class ShapeLinkedList {

public Node head; //head is first node in linked list
public Node tail; //tail is last node in linked list

public ShapeLinkedList(){}

public ShapeLinkedList(Node head){
    head = null;
    tail = null;
}

public boolean isEmpty(){
    return length() == 0;
}
2

2 Answers 2

2

Just use LinkedList#add(int index, E element):

public void add(int index, E element)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Sign up to request clarification or add additional context in comments.

Comments

0

Nice start :D

Now you need to look at it like this...

a -> b -> c -> d

If you want to add at index 0...

e -> a -> ... right?

If you index it at 1, then...

a-> e -> b -> c -> d

In otherwords, the item AT that index points to the new shape, and the new shape points to the next item that used to be in index 2, (but AFTER it will be index 3)

5 Comments

How would this look in pseudo code for the params i got?
I would need to see more to be able to help more... However I feel like your node class has a previous and a next, right? If that is the case, you should set the item at index 1 to point to new shape, the new item previous to point to item at index 1, and the new item next is item at index 3, while index 3 previous is the new item.
Close! Your for loop is a smidgen off. You do not want to subtract 1.
Not too sure what you mean
public void insertAtEnd(Shape data){ Node temp = new Node(data); tail.next = temp; tail = temp; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.