将LinkedList类更改为类似于java.util.ArrayList类的通用集合
这是一个将LinkedList类更改为类似于java.util.ArrayList类的通用集合,java,linked-list,generic-collections,Java,Linked List,Generic Collections,这是一个链接列表,具有修改列表的方法。我必须做的一件事是: 将LinkedList类更改为类似于java.util.ArrayList类的通用集合 我不知道怎么做 import java.util.NoSuchElementException; /** A linked list is a sequence of nodes with efficient element insertion and removal. This class contains a subset of the me
链接列表
,具有修改列表的方法。我必须做的一件事是:
将LinkedList类更改为类似于java.util.ArrayList
类的通用集合
我不知道怎么做
import java.util.NoSuchElementException;
/**
A linked list is a sequence of nodes with efficient
element insertion and removal. This class
contains a subset of the methods of the standard
java.util.LinkedList class.
*/
public class LinkedList<E>
{
int currentSize =0;
private Node head;
private Node tail;
/**
Constructs an empty linked list.
*/
public LinkedList()
{
head = null;
tail = null;
}
/**
Adds an element to the front of the linked list.
@param element the element to add
*/
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = head;
currentSize++;
head = newNode;
tail = head;
while( tail.next != null)
{
tail = tail.next;
}
}
/**
Removes the head element in the linked list.
@return the removed element
*/
public Object removeFirst()
{
if (head == null)
throw new NoSuchElementException();
Object temp = head.data;
head = head.next;
currentSize--;
return temp;
}
/**
Returns the head element in the linked list.
@return the head element in the linked list
*/
public Object getFirst()
{
if (head == null)
throw new NoSuchElementException();
return head.data;
}
/**
Returns the element at a given position in the linked list.
@param index the element position
@return the element at the given position
*/
Object get(int index)
{
if (index < 0)
throw new NoSuchElementException();
Node current = head;
int i = 0;
while (current != null && i < index)
{
current = current.next;
i++;
}
if (current == null)
throw new NoSuchElementException();
return current.data;
}
/**
Computes the size of the linked list
@return the number of elements in the list
*/
public int size()
{
//to be completed for lab 7.1 #2
return currentSize; // rewrite this, just makes it compile
}
/**
Reverses all elements in a linked list.
*/
public void reverse()
{
//to be completed for lab 7.1 #1
if(head == null)
throw new NoSuchElementException();
tail = head;
Node current = head;
Node nextt= head.next;
Node nul= null;
while(nextt!= null)
{
current.next = nul;
nul = current;
current = nextt;
nextt = nextt.next;
}
head = current;
head.next = nul;
}
/**
Adds an element to the end of the linked list.
@param element the element to add
*/
public void add(Object element)
{
if(head==null)
addFirst(element);
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
tail.next = newNode;
tail = newNode;
currentSize++;
}
//to be completed for lab 7.1 #3
}
/**
Returns an iterator for iterating through this list.
Allows the use of the iterator outside of this class.
@return an iterator for iterating through this list
*/
public ListIterator listIterator()
{
return new LinkedListIterator();
}
/**
Returns a string representation of this list in brackets
separated by commas.
@return a string of list elements.
*/
public String toString()
{
StringBuilder temp = new StringBuilder();
ListIterator it = listIterator();
while(it.hasNext()){
temp.append(it.next());
if(it.hasNext())
temp.append(", ");
}
return temp.toString();
}
private static class Node
{
private Object data;
private Node next;
}
private class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
/**
Constructs an iterator that points to the front
of the linked list.
*/
public LinkedListIterator()
{
position = null;
previous = null;
}
/**
Moves the iterator past the next element.
@return the traversed element
*/
public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position; // Remember for remove
if (position == null)
position = head;
else
position = position.next;
return position.data;
}
/**
Tests if there is an element after the iterator
position.
@return true if there is an element after the iterator
position
*/
public boolean hasNext()
{
if (position == null)
return head != null;
else
return position.next != null;
}
/**
Adds an element before the iterator position
and moves the iterator past the inserted element.
@param element the element to add
*/
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = head;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
previous = position;
tail=head;
while( tail.next != null)
{
tail = tail.next;
}
currentSize++;
}
}
/**
Removes the last traversed element. This method may
only be called after a call to the next() method.
*/
public void remove()
{
if (previous == position)
throw new IllegalStateException();
if (position == head)
{
removeFirst();
}
else
{
previous.next = position.next;
tail=head;
while(tail.next != null)
{
tail = tail.next;
}
currentSize--;
}
position = previous;
}
/**
Sets the last traversed element to a different
data.
@param element the element to set
*/
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
}
}
import java.util.NoSuchElementException;
/**
链表是一系列具有高效链接的节点
元素的插入和删除。这个班
包含标准方法的子集
java.util.LinkedList类。
*/
公共类链接列表
{
int currentSize=0;
专用节点头;
私有节点尾部;
/**
构造一个空的链表。
*/
公共链接列表()
{
head=null;
tail=null;
}
/**
将元素添加到链接列表的前面。
@param元素要添加的元素
*/
公共void addFirst(对象元素)
{
Node newNode=新节点();
newNode.data=元素;
newNode.next=头部;
currentSize++;
头=新节点;
尾=头;
while(tail.next!=null)
{
tail=tail.next;
}
}
/**
删除链接列表中的head元素。
@返回已删除的元素
*/
公共对象removeFirst()
{
if(head==null)
抛出新的NoTouchElementException();
对象温度=头数据;
head=head.next;
当前大小--;
返回温度;
}
/**
返回链接列表中的head元素。
@返回链接列表中的head元素
*/
公共对象getFirst()
{
if(head==null)
抛出新的NoTouchElementException();
返回头数据;
}
/**
返回链表中给定位置的元素。
@参数索引元素位置
@将元素返回到给定位置
*/
对象获取(int索引)
{
如果(指数<0)
抛出新的NoTouchElementException();
节点电流=头;
int i=0;
while(当前!=null&&i<索引)
{
当前=当前。下一步;
i++;
}
如果(当前==null)
抛出新的NoTouchElementException();
返回当前数据;
}
/**
计算链接列表的大小
@返回列表中的元素数
*/
公共整数大小()
{
//将为实验7.1#2完成
return currentSize;//重写它,使其可编译
}
/**
反转链接列表中的所有元素。
*/
公共无效反向()
{
//实验7.1#1需完成
if(head==null)
抛出新的NoTouchElementException();
尾=头;
节点电流=头;
Node nextt=head.next;
Node nul=null;
while(nextt!=null)
{
current.next=nul;
nul=电流;
电流=nextt;
nextt=nextt.next;
}
水头=电流;
head.next=nul;
}
/**
将元素添加到链接列表的末尾。
@param元素要添加的元素
*/
公共void添加(对象元素)
{
if(head==null)
addFirst(元素);
其他的
{
Node newNode=新节点();
newNode.data=元素;
newNode.next=null;
tail.next=newNode;
tail=newNode;
currentSize++;
}
//将为实验7.1#3完成
}
/**
返回迭代此列表的迭代器。
允许在此类之外使用迭代器。
@返回迭代此列表的迭代器
*/
公共ListIterator ListIterator()
{
返回新的LinkedListIterator();
}
/**
返回此列表在括号中的字符串表示形式
用逗号分隔。
@返回列表元素的字符串。
*/
公共字符串toString()
{
StringBuilder temp=新的StringBuilder();
ListIterator it=ListIterator();
while(it.hasNext()){
temp.append(it.next());
if(it.hasNext())
临时附加(“,”);
}
返回温度toString();
}
私有静态类节点
{
私有对象数据;
私有节点下一步;
}
私有类LinkedListIterator实现ListIterator
{
私有节点位置;
前一个私有节点;
/**
构造一个指向前面的迭代器
链接列表的一部分。
*/
公共链接标识符()
{
位置=空;
previous=null;
}
/**
将迭代器移过下一个元素。
@返回被遍历的元素
*/
公共对象下一个()
{
如果(!hasNext())
抛出新的NoTouchElementException();
previous=position;//记住删除
如果(位置==null)
位置=头部;
其他的
位置=位置。下一步;
返回位置数据;
}
/**
测试迭代器后面是否有元素
位置。
@如果迭代器后面有元素,则返回true
位置
*/
公共布尔hasNext()
{
如果(位置==null)
返回头!=null;
其他的
返回位置.next!=null;
}
/**
在迭代器位置之前添加元素
并将迭代器移过插入的元素。
@param元素要添加的元素
*/
公共void添加(对象元素)
{
如果(位置==null)
{
addFirst(元素);
位置=头部;
}