stack example

Stack Example 2: Delimiter Matching

In previous article Stack introduction and implementation we had learnt how to implement stack in java and in our first example Stack Example 1 we have seen how to use stack to reverse a word or a string.

Stacks are commonly used to parse certain kind of text strings. For example, the below program uses stack to check the matching delimiters typed by the user.

Here are some examples:

Stack Example: Delimiter Matching

The above program reads characters from the string one at a time and pushes opening delimiters on to stack when if finds them.

Once it reads a closing delimiter from the input, then it pops the opening delimiter from the top of the stack and attempts to match it with the closing delimiter. If it did not find the matching type then it will show error.

Try running the above program with correct and in-correct strings and see what happens.

Output

Recommended Posts

References

Leave a Reply

avatar
  Subscribe  
Notify of