Hot answers tagged

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. ...
Right leg's user avatar
  • 441
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 ...
Sharon Ben Asher's user avatar
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 ...
Zeta's user avatar
  • 19.2k
23 votes
Accepted

Implementing a Stack with Templates and Smart pointers

Readability Please put a space after the include and before the < ...
Martin York's user avatar
  • 94.3k
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 ...
Matthieu M.'s user avatar
  • 4,764
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 ...
Toby Speight's user avatar
  • 72.1k
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), ...
Luke's user avatar
  • 281
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 ...
aloisdg's user avatar
  • 561
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 ...
Edward's user avatar
  • 65.3k
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 ...
200_success's user avatar
14 votes

Array-based stack

The methods named emptyCheck() and fullCheck() are ambiguous. They should be named isEmpty() ...
TwiN's user avatar
  • 453
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 ...
t3chb0t's user avatar
  • 44.2k
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 ...
vnp's user avatar
  • 55.9k
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 ...
snakecharmerb's user avatar
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 ...
Carcigenicate's user avatar
12 votes
Accepted

Stack implementation with move semantics

General Comments Don't like the naming of your class _stack_node or stack. The first one has a leading underscore. This is ...
Martin York's user avatar
  • 94.3k
12 votes

Array-based stack data structure in Java

Auto-resize. There's nothing saying an array-based stack has to have a fixed size. The standard library Stack doesn't have a fixed size, and it's array-based. Same with ArrayDeque, the stack you ...
user2357112's user avatar
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 ...
Duarte Meneses's user avatar
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. ...
ratchet freak's user avatar
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 ...
user673679's user avatar
  • 11.6k
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 <...
JaDogg's user avatar
  • 4,531
11 votes
Accepted

Array implementation of Java Stack

Suppressing warnings It's hard to defend suppressing warnings at the class level. The problematic lines in your code are these: ...
janos's user avatar
  • 111k
11 votes

SortedStack implementation in Java

First up, let's both simplify your low level Stack class, and correct the Generics at the same time. Stack The issues with the generics are "obvious" by the ...
rolfl's user avatar
  • 97.3k
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 ...
Serge Ballesta's user avatar
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 ...
Edward's user avatar
  • 65.3k
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 ...
RoToRa's user avatar
  • 10.9k
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 ...
VisualMelon's user avatar
  • 7,421
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 ...
GiantTree's user avatar
  • 628
10 votes

Simple FIFO and LIFO ferry simulator

A single ferry?! Why is this Ferry class (mostly) static? There is such thing as multiple ferries. And, since this information that you are putting inside the Ferry ...
SirPython's user avatar
  • 13.3k

Only top scored, non community-wiki answers of a minimum length are eligible