ITeye博客

Java实现判断入栈序列是否可以以某个序列出栈

xglv2013 · 2013-05-17 00:48 · 算法 · 编程语言 · 算法 · Java · Markdown

输入第一个序列inSequence,是车厢入栈顺序 输入第二个序列outSequence,是车厢出栈顺序 算法判断以inSequence入栈的车厢可否以outSequence的顺序出栈 若可以,则返回出入栈动作的顺序并打印YES 若不可以,则返回出入栈动作的顺序直到失败的车厢并打印NO

算法的图示: 带括号的数字代表步数,红叉代表出栈,入栈序列的指针需要左右移动故采用双向链表,出栈序列的指针只向右移动故采用单向链表

package train;

public interface AddDelete
{
	void addNode(int addNumber);
	void deleteTop();
}
package train;

public class BidirectionalLinkedList implements AddDelete
{
	BidirectionalNode top;//stack top
	BidirectionalNode headNode;
	BidirectionalNode tail;
	int nodeNumber;
	
	public BidirectionalLinkedList()
	{
		headNode = new BidirectionalNode(0);
		tail = headNode;
		nodeNumber = 0;
	}
	
	public void addNode(int newNumber)
	{
		BidirectionalNode bn = new BidirectionalNode(newNumber);
		if(nodeNumber == 0)
		{
			top = bn;
		}
		tail.next = bn;
		bn.last = tail;
		tail = bn;
		nodeNumber++;
	}
	
	public void deleteTop()
	{
		if(top.last.equals(headNode))
		{
		    top = top.next;
		    headNode.next = top;
		    if(top != null)
		    {
		    	System.out.println("in");
		    	top.last = headNode;
		    }
		}
		else
		{
		    top.last.next = top.next;
		    if(top.next != null)
		    {
		    	top.next.last = top.last;
		    }
		    top = top.last;
		}
	}
	
	public int getNodeNumber()
	{
		return nodeNumber;
	}
}

package train;

public class BidirectionalNode
{
	int number;
	BidirectionalNode next;
	BidirectionalNode last;
	
	public BidirectionalNode(int number)
	{
		this.number = number;
		this.next = null;
		this.last = null;
	}
}
package train;

public class LinkedList implements AddDelete
{
	Node top;//stack top
	Node headNode;
	Node tail;
	int nodeNumber;
	
	public LinkedList()
	{
		headNode = new Node(0);
		tail = headNode;
		nodeNumber = 0;
	}
	
	public void addNode(int newNumber)
	{
		Node bn = new Node(newNumber);
		if(nodeNumber == 0)
		{
			top = bn;
		}
		tail.next = bn;
		tail = bn;
		nodeNumber++;
	}
	
	public void deleteTop()
	{
		headNode.next = top.next;
		top = headNode.next;
		nodeNumber--;
	}
	
	boolean isEmpty()
	{
		return nodeNumber == 0;
	}
}

package train;

public class Node
{
	int number;
	Node next;
	
	public Node(int number)
	{
		this.number = number;
		this.next = null;
	}
}
package train;

import java.io.IOException;
import java.io.InputStreamReader;

public class Demo
{   
	public static void readInSequence(AddDelete a, InputStreamReader in) throws NumberFormatException, IOException
	{
		int c;
		StringBuffer sb = new StringBuffer();
			while((c = in.read()) != -1)
			{
				char ch = (char)c;
				if(ch == ' ')
				{
					if(sb.toString().trim().equals(""))
						continue;
				    a.addNode(Integer.parseInt(sb.toString().trim()));
				    sb.delete(0, sb.length());
				}
				else if(ch == '\r')
				{
					if(!sb.toString().trim().equals(""))
					    a.addNode(Integer.parseInt(sb.toString().trim()));
					break;
				}
				else
				{
					sb.append(ch);
				}
			}
	}
	
	public static void main(String[] args)
	{
		BidirectionalLinkedList inSequence = new BidirectionalLinkedList();
		LinkedList outSequence = new LinkedList();
		
	    InputStreamReader inIn = new InputStreamReader(System.in);
	    InputStreamReader inOut = new InputStreamReader(System.in);
	    
	    try 
	    {
			System.out.println("pls input inSequence:");
			readInSequence(inSequence, inIn);
		    
			System.out.println("pls input outSequence:");
		    readInSequence(outSequence, inOut);
		}
	    catch (NumberFormatException e)
	    {
			e.printStackTrace();
		}
	    catch (IOException e)
	    {
			e.printStackTrace();
		}
	    finally
	    {
			try 
			{
		    	if(inIn != null)
				    inIn.close();
		    	if(inOut != null)
		    		inOut.close();
			}
			catch (IOException e) 
			{
				e.printStackTrace();
			}
	    }
	    
	    System.out.println("in");
	    
	    while(inSequence.top != null)
	    {
	    	if(inSequence.top.number != outSequence.top.number)
	    	{
	    		inSequence.top = inSequence.top.next;
	    		System.out.println("in");
	    	}
	    	else
	    	{
	    		System.out.println("out");
	    		inSequence.deleteTop();
	    		outSequence.deleteTop();
	    	}
	    }
	    if(outSequence.isEmpty())
	    {
	    	System.out.println("YES");
	    }
	    else
	    {
	    	System.out.println("NO");
	    }
	}	
}

程序的运行结果: 返回NO的例子:

返回YES的例子:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。