1

I completely understand what a stack is supposed to do (last in, first out). It's just one part of the stack that gets me confused.

typedef struct dynArrStruct
{
    char *location; //element
    int length;  //max size
    int currSize; //top
}dynArr;


dynArr a; //declared in main
//sorry this is an assignment and I try to refrain from posting my full code
//on here b/c of potential cheaters

I use this code as my stack.

Basically my program is supposed to find balanced characters: '{', '(','<', '[' and their closing counter parts.

In a nutshell, everytime I find an opening brace, I push it onto the stack. I keep pushing it onto the stack until I find a closing brace and as soon as I find a closing brace I need to pop the stack.

What I'm getting confused is with the variable char* location.

Let's say my string is "()"

In GDB:

If I read in '(' I push it onto the stack.. and if I read in ')' I pop it.

When I do: p a->location it prints out "()"

I'm just wondering am I supposed to be deleting "()" from the value of a->location everytime I pop a stack or is popping the stack irrelevant to a->location?

In other words should it print out "" after it has been popped?

I apologize ahead of time if this question doesn't make sense

2
  • 1
    side note: only a moron would "cheat" by copying code that is admittedly broken (thus why we're here). (and at least one of the answers below is pretty solid). Commented Feb 21, 2013 at 3:31
  • Are you doing the Shunting Yard Algorithm? Commented Feb 21, 2013 at 3:35

2 Answers 2

2

Since you haven't posted your code it's hard to be sure, but I suspect you are confusing a pointer (e.g. char * location) with the contents it is pointing to. A pointer currently pointing to a string "()" will print out as () in gdb, but the character pointed to (top of stack) would be just '('.

It will be easier to be more specific if you post at least part of your code.

Sign up to request clarification or add additional context in comments.

2 Comments

my "top" variable is what holds the stack correct? I'm referring to my "location" variable. After it's all said and done Is it okay if my char location variable prints out what characters it read in or am I supposed to be deleting the elements as part of the stack implementation? I do decrement the top variable (which is currSize in my code).
@juice I really can't tell any more without seeing some code.
2

Yes, after you pop an element from the stack, it should no longer be accessible on the stack. The size of the stack, representing the number of accessible elements, should also decrease by one. The difference between accessing the top-value and popping the top value is one of the reasons why the C++ STL (I know you're working in C, but this is just for an example) provides two different functions for popping and accessing the top value in a std::stack object. The std::stack::top method allows you to actually access the element on the top of the stack, while the std::stack::pop method removes the element by "popping" it off the top. If you are going to go with a single pop method though, then that method should both return a copy of the value that was on the top of the stack while also removing the internal representation of that value from the stack data-structure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.