2 Star 0 Fork 0

CS-IMIS-23/zc20172324

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docs
lx
src
HttpSocket
chap10
chap11
chap12
chap15
chap3
chap4
chap5
chap6
ArrayList.java
ArrayListTest.java
Course.java
ElementNotFoundException.java
Josephus.java
LinkedList.java
LinkedListTest.java
ListADT.java
NonComparableElementException.java
OrderedArray.java
OrderedList.java
OrderedListADT.java
POSTester.java
POSTester2.java
Product.java
ProductList.java
ProductListTest.java
ProgramOfStudy.java
ProgramOfStudy2.java
UnorderedArray.java
UnorderedList.java
UnorderedListADT.java
chap9
cn/edu/besti/cs172324
database
exp
exp3
exp5
week1
week10
week11
week12
week13
week2
week3
week4
week5
week6
week7
week8
week9
实验1
实验2
实验3

.lin2.java.swp
.gitignore
LICENSE
README.md
run.sh
statistics.sh
克隆/下载
LinkedList.java 8.19 KB
一键复制 编辑 原始数据 按行查看 历史
zc20172324 提交于 7年前 . 代码6.9
package chap6;
import chap3.EmptyCollectionException;
import chap4.LinearNode;
import java.util.*;
/**
* LinkedList represents a linked implementation of a list.
*
* @author Lewis and Chase
* @version 4.0
*/
public abstract class LinkedList<T> implements ListADT<T>, Iterable<T>
{
protected int count;
protected LinearNode<T> head, tail;
protected int modCount;
/**
* Creates an empty list.
*/
public LinkedList()
{
count = 0;
head = tail = null;
modCount = 0;
}
/**
* Removes the first element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return a reference to the first element of this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("LinkedList");
}
LinearNode<T> node = head;
T result = node.getElement();
head = head.getNext();
count--;
return result;
}
/**
* Removes the last element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeLast() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("LinkedList");
}
LinearNode<T> node = head;
T result = node.getElement();
while (node.getNext() != tail){
node = node.getNext();
}
tail = node;
count--;
return result;
}
/**
* Removes the first instance of the specified element from this
* list and returns a reference to it. Throws an EmptyCollectionException
* if the list is empty. Throws a ElementNotFoundException if the
* specified element is not found in the list.
*
* @param targetElement the element to be removed from the list
* @return a reference to the removed element
* @throws EmptyCollectionException if the list is empty
* @throws ElementNotFoundException if the target element is not found
*/
public T remove(T targetElement) throws EmptyCollectionException,
ElementNotFoundException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
boolean found = false;
LinearNode<T> previous = null;
LinearNode<T> current = head;
while (current != null && !found)
if (targetElement.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new ElementNotFoundException("LinkedList");
if (size() == 1) // only one element in the list
head = tail = null;
else if (current.equals(head)) // target is at the head
head = current.getNext();
else if (current.equals(tail)) // target is at the tail
{
tail = previous;
tail.setNext(null);
}
else // target is in the middle
previous.setNext(current.getNext());
count--;
modCount++;
return current.getElement();
}
/**
* Returns the first element in this list without removing it.
*
* @return the first element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
return head.getElement();
}
/**
* Returns the last element in this list without removing it.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T last() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("LinkedList");
}
LinearNode<T> temp = head;
while(temp.getNext()!= null) {
temp = temp.getNext();
}
tail = temp;
return tail.getElement();
}
/**
* Returns true if the specified element is found in this list and
* false otherwise. Throws an EmptyCollectionException if the list
* is empty.
*
* @param targetElement the element that is sought in the list
* @return true if the element is found in this list
* @throws EmptyCollectionException if the list is empty
*/
public boolean contains(T targetElement) throws
EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
boolean search = false;
LinearNode current = new LinearNode();
while (current.getNext() != null) {
current = current.getNext();
}
if (current == targetElement)
return search;
else
return !search;
}
/**
* Returns true if this list is empty and false otherwise.
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
if(size()==0)
return true;
else
return false;
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in the list
*/
public int size()
{
return count;
}
/**
* Returns a string representation of this list.
*
* @return a string representation of the list
*/
public String toString()
{
String result = "";
LinearNode node = head;
for(int a=0;a<count;a++)
{
result += node.getElement()+" ";
node = node.getNext();
}
return result;
}
/**
* Returns an iterator for the elements in this list.
*
* @return an iterator over the elements of the list
*/
public Iterator<T> iterator()
{
return new LinkedListIterator();
}
/**
* LinkedIterator represents an iterator for a linked list of linear nodes.
*/
private class LinkedListIterator implements Iterator<T>
{
private int iteratorModCount; // the number of elements in the collection
private LinearNode<T> current; // the current position
/**
* Sets up this iterator using the specified items.
*/
public LinkedListIterator()
{
current = head;
iteratorModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = current.getElement();
current = current.getNext();
return result;
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/zc20172324.git
git@gitee.com:CS-IMIS-23/zc20172324.git
CS-IMIS-23
zc20172324
zc20172324
master

搜索帮助