Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

generics - How to write an add/insert method with a DoublyLinkedList in Java

I need help with my add method in java. It works with DoublyLinked List.

I am implementing a cyclic DoublyLinkedList data structure. Like a singly linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node. Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.

What the method is suppose to do is insert the value parameter at the specified index in the list. Be sure to address the case in which the list is empty and/or the added element is the first in the list. If the index parameter is invalid, an IndexOutOfBoundsException should be thrown.

Here is my code below:

public class DoublyLinkedList<E>
{
private Node first;
private int size;

@SuppressWarnings("unchecked")
public void add(E value)
{

    if (first == null)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
        {
        first.prev.next = new Node(value, first, first.prev);
        first.prev = first.prev.next;
    }
    size++;
}
private class Node<E>
{
    private E data;
    private Node next;
    private Node prev;

    public Node(E data, Node next, Node prev)
    {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
}

Here is the method where it fails. I will comment the line where I'm stuck on, but other than that, what is done in the previous lines is correct from what I heard.

@SuppressWarnings("unchecked")
public void add(int index, E value)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first.data == null)
    {
        throw new NullPointerException();
    }
    if (index == 0)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
        current.prev.next = new Node(value, current, current.prev); // This is the line where I get lost on. 
        current.prev = current.prev.next;
    }
    size++;
}

The rest of my code is here. Please focus on the method I'm working on. thank you!

@SuppressWarnings("unchecked")
public void remove(int index)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first.data == null)
    {
        throw new IndexOutOfBoundsException();
    }
    else if (index == 0)
    {
        first = first.next;
    }
    else
        {
            Node current = first.next;
            for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
           // current.prev = current.next;
            current.next = current.next.next;
    }
    size--;
}
@SuppressWarnings("unchecked")
public E get(int index)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    Node current = first;
    for (int i = 0; i < index; i++)
    {
        current = current.next;
    }
    return (E) current.data;
}
@SuppressWarnings("unchecked")
public int indexOf(E value)
{
    int index = 0;
    Node current = first;
    while (current != current.next)
    {
        if (current.data.equals(value))
        {
            return index;
        }
        index++;
        current = current.next;
    }
    return index;
}
public boolean isEmpty()
{
    if (size == 0)
    {
        return true;
    }
    else
        {
        return false;
    }
}
public int size()
{
    return size;
}
@SuppressWarnings("unchecked")
public String toString()
{
    if (isEmpty())
    {
        return "[]";
    }
    else
        {
            String result = "[" + first.data;
            Node current = first.next;
        for(int i = 0; i < size-1; i++)
        {
            result += ", " + current.data;
            current = current.next;
        }
        result += "]";
        return result;
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was not easy at all, however I figured out the answer to my question.

 @SuppressWarnings("unchecked")
 public void add(int index, E value)
 {
    if(index > size || index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if (first == null)
    {
        Node n = new Node(value, null, null);
        n.next = n;
        n.prev = n;
        first = n;
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index; i++)
        {
            current = current.next;
        }
        //current points to node that will follow new node.
        Node n2 = new Node(value, current, current.prev);
        current.prev.next = n2;
        current.prev = n2;
        //update first if necessary.
        if(index == 0)
        {
            first = n2;
        }
    }
    size++;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...