数据结构与算法分析(Java语言描述)学习--第四天
第3章 表、栈和队列
ArrayList类的实现
1、保持基础数组,数组的容量,以及存储在ArrayList中的当前项数。
2、改变基础数组的容量。
3、提供get和set的实现
4、提供size、isEmpty和clear,remove和两种版本的add。
5、提供实现Iterator接口的类。这个类提供next、hasNext和remove。
public class MyArrayList<AnyType> implements Iterable<AnyType>
{
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType[] theItems;
public MyArrayList()
{
doClear();
}
public void clear()
{
doClear();
}
private void doClear()
{
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size()
{
return theSize;
}
public boolean isEmpty()
{
return size() == 0;
}
public void trimToSize()
{
ensureCapacity(size());
}
public AnyType get(int idx)
{
if(idx < 0 || idx >= size())
throws new ArrayIndexOutOfBoundException();
return theItems[idx];
}
public AnyType set(int idx, AnyType newVal)
{
if(idx < 0 || idx >= size())
throws new ArrayIndexOutOfBoundException();
AnyType old = theIems[idx];
theItems[idx] = newVal;
return old;
}
public void ensureCapacity(int newCapacity)
{
if(newCapacity < theSize)
return;
AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapacity];
for(int i = 0; i < size(); i++)
{
theItems[i] = old[i];
}
}
public boolean add(AnyType x)
{
add(size(), x);
return true;
}
public void add(int idx, AnyType x)
{
if(theItems.length == size())
ensureCapacity(size() * 2 + 1);
for(int i = theSize; i > idx; i--)
theItems[i] = theItems[i - 1];
theItems[idx] = x;
theSize++;
}
public AnyType remove(int idx)
{
AnyType removedItem = theItems[idx];
for(int i = idx; i < size(); i++)
theItems[i] = theItems[i + 1];
theSize--;
return removedItem;
}
public java.util.Iterator<AnyType> iterator()
{
return new ArrayListIterator();
}
private class ArrayListIterator implements java.util.Iterator<AnyType>
{
private int current = 0;
public boolean hasNext()
{
return current < size();
}
public AnyType next()
{
if(!hasNext())
throw new java.util.NoSuchElementException();
return theItems[current++];
}
public void remove()
{
MyArrayList.this.remove(--current);
}
}
}
基本类
迭代器、Java嵌套类和内部类
LinkedList类的实现
双链表,保留到该表两端的引用。
1、MyLinkedList类本身,包含到两端的链、表的大小以及一些方法。
2、Node类,包含数据以及到前一个节点的链和到下一个节点的链,还有适当的构造方法。
3、LinkedListIterator类,实现接口Iterator,提供next、hasNext和remove。
在表的终端和前端创建额外的节点,额外的节点叫做标记节点。头节点,尾节点。
public class MyLinkedList<AnyType> implements Iterable<AnyType>
{
private static class Node<AnyType>
{
public Node(AnyType d, Node<Anytype> p, Node<AnyType> n)
{
data = d;
prev = p;
next = n;
}
public AnyType data;
public Node<AnyType> prev;
public Node<AnyType> next;
}
public MyLinkedList()
{
doClear();
}
public void clear()
{
doClear();
}
private void doClear()
{
beginMarker = new Node<AnyType>(null, null, null);
endMarker = new Node<AnyType>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
public int size()
{
return theSize;
}
public boolean isEmpty()
{
return size() == 0;
}
public boolean add(AnyType x)
{
add(size(), x);
return true;
}
public void add(int idx, AnyType x)
{
addBefore(getNode(idx, 0, size()), x);
}
public AnyType get(int idx)
{
return getNode(idx).data;
}
public AnyType set(int idx, AnyType newVal)
{
Node<AnyType> p = getNode(idx);
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
}
public AnyType remove(int idx)
{
return remove(getNode(idx));
}
private void addBefore(Node<AnyType> p, AnyType x)
{
Node<AnyType> newNode = new Node<>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}
private AnyType remove(Node<AnyType> p)
{
p.next.prev = p.prev;
p.prev.ext = p.next;
theSize--:
modCount++;
return p.data;
}
private Node<AnyType> getNode(int idx)
{
return getNode(idx, 0, size() - 1);
}
private Node<AnyType> getNode(int idx, int lower, int upper)
{
Node<AnyType> p;
if(idx < lower || idx > upper)
throw new IndexOutOfBoundsException();
if(idx < size() / 2)
{
p = beginMarker.next;
for(int i = 0; i < idx; i++)
p = p.next;
}
else
{
p= endMarker;
for(int i = size(); i > idx; i--)
p = p.prev;
}
return p;
}
public java.util.Iterator<AnyType> iterator()
{
return new LinkedListIterator();
}
private class LinkedListIterator implements java.util.Iterator<AnyType>
{
private Node<AnyType> current = beginMarker.next;
private int expectedModCount = modeCount;
private boolean okToRemove = false;
public boolean hasNext()
{
return current != endMarker;
}
public AnyType next()
{
if(modCount != expectedModCount)
throw new java.util.ConcurrentModificationException();
if(!hasNext())
throw new java.util.NoSuchElementException();
AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}
public void remove()
{
if(modCount != expectedModCount)
throw new java.util.ConcurrentModificationException();
if(!hasNext())
throw new java.util.NoSuchElementException();
MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove = false;
}
}
private int theSize;
private int modCount = 0;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;
}