Stack or Queue?

Discussion in 'C++' started by deanfamily, Dec 9, 2005.

  1. deanfamily

    deanfamily Guest

    I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example:
    {25 + (3 - 6) * 8}
    In this case they match (one corresponding left and right bracket for both). In this case:
    (25 + (3 - 6} * 8)
    they don't match (there is a } that should be a )). If anyone can give me an idea or something to get me heading in the correct direction, I'd appreciate it.
    Please note: I HAVE to use a stack or queue.
     
    deanfamily, Dec 9, 2005
    #1
    1. Advertising

  2. deanfamily

    Guest

    deanfamily wrote:
    > I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example:
    > {25 + (3 - 6) * 8}
    > In this case they match (one corresponding left and right bracket for both). In this case:
    > (25 + (3 - 6} * 8)
    > they don't match (there is a } that should be a )). If anyone can give me an idea or something to get me heading in the correct direction, I'd appreciate it.
    > Please note: I HAVE to use a stack or queue.


    The answer to this is REALLY easy. I am reluctant to even give a hint.

    Here is your hint...

    What are the main operations of a stack or queue? (in general, not the
    std:: versions specifically)
     
    , Dec 9, 2005
    #2
    1. Advertising

  3. deanfamily wrote:
    > I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example:
    > {25 + (3 - 6) * 8}
    > In this case they match (one corresponding left and right bracket for both). In this case:
    > (25 + (3 - 6} * 8)
    > they don't match (there is a } that should be a )). If anyone can give me an idea or something to get me heading in the correct direction, I'd appreciate it.
    > Please note: I HAVE to use a stack or queue.


    OK, here's a hint. A stack is LIFO (last in, first out). A queue is
    FIFO (first in, first out). When you analyze an arithmetic expression,
    and you come across, say, a right parenthesis, do you look at the most
    recent symbol you came across (that is, the last thing you added) or
    the first symbol you came across (that is, the first thing you added)?

    And this isn't a peculiar C++ assignment - sounds like a pretty typical
    introductory assignment to me. And a fairly easy one, once you think
    it through.

    Best regards,

    Tom
     
    Thomas Tutone, Dec 9, 2005
    #3
  4. deanfamily

    Sashi Guest

    When is your homework due? Don't you have your textbook with you?
    Sashi
    wrote:
    > deanfamily wrote:
    > > I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example:
    > > {25 + (3 - 6) * 8}
    > > In this case they match (one corresponding left and right bracket for both). In this case:
    > > (25 + (3 - 6} * 8)
    > > they don't match (there is a } that should be a )). If anyone can give me an idea or something to get me heading in the correct direction, I'd appreciate it.
    > > Please note: I HAVE to use a stack or queue.

    >
    > The answer to this is REALLY easy. I am reluctant to even give a hint.
    >
    > Here is your hint...
    >
    > What are the main operations of a stack or queue? (in general, not the
    > std:: versions specifically)
     
    Sashi, Dec 9, 2005
    #4
  5. deanfamily wrote:
    > I have a rather pecurliar C++ assignment. I need to create a program
    > (using a stack or queue) to verify if the grouping symbols in an
    > arithmetic expression match. For example:
    > {25 + (3 - 6) * 8}
    > In this case they match (one corresponding left and right bracket for
    > both). In this case:
    > (25 + (3 - 6} * 8)
    > they don't match (there is a } that should be a )). If anyone can give
    > me an idea or something to get me heading in the correct direction, I'd
    > appreciate it.
    > Please note: I HAVE to use a stack or queue.


    Use both. A queue has to begin and end in a parenthesis or a curly brace.
    The contents of each queue are the "primary expressions" between the
    corresponding symbols. As you encounter an "opening" symbol, start
    another queue. As you encounter a "closing" symbol, check the currently
    "active" queue and if it's OK, finish it and make a "primary expression"
    from it, and add to the previous queue. To maintain active queues, you
    will probably need to use a stack.

    I don't see any C++ language questions, though.

    V
     
    Victor Bazarov, Dec 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,114
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    726
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    1,049
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    601
    Jerry Hill
    Apr 25, 2008
  5. Kris
    Replies:
    0
    Views:
    538
Loading...

Share This Page