Java linkedlist实现添加方法
我正在实现一个在线课程的链表,我的add函数似乎有问题。当我尝试添加第一个元素时,Eclipse打印null,对于第二个元素,Eclipse显示错误。(我假设第一个元素从未添加过,所以不可能有第二个。) 这是我的链接列表的实现:Java linkedlist实现添加方法,java,Java,我正在实现一个在线课程的链表,我的add函数似乎有问题。当我尝试添加第一个元素时,Eclipse打印null,对于第二个元素,Eclipse显示错误。(我假设第一个元素从未添加过,所以不可能有第二个。) 这是我的链接列表的实现: package textgen; import java.util.AbstractList; public class MyLinkedList<E> extends AbstractList<E> {
package textgen;
import java.util.AbstractList;
public class MyLinkedList<E> extends AbstractList<E> {
LLNode<E> head;
LLNode<E> tail;
int size;
/** Create a new empty LinkedList */
public MyLinkedList() {
size = 0;
head = new LLNode<E>();
tail = new LLNode<E>();
head.next = tail;
tail.prev = head;
}
/**
* Appends an element to the end of the list
* @param element The element to add
*/
public boolean add(E element )
{
add(size, element);
return false;
}
/** Get the element at position index
* @throws IndexOutOfBoundsException if the index is out of bounds. */
public E get(int index) throws IndexOutOfBoundsException
{
if(index >= this.size){
throw new IndexOutOfBoundsException("Your index is out of bounds!");
}
LLNode<E> lGet = head;
for(int i = 0; i < index + 1; i++){
lGet = lGet.next;
}
return lGet.data;
}
public void printList(){
LLNode lTemp = head;
while(lTemp.next != tail){
System.out.println(lTemp.next.data);
lTemp = lTemp.next;
}
}
/**
* Add an element to the list at the specified index
* @param The index where the element should be added
* @param element The element to add
*/
public void add(int index, E element ) throws IndexOutOfBoundsException
{
if(index > this.size){
throw new IndexOutOfBoundsException("Oops! Out of bounds!");
}
else{
LLNode<E> nAdd = new LLNode<E>(element);
LLNode<E> nIt = null;
if(index <= size/2) // if the index is closer to the start from the beginning of the list
{
nIt = head;
for(int i = 0; i < index + 1; i++){
nIt = nIt.next;
}
}
else {
nIt = tail;
for(int i = this.size; i > index; i--){
nIt = nIt.prev;
}
}
nIt.prev.next.prev = nAdd;
nAdd.next = nIt.prev.next;
nIt.prev.next = nAdd;
nAdd.prev = nIt.prev;
size++;
}
}
/** Return the size of the list */
public int size()
{
return size;
}
/** Remove a node at the specified index and return its data element.
* @param index The index of the element to remove
* @return The data element removed
* @throws IndexOutOfBoundsException If index is outside the bounds of the list
*
*/
public E remove(int index)
{
// TODO: Implement this method
size--;
return null;
}
/**
* Set an index position in the list to a new element
* @param index The index of the element to change
* @param element The new element
* @return The element that was replaced
* @throws IndexOutOfBoundsException if the index is out of bounds.
*/
public E set(int index, E element)
{
// TODO: Implement this method
return null;
}
}
class LLNode<E>
{
LLNode<E> prev;
LLNode<E> next;
E data;
public LLNode(){
this.data = null;
this.prev = null;
this.next = null;
}
public LLNode(E e)
{
this.data = e;
this.prev = null;
this.next = null;
}
}
This is the main:
package textgen;
导入java.util.AbstractList;
公共类MyLinkedList扩展了AbstractList{
淋巴结头;
淋巴结尾;
整数大小;
/**创建一个新的空LinkedList*/
公共MyLinkedList(){
尺寸=0;
head=新的LLNode();
tail=新的LLNode();
head.next=tail;
tail.prev=头部;
}
/**
*将元素追加到列表的末尾
*@param element要添加的元素
*/
公共布尔加法(E元素)
{
添加(大小、元素);
返回false;
}
/**在位置索引处获取元素
*@如果索引超出范围,则抛出IndexOutOfBoundsException*/
public E get(int index)抛出IndexOutOfBoundsException
{
如果(索引>=此.size){
抛出新的IndexOutOfBoundsException(“您的索引超出范围!”);
}
LLNode lGet=头部;
对于(int i=0;ithis.size){
抛出新的IndexOutOfBoundsException(“哎呀!出界了!”);
}
否则{
LLNode nAdd=新的LLNode(元素);
LLNode nIt=null;
if(索引;i--){
nIt=nIt.prev;
}
}
nIt.prev.next.prev=nAdd;
nAdd.next=nIt.prev.next;
nIt.prev.next=nAdd;
nAdd.prev=nIt.prev;
大小++;
}
}
/**返回列表的大小*/
公共整数大小()
{
返回大小;
}
/**删除指定索引处的节点并返回其数据元素。
*@param index要删除的元素的索引
*@返回已删除的数据元素
*@如果索引超出列表的范围,则引发IndexOutOfBoundsException
*
*/
公共E删除(int索引)
{
//TODO:实现此方法
大小--;
返回null;
}
/**
*将列表中的索引位置设置为新元素
*@param index要更改的元素的索引
*@param元素新元素
*@返回被替换的元素
*@如果索引超出范围,则抛出IndexOutOfBoundsException。
*/
公共E集(整数索引,E元素)
{
//TODO:实现此方法
返回null;
}
}
类LLNode
{
LLNode-prev;
LLNode-next;
E数据;
公共LLNode(){
this.data=null;
this.prev=null;
this.next=null;
}
公共LLNode(E)
{
这个数据=e;
this.prev=null;
this.next=null;
}
}
这是主要问题:
包装textgen
public class fixAdd {
public static void main(String [] Arg){
MyLinkedList<String> ll = new MyLinkedList<String>();
ll.add(0, "happy");
ll.add(1, "gilda");
System.out.println(ll);
}
}
And this is the error printed:
Exception in thread "main" java.lang.NullPointerException
at textgen.MyLinkedList.get(MyLinkedList.java:57)
at java.util.AbstractList$Itr.next(Unknown Source)
at java.util.AbstractCollection.toString(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at textgen.fixAdd.main(fixAdd.java:11)
公共类fixAdd{
公共静态void main(字符串[]Arg){
MyLinkedList ll=新的MyLinkedList();
ll.加上(0,“快乐”);
11.添加(1,“吉尔达”);
系统输出打印项次(ll);
}
}
这是打印的错误:
线程“main”java.lang.NullPointerException中出现异常
获取(MyLinkedList.java:57)
位于java.util.AbstractList$Itr.next(未知源)
位于java.util.AbstractCollection.toString(未知源)
位于java.lang.String.valueOf(未知源)
位于java.io.PrintStream.println(未知源)
位于textgen.fixAdd.main(fixAdd.java:11)
我已经检查了我的add方法很多次,并将其与我在网上找到的其他实现进行了比较,一切似乎都正常。我完全糊涂了,希望能得到任何帮助。谢谢 请尝试以下代码:
public void insert(int index, T item)
{
if(index == size())
{
add(item);
}
else if(index == 0)
{
MyNode<T> temp = new MyNode<T>();
temp.data = item;
temp.next = head;
head.previous = temp;
head = temp;
count++;
}
temp = head;
for(int i = 0; i < index-1; i++)
{
temp = temp.next;
MyNode<T> myNode = new MyNode<T>();
myNode.data = item;
myNode.next = temp.next;
temp.next = myNode;
count++;
}
}
public void insert(整数索引,T项)
{
如果(索引==size())
{
增加(项目);
}
else if(索引==0)
{
MyNode temp=新的MyNode();
温度数据=项目;
下一个温度=压头;
head.previous=温度;
压头=温度;
计数++;
}
温度=水头;
对于(int i=0;i
此实施存在多个问题:
get()
方法(通过调用AbstractList.toString()
)add(E元素)
中,您正在调用add(size,element)
将size
作为索引传递,然后检查if(index>this.size)
哪个是pr
nIt.prev.next.prev = nAdd;
nAdd.next = nIt.prev.next;
nIt.prev.next = nAdd;
nAdd.prev = nIt.prev;
public void add(int index, E element)
{
if (index > size)
{
throw new IndexOutOfBoundsException("Oops! Out of bounds!");
}
LLNode<E> node = new LLNode<E>(element);
//Add first and last
if(size == 0)
{
head = tail = node;
}
else
{
//Add first
if(index == 0)
{
node.next = head;
head.prev = node;
head = node;
}
//Add last
else if(index == size)
{
node.prev = tail;
tail.next = node;
tail = node;
}
//Add between
else
{
LLNode<E> current = this.head;
for(int i = 0; i < index; i++)
{
current = current.next;
}
node.next = current;
node.prev = current.prev;
current.prev.next = node;
}
}
size++;
}