C++ Program to Check for Balanced Parentheses using Stack

This C++ program, using a stack data strucure, computes whether the given parantheses expression is valid or not by checking whether each parentheses is closed and nested in the input expression.

Here is the source code of the C++ program to display if it is a balanced expreesion or an invalid string. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Check for balanced paranthesis by using Stacks
  3.  */
  4. #include <iostream>
  5. #include <conio.h>
  6. using namespace std;
  7. struct node
  8. {
  9.     char data;
  10.     node *next;
  11. }*p = NULL, *top = NULL, *save = NULL,*ptr;
  12. void push(char x)
  13. {
  14.     p = new node;
  15.     p->data = x;
  16.     p->next = NULL;
  17.     if (top == NULL)
  18.     {
  19.         top = p;
  20.     }
  21.     else
  22.     {
  23.         save = top;
  24.         top = p;
  25.         p->next = save;
  26.     }
  27. }
  28. char pop()
  29. {
  30.     if (top == NULL)
  31.     {
  32.         cout<<"underflow!!";
  33.     }
  34.     else
  35.     {
  36.         ptr = top;
  37.         top = top->next;
  38.         return(ptr->data);
  39.         delete ptr;
  40.     }
  41. }  
  42. int main()
  43. {
  44.     int i;
  45.     char c[30], a, y, z;
  46.     cout<<"enter the expression:\n";
  47.     cin>>c;
  48.     for (i = 0; i < strlen(c); i++)
  49.     {
  50. 	if ((c[i] == '(') || (c[i] == '{') || (c[i] == '['))
  51. 	{
  52.             push(c[i]);
  53. 	}
  54. 	else 
  55. 	{
  56. 	    switch(c[i])
  57. 	    {
  58. 		case ')':
  59.                          a = pop(); 
  60. 		         if ((a == '{') || (a == '['))
  61. 			 {
  62. 			     cout<<"invalid expr!!";
  63.                              getch();
  64. 			 }   
  65. 			 break;
  66. 		case '}':
  67.                          y = pop();
  68. 			 if ((y == '[') || (y == '('))
  69. 			 {
  70. 			     cout<<"invalid expr!!";
  71.                              getch();
  72. 			 }   
  73. 			 break;
  74. 		case ']':
  75.                          z = pop();
  76. 			 if ((z == '{') || (z == '('))
  77. 			 {
  78. 			     cout<<"invalid expr!!";
  79.                              getch();
  80. 			 }   
  81. 			 break;
  82. 	    }
  83. 	}
  84.     }
  85.     if (top == NULL)
  86.     {
  87. 	cout<<"balanced expr!!";
  88.     }
  89.     else
  90.     {
  91. 	cout<<"string is not valid.!!";
  92.     }
  93.     getch();
  94. }

Output
enter the expression:
[{{{}}}{{()[[]]}}]
balanced expr!!   

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.