Data structure matches algorithm stack and queue separator

Keywords: Java

Separator match:

Stack is usually used to parse a certain type of text string. Usually, text string is a line of code written in computer language, and the program to parse them is compiler.

The call of check() looks at the StackX2 class.

In the main program, read the query line from the user without interruption, create a BracketCHecker object with the text string as the parameter, and then call the check() method of the BracketChecker object. If there are any errors, the check() method will display, otherwise the separator syntax is correct.

The check () method can report the location of mismatched delimiters.

The efficiency of stack, the time complexity of data entry and exit is constant O(1), the time of stack operation does not depend on the number of data entry in stack.

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: 

Queue:

First in, first out is adopted, and the data stored first is read out.

Insert:

Team head + 1, team tail unchanged

Delete:

Team end + 1, team head unchanged

Loop queue:

To avoid queue dissatisfaction but unable to insert new data, you can let the queue head and tail pointer go back to the beginning of the array.


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(" ");
	}
}

Posted by jiayanhuang on Fri, 31 Jan 2020 14:36:16 -0800