⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mystack.java

📁 用java实现浮点数加减乘除四则混合运算
💻 JAVA
字号:
/**
  * This is the stack class for the program.
	* It is made up of the linked list.  <p> 
	* It can do th push, pop, peek operations as the stack requires. <p>
	* Besides,user can search for the element in the stack and get its index. <p>
	* It is used to store the data in the calculator. <p>
	* 
	* @author Rachel Chen 0122070
	* @version 1.0 2003/3/22
	*/
public class MyStack 
{
	/** The top pointer for the stack.*/
	private StackNode top;

	/** The number of the nodes in the stack.*/
	private int count;

  /**
	  * The constructor for the class.
		* Initialize the fields as top to null and count to 0.
		*/
	public MyStack()
	{
		top = null;
		count = 0;
	}
  
	/**
	  * Push the object onto the stack and return it.
		*
		* @param item The object to be pushed.
		* @return The pushed object.
		*/
	public Object push( Object item )
	{
		if ( top == null )
		{
			top = new StackNode( item );
			count = 1;
		}
		else
		{
			top = new StackNode( item, top );
			count++;
		}
		return item;
	}
  
	/**
	  * Pop the top object from the stack and return it.
		* 
		* @return The object poped if the stack is not empty. 
		* Or null for empty stack.
		*/
	public Object pop()
	{
		Object value;
		if ( top == null )
		{
       throw new RuntimeException( "Error:Index out of bound!" );
		}
		else 
		{
			value = top.element();
			top = top.next();
			count--;
		}
		return value;
	}
	
	/**
	  * Return the top object of the stack.
		* 
		* @return The object at the top of the stack if the stack is not empty. 
		* Or null for empty stack.
		*/
	public Object peek()
	{
		if ( top == null )
		{
       throw new RuntimeException( "Error:Index out of bound!" );
		}
		else 
		{
			return top.element();
		}
	}

  /**
	  * Check if the stack is empty.
		*
		* @return True for empty.False for non-empty.
		*/
	public boolean isEmpty()
	{
		return  count == 0;
	}

  /**
	  * Search for the given item and give the index of the item.
		*
		* @param item The object to be searched for.
		* @return The index of the object and -1 if it is not in the stack.
		*/
	public int search( Object item )
	{
		int number = 0;
		StackNode currentNode = top;

  	while ( currentNode != null )
		{ 
			if ( currentNode.element().equals(item ))
			{
				return number;
			}
			number++;
			currentNode = currentNode.next();
		}		
	
		return -1;
 	}
  
	/**
	  * Given the size of the stack.
		*
		* @return The size of the stack.
		*/
	public int size()
	{
		return count;
	}  	
}

⌨️ 快捷键说明

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