Queue help

Pages: 1... 345
There is more to your stack program. Look for something that uses charStack.top().
this is my stack version.

I dont have anything with charStack.top(). So I'm confused

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <stack>

using namespace 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 (unsigned int k = 0; k < str.length(); k++)
    {
        switch (str[k])
        {
        case '(':
            // Put left paren on stack
            charStack.push(str[k]); break;
        case ')':

            if (charStack.empty())
                return false;
            else
                charStack.pop();
            break;
        default:

            
        }
    }
    if (charStack.empty())
        return true;
    else
        return false;
}
Okay, it appears to me that both the stack and the queue based programs work. Are you getting errors? If so, what kind?

Note that if the program has to work with [] and {} also, then you'll need a little more logic.
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.
It's using a queue, so I guess it's correct. If you post the assignment, we can verify.

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.
So is my queue version right ?
Hey Toaster. Please stop stringing this piece of human garbage along.
See http://www.cplusplus.com/forum/lounge/269729/
Go away
Please don't report me to the "internet police"!
*poops pants*
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.
Thanks
Topic archived. No new replies allowed.
Pages: 1... 345