数据结构与算法分析(Java语言描述)学习--第四天

本文详细介绍如何使用Java实现ArrayList和LinkedList,包括基本操作如增删改查、迭代器的使用及内部类的设计。通过具体代码展示了如何高效管理和操作数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值