liststack.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 51 行

JAVA
51
字号
package stacks;

import dslib.base.DispenserUos;
import dslib.list.LinkedSimpleListUos;
import dslib.exception.*;

/**	A Dispenser for items of type Object with the last in, first out 
	structure typical of a stack. item() refers to the top of the stack and
	can be deleted. It is implemented by inheriting LinkedSimpleListUos. */
public class ListStack extends LinkedSimpleListUos implements DispenserUos
{
	/**	Create a new ListStack object. 
		Analysis: Time = O(1) */
	public ListStack() {}

	/**	Insert x onto the top of this stack.
		Analysis: Time = O(1) */
	public void insert(Object x)
	{
		insertFirst(x);
	}

	/**	The item at the top of the stack.
		Analysis: Time = O(1) 
		PRECONDITION: 
			!isEmpty() */
	public Object item() 
	{
		return firstItem();
	}

	/**	Is there a current item?
		Analysis: Time = O(1) */
	public boolean itemExists()
	{
		return !isEmpty();
	}

	/**	Delete the item on the top of this stack.
		Analysis: Time = O(1) 
		PRECONDITION:
			!isEmpty() */
	public void deleteItem() throws ContainerEmptyUosException
	{
		if (isEmpty())
			throw new ContainerEmptyUosException("There is no current item to delete");

		deleteFirst();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?