We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [stack]

A stack is a last in, first out (LIFO) abstract data type and data structure. Perhaps the most common use of stacks is to store subroutine arguments and return addresses.

Filter by
Sorted by
Tagged with
40
votes
2answers
35k views

Balanced parentheses

Given an expression string exp, write a program to examine whether the pairs and the orders of "{","}","(",")","[","]" are correct in exp. For example,...
30
votes
7answers
64k views

Check for balanced parentheses

Given an expression string exp, write a program to examine whether the pairs and the orders of "{","}","(",")","[","]" are correct in exp. For example,...
26
votes
1answer
2k views

Stack implemented using linked list in x86_64 assembly

I wrote a stack implementation using a singly linked list in x86_64 assembly. This stack supports the usual push/pop operations ...
26
votes
1answer
996 views

How can I make my stack monad faster?

I made a stack monad that lets one manipulate and control values on a stack. I want to know if it's correct (it seems correct), how to make it faster, and how I could detect stack overflows without ...
23
votes
3answers
5k views

Leetcode: Valid parentheses

https://leetcode.com/problems/valid-parentheses/ Given a string containing just the characters (, ), ...
16
votes
6answers
34k views

Implementing a Stack in Java for a technical interview

This code is based off the Stack implementation in Chapter 3 of Cracking The Coding Interview. I modified the code to make it compile and give me the correct output. I'd appreciate any feedback on ...
15
votes
4answers
22k views

Implementation of stack using pointers

Please review my code and let me know how I can possibly improve it. ...
15
votes
3answers
2k views

Java and C++ stack implementations with dramatically different performance

I'm developing a small programming language for use in any project I have where I feel a small scripting language could be used well. I've written two emulators for the language, one in C++ and one in ...
14
votes
3answers
1k views

Stack Implementation in C++ with Exceptions

Appreciate your thoughts and feedback... Stack.h ...
14
votes
2answers
114k views

Stack implementation using linked list

This is a working stack implementation using a linked list. I'm just curious to know if this is a good way of doing it. Any suggestions are welcome. Can we keep track of the number of elements in ...
12
votes
5answers
3k views

Stack class in Java 8

I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. Are there better practices to use in my code? Is there something I should ...
12
votes
1answer
2k views

Stack implementation using Swift

I would like to request some code review and feedback on a stack implementation using Swift. Disclaimer: I might have not used protocols, optional unwrapping and error and ...
12
votes
3answers
2k views

Stack implementation with move semantics

I have implemented a basic stack just for fun. I've just started learning about move constructors and assignment and move semantics as a whole, so I would really appreciate some feedback as to whether ...
11
votes
3answers
12k views

C++ Stack using template

I'm learning C++, so I wrote a stack with the help of templates. I'm wondering if there is anything I can improve or if I did something vastly wrong. Stack.h ...
11
votes
3answers
2k views

Implementation of an Stack without Linked Lists in C

I'm a beginning C programmer. As an exercise, I have attempted to implement a stack of strings in C. This is my first real exercise working with pointers. I looked at some other similar posts on here, ...
11
votes
2answers
1k views

Stack having push, pop and return min in O(1)

I am using a class which is taking care of minimum value so far. Please let me know of any improvements or suggestions. ...
11
votes
2answers
3k views

How can this simple coroutine implementation be improved?

This is a quick and dirty implementation of coroutines that implements yield by saving and restoring the stack to and from the heap. Here's an earlier version, which does the most naive possible thing,...
11
votes
1answer
2k views

Undo and redo functionality for a GTK+ program

I'm working on a small GTK+ program and I'm now implementing undo and redo capabilities. This is the code that I'm using to handle the different states. It's just a linked list storing the allocated <...
10
votes
6answers
23k views

Reversing a String using Stack

Is this code okay? ...
10
votes
3answers
2k views

Implementing a Stack with Templates and Smart pointers

I'm trying to understand the concepts of smart pointers and templates. To get a better understanding of these, I tried to implement these two concepts on Stack. I ...
10
votes
3answers
111k views

Stack implementation using a linked list

To understand the concept, I implemented the stack operations using a linked list. Please review the code and tell me your suggestions. Node.java ...
10
votes
4answers
1k views

Array implementation of Java Stack

As an exercise, I wanted to make an array implementation of Stack, including most of the methods from the native Java Stack. I'...
10
votes
3answers
2k views

StackList<T> implementation

I needed a structure that works really similar to the predefined Stack<T> but with a few extra functionalities. Examples include getting the index of value in ...
10
votes
2answers
197 views

SortedStack implementation in Java

This is a data structure that supports the push(), pop() and isEmpty() operations and ...
10
votes
4answers
1k views

Validator for mathematical expression in infix form

This program will validate whether a mathematical expression entered by the user is a valid expression based off whether the expression itself has been entered with the correct scope openers and scope ...
10
votes
4answers
734 views

Stack data structure implementation

