34
votes
Accepted
Balanced parentheses
This is almost all style suggestions; the code itself looks great.
Personally, I prefer the brace-on-same-line style for everything in JS, and I prefer proper blocks instead of inlining expressions. ...
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 ...
28
votes
Accepted
Stack implementation using Swift
Let's start with some general remarks:
Swift does not require semicolons after statements (but they are allowed). From what I have seen since Swift was introduced last year,
most people do not write ...
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
Reversing a String using Stack
Unicode is hard to get right, especially in Java as it has a more or less broken concept of a “character”. An Unicode code point is a 21-bit number. These code points are encoded to bytes with the UTF-...
20
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
Accepted
Implementing a Stack in Java for a technical interview
A stack should not know about the first element. A stack only know about the last element that was pushed, so the Stack object itself should only have one ...
18
votes
Accepted
Stack implementation using a linked list
Time complexity
The time complexity of push and pop operations should be \$O(1)\$, and so it is in your case too. It doesn't matter how many elements you have, these operations should take constant ...
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), ...
17
votes
Reversing a String using Stack
This question provides a really good example of how learning 'tools' is important, but understanding efficiency is better.
The purpose of the task was probably to teach you to use a stack, but there ...
16
votes
Accepted
Check for balanced parentheses
I just wanted to write that the code looks completely fine to me, until...
assertEquals(false, isBalanced("[["));
I''m sure you'll find the error :-)
...
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 ...
15
votes
Implementation of stack using pointers
Please fix your indentation all over the header. It should be consistent with everything else.
You don't need the return 0 at the end of ...
14
votes
Accepted
Stack implementation using an array
sscanf is not safe
It's not part of your stack but part of your driver code, but scanf() is not a good function to use. There are many reasons for this, but the ...
14
votes
Accepted
Implementation of stack using pointers
First of all, I will start with one of the most common remarks: please, do not use using namespace std;. It is especially bad if you write it in a header file since ...
14
votes
Implementing a Stack in Java for a technical interview
As usual, revewing a design rather than code.
As coded, pop takes time proportional to the current stack size. It could and should take constant time. Hint: ...
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() ...
14
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
13
votes
Reversing a String using Stack
@amon wrote a great answer. With Java 8, handling of surrogate pairs is even easier. There is a codePoints() method on the CharSequence interface.
...
13
votes
Accepted
Java and C++ stack implementations with dramatically different performance
Performance
I'll get to a bit of a review, but first, since you're most interested in performance, I've done some benchmarking. In short, all evidence points towards maaartinus' theory being the ...
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
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stack × 399java × 129
c++ × 104
c × 65
linked-list × 56
algorithm × 48
beginner × 45
python × 37
queue × 34
performance × 31
c# × 26
reinventing-the-wheel × 25
c++11 × 24
array × 21
interview-questions × 20
programming-challenge × 18
javascript × 15
python-3.x × 14
balanced-delimiters × 14
strings × 13
pointers × 11
sorting × 10
memory-management × 10
complexity × 10
math-expression-eval × 10