/**
* All of the operations perform as could be expected for a doubly-linked
* list. Operations that index into the list will traverse the list from
* the beginning or the end, whichever is closer to the specified index.<p>
*
* <p><strong>Note that this implementation is not synchronized.</strong>
* If multiple threads access a linked list concurrently, and at least
* one of the threads modifies the list structurally, it <i>must</i> be
* synchronized externally. (A structural modification is any operation
* that adds or deletes one or more elements; merely setting the value of
* an element is not a structural modification.) This is typically
* accomplished by synchronizing on some object that naturally
* encapsulates the list.
*/
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
private transient Entry<E> header = new Entry<E>(null, null, null);
private transient int size = 0;
/**
* Constructs an empty list.
*/
public LinkedList() {
header.next = header.previous = header;
}
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
addBefore(e, header);
return true;
}
private Entry<E> addBefore(E e, Entry<E> entry) {
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
newEntry.previous.next = newEntry;
newEntry.next.previous = newEntry;
size++;
modCount++;
return newEntry;
}
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public boolean remove(Object o) {
if (o==null) {
for (Entry<E> e = header.next; e != header; e = e.next) {
if (e.element==null) {
remove(e);
return true;
}
}
} else {
for (Entry<E> e = header.next; e != header; e = e.next) {
if (o.equals(e.element)) {
remove(e);
return true;
}
}
}
return false;
}
private E remove(Entry<E> e) {
if (e == header)
throw new NoSuchElementException();
E result = e.element;
e.previous.next = e.next;
e.next.previous = e.previous;
e.next = e.previous = null;
e.element = null;
size--;
modCount++;
return result;
}
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
return entry(index).element;
}
/**
* Returns the indexed entry.
*/
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}
private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;
Entry(E element, Entry<E> next, Entry<E> previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
分享到:
相关推荐
List-LinkedList 单链表就地反转 的代码的一种实现。
和数组一样,LinkedList 也是一种线性数据结构,但它不像数组一样在连续的位置上存储元素,而是通过引用相互链接。 LinkedList 中的每一个元素都可以称之为节点(Node),每一个节点都包含三个项目:其一是元素本身...
使用LinkedList模拟堆栈操作,包括进栈、出栈,入队、出队
ArrayList、LinkedList、Vector区别简介。
LinkedList<Integer> ab=new LinkedList(); for(int i=0;i;i++) ab.add(i+1); for(;;){ if(ab.size()==0)break;//结束条件 int i=1; while(i!=M){ ab.add(ab.remove());//如果没有报到M这个数字的人将其从...
Java 中Linkedlist类的源代码Java 中Linkedlist类的源代码Java 中Linkedlist类的源代码Java 中Linkedlist类的源代码
java LinkedList的添加删除操作 java LinkedList的添加删除操作
非常简单的Java LinkedList 应用实例
LinkedList<String> list = new LinkedList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); /* * public void addFirst(E e)方法 * 将指定元素插入此列表的开头 */ list.addFirst("000...
使用LinkedList类编写程序,用某种集合接口的实现类作存储,实现具有自定义排序功能的包含姓名、年龄、身高、职称等内容的人事信息输入和打印。
LinkedList 是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。 LinkedList 实现 List 接口,能对它进行队列操作。 LinkedList 实现 Deque 接口,即能将...
数据结构,LinkedList在c++的实现,一共有三个头文件:Link.h 、List.h 、LinkedList。
1.List是接口类,ArrayList和LinkedList是List的实现类 2.ArrayList是动态数组(顺序表)的数据结构 3.LinkedList
阶段练习:实现链表(LinkedList) 简介:写一个链表的数据结构,要求实现IList接口。 具体要求: 1、 使用代码规范。 2、 至少对IList中的Add,Remove,Insert,Indexer,IEnumerator进行单元测试。 3、 对上述每个...
链表类LinkedList的完全c++实现,根据数据结构与算法课堂总结。
比较ArrayList,LinkedList,Vector三者随机读取,插入,删除性能。
java中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.doc
测试ArrayList和LinkedList的add方法
LinkedLIst.cpp
LinkedList的使用.pdf