I have implemented in C# a stack data structure, without using any existing C#/.NET containers (data structures). I have used TDD technique to implement this requirements, so you can find tests, too. ...
10
votes
3answers
2k views

Making an atomic transaction out of several operations

I'm looking for a design pattern or suggestions that can help refactor my code into something a bit less repetitive. I have a method that has several sequential steps (10-15) that if any of them fail ...
10
votes
1answer
1k views

Dynamic stack C implementation

I'm a C beginner. I'm studying and implementing some data structures. I'd like to get feedback. This is a stack implemented with an dynamic array. In main there ...
10
votes
2answers
2k views

Managing a programmatically accessible stack trace

VBA has a call stack... but there's no programmatic way to tap into it, which means in order to get a stack trace for a runtime error, one has to manage it manually. Here's some example code that ...
9
votes
4answers
4k views

Checking for parentheses balance in C

A recent assignment asks me to create a program that checks for parentheses balance in a given string. As I can't find any duplicate questions that have code written in C, I decided to post another ...
9
votes
2answers
2k views

A counterexample in balanced parentheses

In this program I had to analyse if the parenthesis given are well balanced. For instance, input (()) is correct and input ())(()...
9
votes
2answers
3k views

Balanced parentheses using standard C++

Here is a small program for checking if the parentheses are balanced or not. I am new to the standard library, and this is my first program. How can I improve it? Any kind of modifications or ...
9
votes
5answers
708 views

Stack data structure in python 3

I have implemented a Stack. It works well. But if you think something needs to be improved, say it. you would love to review my code . ...
9
votes
4answers
4k views

Finding the maximum element of a Stack

I have been solving this problem of Hackerrank recently .. https://www.hackerrank.com/challenges/maximum-element A little bit about the problem You have an empty sequence, and you will be given N ...
9
votes
3answers
297 views

Fuzzy Octo Guacamole interpreter

This is my code that interprets my own golfing and esoteric language, Fuzzy-Octo-Guacamole. It has 2 stacks to store information in, and various operations can be performed on each stack, or both at ...
9
votes
3answers
2k views

Generic stack in C

I tried to implement a generic stack in C using void pointers and tried to keep it as simple as possible by delegating all responsibility to the caller of the ...
8
votes
3answers
2k views

HackerRank Implement Queue using two stacks Solution

Challenge Problem Statement - Implement a Queue using two Stacks I implemented dequeue() in \$O(1)\$ at the cost of enqueue() ...
8
votes
3answers
9k views

Simple array-based Stack class in Java

I have created a simple array-based Stack class with some methods like the actual Stack in Java. I am testing this for mistakes, but since I am learning Java, my tests may not be as comprehensive as ...
8
votes
2answers
18k views

STL Stack Implementation

I implemented std::stack from the STL for deeper understanding of the language and memory since I still am only a beginner. I implemented the stack using a singly ...
8
votes
2answers
1k views

Which is the most correct pattern for using Stack.Pop?

There at least 2 patterns of using Stack.Pop ...
8
votes
3answers
318 views

Stack as a Persistent Data Structure Implementation

After reading some articles about immutability, I found out that there are persistent data structures. In the following, I implemented a stack as a persistent data structure. What I like about the ...
8
votes
6answers
800 views

Implement queue using two stacks

Although it's a well-known algorithm, I thought to implement it from the perspective of a coding interview. I want to know if I can improve my code somehow. So, given the time constraint, is this a ...
8
votes
3answers
2k views

Simple implementation of a generic stack

I'm fairly new to C++ programming. To help I've been writing my own data structures for practice. I'd liked this to be judged as if it was written by a professional, and receive honest feedback. I ...
8
votes
2answers
1k views

Python unit testing for a Stack data structure

I am completely new to unit testing. I read this chapter in Dive Into Python 3, and decided to give it a try on a basic data structure: a stack. Here is the stack code (taken from "Problem Solving ...
8
votes
2answers
557 views

Implement a stack using a linked list

I am to implement a stack in C using a linked list. My code is below. My design: I use stack_ prefix to each function, and each function has a ...
8
votes
1answer
8k views

A working stack allocator

Here's an absolutely essential piece of C++ lore, a stack allocator, that will allow you to, say, allocate strings and vectors on the stack. There are 2 stack allocators I know of, here and here. The ...
8
votes
2answers
6k views

Balance braces method

This is a continuation of this question, at least the first part of that question. I want to check if everything in the new code is right or if there are other way to make it better. Exercise The <...
8
votes
1answer
174 views

Compute the height of a tower made of buckets

I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable ...
8
votes
1answer
77 views

Implementing Multi-File Programs in Vitsy

I'm the proud owner of the Vitsy programming language, which I've been working on for some time (except recently, because high school). It's only been used in PPCG so far, but I hope to expand it to ...
7
votes
3answers
32k views

Stack implementation using an array

I am trying a stack implementation using an array. I want to know if this approach is OK or if there is a logical problem. This program is working fine. ...