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

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

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

[img]http://dl.iteye.com/upload/attachment/0084/6030/4686addf-ca92-3e2b-8e12-e14fadbd19f3.png[/img]

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的例子:

[img]http://dl.iteye.com/upload/attachment/0084/6033/83f3e3a7-7896-3939-ab6c-2696c5728f38.jpg[/img]

返回YES的例子:

[img]http://dl.iteye.com/upload/attachment/0084/6035/fd08c81d-9d6d-3ab4-b1e0-5167a8bedf23.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值