How can I get the index of a specific node in a doubly linked list?

Nutnicha

I have a class called SLList whuch accepts a doubly linked list SLList of any generic type as an input.

I've decided to use a sentinel approach to aid in my task.

I am now trying to convert my doubly linked list into a string using function public String makestring() and also get the index of a specific node via function public T findindexof(int i)

Here is my code so far:

public class SLList<T>{ 
        private class IntNode {
        private T data;
        private IntNode previous;
        private IntNode next;

        public IntNode (T data, IntNode previous, IntNode next) {
            this.data = data;
            this.previous = previous;
            this.next = next;
        }

        public IntNode () { 
            next = previous = this;
        }
    }

    IntNode sentinel; 
    private int length = 0;

    public SLList(){
        sentinel = new IntNode(); // Self referencing previous/next
    }

    
    public String makestring() {
        return sentinel.data.toString();
    }
    public T findindexof(int i) {
        return sentinel.data[i];
    }

My findindexof(int i) must only get the item at the given index, where 0 is the front, 1 is the next item and so forth. If no such item exists, returns null. It must not alter the deque either.

I am struggling to understand how to implement this function, so any advice would be greatly appreciated.

Thank you

Unmitigated

You can loop for i iterations, moving between nodes through the next field each time.

public T findindexof(int i) {
    IntNode curr = sentinel;
    for (int j = 0; j < i && curr != null; j++)
        curr = curr.next;
    return curr != null ? curr.data : null;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I add a node at the beginning of a doubly linked list in-between two dummy nodes?

How can I remove the first and last Node from doubly linked list?

How can I write a heapsort for a doubly linked list? c++

How can I print a doubly-linked list?

How do I get insertion sort to work with a doubly linked list

How to append a node in a doubly linked list in Java?

How do I filter a doubly linked list?

How do I insert a new node before first node of a doubly-linked list?

How could I make this singly linked list into a doubly linked list?

Deleting a node in a doubly linked list

Can someone please explain how can I remove first and last nodes in my doubly linked list?

How can I return the odd indexed nodes of a singly linked list in a new singly linked list ?Assume index of the first node as 1

How to delete a specific value from a doubly linked list?

Convert Binary Tree to doubly linked list. How can I avoid using global variable here?

How can I initialize a Doubly Linked List and then add the first element in java?

How can I find if a number in a doubly linked list is between max and min?

how to delete node from doubly-linked list

How to search name and delete node in doubly linked list in C?

How add node before doubly linked list c++

How to add a new node at the start of a doubly linked list?

How to sort a doubly linked list with the nodes instead of the node's values

Recursively Add Node to Linked List at Specific Index

Linked List: How to Sort Doubly Linked List?

How can I get an element from linked list with given index in java?

How do I delete a specific node in a linked list

Doubly Linked List C, insertion at specific position

Node deletion in a doubly-linked list

search for a node at a given position doubly linked list

python doubly linked list - insertAfter node