Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 63 / 68

Checking that brackets match (an application of stacks)

This lesson covers checking bracket matching with a stack in VB.NET, with the aim of seeing a practical application of the Stack data structure.

Pushing each opening bracket onto a Stack and, when a closing bracket arrives, taking the top off and checking it corresponds, verifies that the brackets match. It is the same idea used in real syntax checking.

The sample code holds the opening bracket for each closing bracket in a Dictionary called pairs, pushes with stack.Push() when an opening bracket arrives, and checks against the top when a closing one does. If the stack is empty at the end, the brackets balance.

A common early stumble is handling several kinds of bracket at once (round, square, and curly mixed together). Counting openings and closings is not enough — you have to check they nest in the right order.

In professional work, this thinking is used widely wherever bracket or tag correspondence has to be verified — a language parser, validating JSON or XML.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)