数据结构与算法11-栈和队列-分隔符匹配

 分隔符匹配:

栈通常用于解析某种类型的文本串,通常,文本串是用于计算机语言写的代码行,而解析它们的程序就是编译器。

check()调用看StackX2类。

主程序中从用户那里不间断读取问本行,把文本字符串作为参数创建一个BracketCHecker对象,然后调用这个BracketChecker对象的check()方法。如果出现任何错误,check()方法将显示出来,否则分隔符的语法是正确的。

check()方法能报告出现不匹配分隔符的位置。

栈的效率,数据项入栈和出栈的时间复杂度为常数O(1),栈操作所耗的时间不依赖于栈中数据项的个数。

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


class StackX2 {
private int maxSize;
private char[] stackArray;
private int top;
	public StackX2(int s)
	{
		maxSize = s;
		stackArray = new char[maxSize];
		top = -1;
	}
	public void push(char j)
	{
		stackArray[++top] = j;
	}
	public char pop()
	{
		return stackArray[top--];
	}
	public char peek()
	{
		return stackArray[top];
	}
	public boolean isEmpty()
	{
		return (top == -1);
	}
}
class BracketChecker
{
	private String input;
	public BracketChecker(String in)
	{
		input = in;
	}
	public void check()
	{
		int stackSize = input.length();
		StackX2 theStack = new StackX2(stackSize);
		for(int j=0;j<input.length();j++)
		{
			char ch = input.charAt(j);
			switch(ch)
			{
			case '{':
			case '[':
			case '(':
				theStack.push(ch);
				break;
			case '}':
			case ']':
			case ')':
				if(!theStack.isEmpty())
				{
					char chx = theStack.pop();
					if((ch=='}'&&chx!='{') || 
						(ch==']'&&chx!='[') ||
						(ch==')'&&chx!='('))
						System.out.println("error:" + ch + "at" + j);
				}
					else
						System.out.println("error:" + ch + "at" + j);
					break;
					default:
						break;
				}
			}
			if(!theStack.isEmpty())
				System.out.println("error:missing");
		}
	}
class BracketsApp
{
	public static void main(String[] args) throws IOException
	{
		String input;
		while(true)
		{
			System.out.print("enter string containing delimiters: ");
			System.out.flush();
			input = getString();
			if(input.equals(" "))
				break;
			BracketChecker theChecker = new BracketChecker(input);
			theChecker.check();
		}
	}

	private static String getString() throws IOException {
		// TODO Auto-generated method stub
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String s = br.readLine();
		return s;
	}
}
enter string containing delimiters: 
enter string containing delimiters: f{fklkfla(ff[d]}
error:}at15
error:missing
enter string containing delimiters: 
enter string containing delimiters: a{b(c]d}e
error:]at5
enter string containing delimiters: 

队列:

采用先进先出,先存入的数据先被读出。

插入:

队头+1,队尾不变

删除:

队尾+1,队头不变

循环队列:

避免队列不满但不能插入新的数据,可以让队头队尾指针绕回数组开始的位置。


class Queue {
	private int maxSize;
	private long[] queArray;
	private int front;
	private int rear;
	private int nElems;
	public Queue(int s)
	{
		maxSize = s;
		queArray = new long[maxSize];
		front = 0;
		rear = -1;
		nElems = 0;
	}
	public void insert(long j)
	{
		if(rear == maxSize - 1);
		rear = -1;
		queArray[++rear] = j;
		nElems++;
	}
	public long remove()
	{
		long temp = queArray[front++];
		if(front == maxSize)
			front = 0;
		nElems--;
		return temp;
	}
	public long peekFront()
	{
		return queArray[front];
	}
	public boolean isEmpty()
	{
		return (nElems==0);
	}
	public boolean isFull()
	{
		return (nElems==maxSize);
	}
	public int size()
	{
		return nElems;
	}
}
class QueueApp
{
	public static void main(String[] args)
	{
		Queue theQueue = new Queue(5);
		theQueue.insert(10);
		theQueue.insert(11);
		theQueue.insert(12);
		theQueue.insert(14);
		theQueue.remove();
		theQueue.remove();
		theQueue.remove();
		theQueue.insert(77);
		theQueue.insert(88);
		theQueue.insert(99);
		theQueue.insert(77777);
		while(!theQueue.isEmpty())
		{
			long n = theQueue.remove();
			System.out.println(n);
			System.out.println(" ");
		}
		System.out.println(" ");
	}
}

 

转载于:https://my.oschina.net/u/3829307/blog/1912950

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值