Stack Queue Program
Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack. Use the following data to test your code:
( )
[ ] ( ) { }
[ { ( ) } ]
[ [ ) )
[ ( ) ] ] [
( ( ( ) ) ) [ ( ) ( ) ]
{ ( ) [ ( ) ] }
Print out the input string and if the string is matched or mismatched.
Implement a Queue class using the List Class you developed earlier. Test your Queue class by determining if the following strings are char-by-char palindrome. To test for a palindrome, you will used both a stack and a queue data structure.
ABCDEDCBA
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
(({}[][]{}((
Tattarrattat
racecar
Note about your List class. You developed you List class (I hope) using ET (Element Type) for the data type stored in the array (List). Now the above you will need to change the ‘typedef’ of ET from int to char. If you have a good class development, then this should be one line change, if not, you will have several ints to change. Be sure NOT to change the ints that deal with position, size etc. just the data type stored in the list.
You can use a file for input of your strings. You may save this spec sheet to a file and then edit it and leave only the data strings. Save file as StackStr.txt. Do the same thing again and save the QStr.txt for the queue program.
#include <iostream>
#include <stack>
using namespace std;
void balance_parentheses();
int main()
{
int t;
cout << "Enter :";
cin >> t;
for (int i = 0; i < t; i++) {
balance_parentheses();
}
return 0;
}
void balance_parentheses()
{
stack<char> test;
string P;
cout << "Enter parentheses:";
cin >> P;
int flag = 0;
for (int i = 0; i < P.length(); i++)
{
if (P[i] == '{' || P[i] == '[' || P[i] == '(') {
test.push(P[i]);
flag = 1;
}
if (!test.empty()) {
if (P[i] == '}') {
if (test.top() == '{')
{
test.pop();
continue;
}
else
break;
}
if (P[i] == ']') {
if (test.top() == '[') {
test.pop();
continue;
}
else
break;
}
if (P[i] == ')') {
if (test.top() == '(') {
test.pop();
continue;
}
else
break;
}
}
else {
break;
}
}
if ((test.empty()) && (flag == 1))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
//////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Get Answers For Free
Most questions answered within 1 hours.