java用linkedlist编写学生类,如何在Java中使用DoublyLinkedList编写添加/插入方法

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

{

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

{

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;

}

}

}

解决方案

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++;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值