linkednode.java

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

JAVA
60
字号
package simple;

/**	A basic linked node containing an item of type Object and a reference to the next node.
	There are methods for accessing and setting the item and next node, and a toString method
	that provides a string representation of the list of nodes starting with the current node. */
public class LinkedNode
{
	/**	The item of this node. */
	protected Object item;
	
	/**	The next node. */
	protected LinkedNode nextNode; 

	/**	Construct a node with item x.
		Analysis: Time = O(1) */
	public LinkedNode(Object x)
	{
		item = x;
		nextNode = null;
	}

	/**	The item of the node.
		Analysis: Time = O(1) */
	public Object item()
	{
		return item;
	}

	/**	Set the item to x.
		Analysis: Time = O(1) */
	public void setItem(Object x)
	{
		item = x;
	}

	/**	The next node.
		Analysis: Time = O(1) */
	public LinkedNode nextNode()
	{
		return nextNode;
	}
  
	/**	Set nextNode to x.
		Analysis: Time = O(1) */
	public void setNextNode(LinkedNode x)
	{
		nextNode = x;
	}

	/**	String representation suitable for output.
		Analysis: Time = O(n), n = number of nodes linked from this node */
	public String toString()
	{
		String temp = item.toString();
		if (nextNode != null)
			temp += " " + nextNode.toString();
		return temp;
	}
}

⌨️ 快捷键说明

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