#include <iostream>
#include <string>
#include <stack>
usingnamespace std;
bool isBalanced(string s);
int main()
{
cout << "This program checks a string to see if its parentheses are properly \n";
cout << "balanced.";
string str;
cout << "\nType in a string with some parenthesis:\n";
getline(cin, str);
if (isBalanced(str))
cout << "The string has balanced parentheses.";
else
cout << "The string does not have balanced parentheses.";
return 0;
}
bool isBalanced(string str)
{
stack<char> charStack;
for (unsignedint k = 0; k < str.length(); k++)
{
switch (str[k])
{
case'(':
// Put left paren on stack
charStack.push(str[k]); break;
case')':
if (charStack.empty())
returnfalse;
else
charStack.pop();
break;
default:
}
}
if (charStack.empty())
returntrue;
elsereturnfalse;
}
I just wanted to make sure the queue version that i posted was indeed a queue version. I am unable to tell what all an queue version entails. So reason I was unsure.
For this assignment, write a program that uses a single stack to check whether a string containing braces, parentheses, and brackets is properly delimited.
Then, write another program that uses a single queue to check whether a string containing braces, parentheses, and brackets is properly delimited.
Please note that two separate programs will be submitted. One using a stack and the other using a queue.
The difference between queue and stack is FIFO vs LIFO. Whether you remove a character from the beginning or the end doesn't matter as long as the character is being removed at some location from the data structure if the currect char in the string is a closing brace. Also, it doesnt matter what you push onto the structure either. It could be an open brace, a letter, a number, anything. It is merely used for keeping track of the number of braces that are balanced/unbalanced.
The thing that's crazy about this assignment is that they're asking you to use a queue, but you have to use the queue as a stack.
The programs only handles parentheses, not brackets and braces. To be honest, I would leave the program as is. I don't think you'll be able to get it to work for braces and brackets too.