re: Stack - Data structure VIEW POST


Some suggestions (I'm not sure how indepth you want this ie if it should be treated as a complete implementation or just a sample):

  1. Use templates so your stack can be used for more than just ints
  2. Dynamically allocate memory, don't use a static array. Instead of doing it yourself, use std::vector as the array (now you can remove isFull)
  3. Combining 1) and 2), take the collection type as a type parameter so you could use the same stack class for array based or linked list implementations (ie, what std::stack does), so there's no need to write two implementations
  4. If the only reason you branch on a condition is to return a boolean, just return the condition (ie return top == -1, return top >= MAX - 1)
  5. If you're writing your own collection in C++, implement iterators so you can use it with std::find, range-based for loops and other friends (see stackoverflow.com/questions/816456...)
  6. Either expose isEmpty and don't call it in pop (require the user to check before popping - how C++ does it), or don't expose isEmpty and call it yourself in pop (and assert/throw an exception if it is, instead of returning a sentintel value)
  7. Separate pop into top and pop, where top returns a reference to the top thing and pop does the popping (see cpptruths.blogspot.com/2005/10/why...)
  8. Prefer to use std::vector::empty() rather than checking size() == 0 yourself
  9. Combining 4 and 8, the end of isBalanced can be simplified to return openingBracketsStack.empty();
code of conduct - report abuse