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
4
votes
2answers
71 views

Basic Stack in C++ Using Array

I wrote a stack class in C++ using arrays of fixed width. Could anyone review my code ? I didn't comment on any of the functions, because I thought class itself is self explanatory. Is it a wrong ...
5
votes
2answers
88 views

Bracket balance checker in Python

Implement a feature for a text editor to find errors in the usage of brackets in the code. Input: sorted(list(zip(a, b)) Output: False Input: ([{}, {}]) Output: True Here's my implementation in ...
5
votes
3answers
70 views

Unix tail command using stack

I'm working on a version of the Unix tail command that works by iterating over a file, pushing line by line into a stack. Finally, the number of desired lines will be popped and printed to the screen. ...
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() ...
3
votes
3answers
615 views

JavaScript Sort Stack Coding Challenge

A coding challenge to write a function sortStack that receives a stack of integers into ascending order (with largest integers on top) and returns another stack ...
9
votes
2answers
2k 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 ...
4
votes
2answers
66 views

Thread-safe stack in Java with fixed capacity

Implement a multi-threaded stack, with freedom to use existing implementations of stack. On top of being thread-safe, it must block (not busy-wait) a pushing thread when stack is full and a ...
-4
votes
1answer
55 views

Generic Custom STACK Class for Hanoi Problem in C# [closed]

I have created a custom class (Generic) in C# for the Hanoi problem. and I am not sure I did the correct thing. ...
4
votes
2answers
92 views

Create a min stack

The task is taken from leetcode Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes ...
4
votes
1answer
41 views

Directory Stacks with Python

I implemented basic pushd and popd functionality for a Python script. What's a more academic/formal way to implement a directory ...
1
vote
2answers
131 views

Leetcode - find if BST is valid or not

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node'...
8
votes
3answers
166 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 ...
5
votes
3answers
793 views

Stack Interview Code methods made from class Node and Smart Pointers

Mainly looking for feedback on my use of smart pointers to implement a standard stack. For interview level production code I have also include a namespace and asserts to test my class, let me know if ...
5
votes
2answers
74 views

Stack template using linked list

I'm working on a stack implementation using a linked list, but I have a strong feeling that I overcomplicated my solution. I would appreciate it if you review this code and give me any suggestions on ...
22
votes
3answers
5k views

Leetcode: Valid parentheses

https://leetcode.com/problems/valid-parentheses/ Given a string containing just the characters (, ), ...
1
vote
1answer
264 views

C++ Updated Stack Code

The original question can be seen here: Stack implementation in C++ using linked list Changes (with the help of Martin): Added copy constructor and assignment operator overload Changed return type ...
5
votes
2answers
131 views

Stack implementation in C++ using linked list

I am learning C++ and I made a simple stack class using a (reversed) linked-list to test my knowledge. Are there any problems with the following code, or any suggestions to improve it? I want to ...
2
votes
2answers
149 views

Using the stack algorithm for parenthesis matching

I have used Stacks to check for brackets mismatch. (Parentheses matching problem using Stack algorithm) Any suggestions on how to improve the code? I have tried various examples and it works ...
3
votes
2answers
159 views

Stack implementation in C++ including min(), max() functions in O(1)

This implementation seems to work. I would love to get any suggestions / comments on how this code could be improved. Are there any potential memory management issues? Any additional methods that ...
1
vote
2answers
45 views

Stack Array implementation of Java

This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached. I am a self-taught programmer starting to learn data structures and ...
1
vote
1answer
201 views

Stack using Linked List in Swift

I made this implementation of a stack in Swift that stores its elements as a linked list. It seems to work perfectly well, but I’m wondering how I can improve it to follow the best practices of the ...
2
votes
3answers
214 views

Implementing a stack using a linked-list

In my intro CS class we're reviewing data structures. I'm currently working on implementing a stack using a linked list (LIFO) in C. I'd appreciate a review of the implementation as well as of my ...
2
votes
2answers
103 views

LinkedList Stack Implementation

I'm new to the programming and I want to ask a question about my linkedlist stack implementation if it's correct and if it meets the requirements of the linkedlist stack implementation. If there is ...
3
votes
0answers
90 views

Generic stack data structure using linked list and smart pointers

I have decided to rewrite what I did here, following the suggestions to use smart pointers. I will rewrite the other data structures as well using smart pointers where appropriate. I just want to see ...
3
votes
0answers
93 views

All the paths from the root to the leaves

Given a binary tree, return all root-to-leaf paths. Example: -- 1 / \ 2 3 \ 5 Output should be: ["1->2->5", "1->3"] My approach: I walk the branches ...
0
votes
1answer
200 views

Iteratively determine the diameter of a binary tree

Can the performance of the iterative approach be improved? I find that this approach lags behind many recursive options. no recursive answers, please. Baseline recursive approach: takes 52 steps ...
3
votes
2answers
73 views

A Stack Implementation In C

I've written a very simple linked list based stack implementation and was wondering if this is the standard way to do it in C. ...
1
vote
3answers
88 views

Stack implemented with linked list

I have looked at some previous posts and changed my code accordingly. Do you have some suggestions about my current code? Here are my specific questions: What should ...
3
votes
2answers
157 views

Implementing a stack with Java

I am trying to implement a stack with Java: ...
3
votes
3answers
524 views

Threadsafe Stack

Here is a class which I have written to create a simple Stack data structure which can be shared across multiple threads. It's a simple LIFO which has 2 operations: one to push onto the top of the ...
5
votes
3answers
576 views

Abstract Data Type: Stack implementation in C++

While trying to get better at C++ I tried my hand at making a stack implementation. This is the result: ...
4
votes
3answers
661 views

Generic Stack Data Structure Using Linked Lists

I am a mathematician attempting to become proficient with C++. At the moment I am learning about data structures. I am now writing a stack data structure using linked list from scratch. I have tested ...
2
votes
1answer
86 views

Unit Testing- simple stack class

This is my first real attempt at unit testing. This is an area I'm currently focused on improving. I say this because I would greatly appreciate any and every improvement! Stack code: ...
3
votes
0answers
413 views

Find redundant braces in an input string

I am solving interview questions from here. Problem : Write a program to validate if the input string has redundant braces Return 0/1 : ...
3
votes
3answers
1k views

Array-based stack

Description: An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to ...
3
votes
2answers
83 views

Python 2 Stack and LinkedList implementation

I'm currently implementing data structures and algorithms I studed in my college course. This is my implementation of a singly linked list and a stack. I would appreciate some feedback in terms of the ...
4
votes
2answers
2k views

Check if brackets are balanced

...
6
votes
1answer
158 views

Multiple stacks implemented via a linked lists on top of single fixed-size array

Problem Statement Originally, the problem is defined in the book as following: Describe how you could use a single array to implement three stacks. — Cracking the Coding Interview (6th edition) ...
-1
votes
2answers
100 views

List operation while implementing stack

This is a very basic problem of implementing a queue operation using two stacks. Until now there are no issues with the logic but the testing tool report it takes more time than usual. Test case ...
10
votes
4answers
837 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 ...
8
votes
1answer
172 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 ...
5
votes
1answer
467 views

Stack implementation with singly linked list

The following is my implementation of a stack using a linked list. ...
4
votes
1answer
849 views

Stack implementation using a singly linked list

I've implemented a stack using a singly linked list and would like to know if there are any things I could improve on. StackList.h: ...
2
votes
3answers
522 views

A Stack Template

Why you're here: Today I bring you a simple Stack Machine and a Lot of questions. I am trying to gain a little bit more understanding of pointers, memory management and templates and thought a Stack ...
2
votes
2answers
322 views

Converting prefix expressions to infix

Here is my new working code. Man, working with strings in C is tedious and annoying. I had built the algorithm in short time, took hours figuring how to implement it properly in C. Tell me if there ...
1
vote
2answers
743 views

Eight Queens Recursive

I am working on a Java textbook chapter on recursion, and I am to implement an Eight Queens solution using recursion. Though I know that I am to post working code in Code Review, I do believe this ...
14
votes
3answers
1k views

Stack Implementation in C++ with Exceptions

Appreciate your thoughts and feedback... Stack.h ...
0
votes
2answers
112 views

C++ Stack implementation with linked-list

It's proving difficult to find implementations of a Stack with linked list and Im trying to figure out if my implementation is correct/not missing anything obvious. ...
4
votes
3answers
444 views

Stack data structure implementation in Java

I recently made a data structure, stack implementation in Java and I would like feedback on my code to improve it (other than commenting/documenting my code). ...
1
vote
1answer
178 views

Stack calculator in Racket

I have implemented a stack calculator based on this programming task. I wondered if a more experienced racketeer could give me feedback and tell me if I am missing anything in Racket that would enable ...