33
votes
Stack data structure in Python 3
From a pure Python standpoint, a list is already a stack if you narrow its methods down to only pop and append.
...
31
votes
A counterexample in balanced parentheses
May I suggest a simpler solution:
You need only a counter. it is incremented by 1 whenever you encounter an openning parenthesis "(" and is decreased by 1 whenever you encounter a closing parenthesis ...
25
votes
Accepted
HackerRank Implement Queue using two stacks Solution
Algorithmic complexity for combined operations
Containers are interesting elements in most programming languages: they have an internal state, namely their elements and therefore their size.
This ...
23
votes
Accepted
Implementing a Stack with Templates and Smart pointers
Readability
Please put a space after the include and before the <
...
21
votes
Accepted
Simple Stack implementation in C++
Your stack implementation is terrible, and so is @hc_dev: neither handles memory correctly.
Resource Handling
It is generally frowned upon to call new and ...
19
votes
Balanced parentheses using standard C++
Avoid using namespace std; - it's a large namespace, and growing as new C++ standards appear. Bringing all its names into the global namespace completely obviates ...
18
votes
Stack class in Java 8
Other answers correctly point out that using primitive types and not mixing them with Objects (=int instead of Integer), ...
16
votes
Leetcode: Valid parentheses
This is a follow up of @Henrik Hansen. Instead, of a switch I would use a Dictionary<T, K>. A Dictionary offers two main advantages: an increase readibility ...
14
votes
Accepted
Very simple stack in C
I see a number of things that may help you improve your code.
Use a stack pointer as a parameter
Right now, one can only have one stack at a time. Worse, if one calls ...
14
votes
Accepted
Checking for parentheses balance in C
Bugs
The loop header is wrong:
for(i=0;line[i]!=NULL;i++){
Here, you want to scan until you find a byte with ASCII value 0 (also called an ASCII NUL, and ...
14
votes
Array-based stack
The methods named emptyCheck() and fullCheck() are ambiguous. They should be named isEmpty() ...
13
votes
Accepted
StackList<T> implementation
Issues
public void Push(T item)
{
_stackList.Insert(0, item);
}
The hurts the stack list.
It would be much easier if you just added new items at the end of ...
13
votes
Checking for parentheses balance in C
The idea is correct, the implementation is not.
Upon exiting the loop you need to test for pilepointer == -1. Otherwise, the strings like ...
13
votes
Stack data structure in Python 3
On the whole the code is good, but I would look for three improvements:
Information-hiding: I would change self.stack to ...
13
votes
Stack data structure in Python 3
The code is clean. My major concern with how it looks is that your naming doesn't follow PEP8. Names should be snake_case, not camelCase. So isEmpty should be ...
12
votes
Accepted
LeetCode parentheses matching
Usage of Stack
Don't use a Stack. This class is very old, it's thread safe (which you don't need) and achieves it by ...
12
votes
Stack Implementation in C++ with Exceptions
std::allocator_traits is a better way to interact with an allocator. It provides defaults for construct and destroy if the allocator doesn't do it itself.
...
12
votes
Balanced parentheses using standard C++
using namespace std; may seem fine for small projects, but can cause problems later so it's best to avoid it.
Prefer to output "\n" instead of ...
12
votes
Stack class in Java 8
This looks good, However I suggest you properly indent the code.
Further suggestions:
I would change following
private final int INCREMENTSIZE = 1024;
to
<...
11
votes
Accepted
Dynamic stack C implementation
So you have build a dynamic size stack containing integers, and you guarantee that the allocated size if a power of 2 and the used size is between the allocated size and its quarter.
This automatic ...
11
votes
Checking for parentheses balance in C
Here are some things that may help you improve your code.
Don't use gets
Using gets is not good practice because it can lead ...
11
votes
Array-based stack
Formatting
You should clean up your indention. The methods should be indented to the same level as the fields and it is common in Java to place the opening bracket on the same line as the matching ...
11
votes
Leetcode: Valid parentheses
A couple of small things to add:
Maybe not applicable to a timed interview, but inline documentation (///) on public members is always nice, and would help to ...
11
votes
Stack class in Java 8
Your class is small and does what you said it should, that's good.
However, there are a few things you could do better.
1. Use primitive types unless the boxed ones are specifically needed:
You are ...
10
votes
Finding the maximum element of a Stack
This type of question actually requires a little thinking outside the box.
The idea here, since we need to store the max with each element, is to use the built-in ...
10
votes
Accepted
Stack Implementation in C++ with Exceptions
I would make methods names start with a lower case letter to distinguish them from user defined types. But that's me.
A couple of small things:
...
10
votes
Validator for mathematical expression in infix form
Don’t write using namespace std;.
You can, however, in a CPP file (not H file), or inside a function, put individual ...
10
votes
Accepted
C pointer based growable stack
I suggest that you do more error checking:
malloc or realloc may fail.
I can initialize the stack with negative capacity.
I can ...
10
votes
Stack Implementation in JavaScript
Encapsulate
The object Stack should protect its state as it is easy to add and remove items via the exposed referenced ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stack × 414java × 129
c++ × 111
c × 71
linked-list × 61
algorithm × 48
beginner × 47
python × 38
queue × 35
performance × 32
reinventing-the-wheel × 26
c# × 24
c++11 × 24
array × 22
interview-questions × 20
programming-challenge × 17
balanced-delimiters × 16
javascript × 15
python-3.x × 14
strings × 13
pointers × 11
math-expression-eval × 11
sorting × 10
memory-management × 10
complexity × 10