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.
380
questions
0
votes
1answer
32 views
Generic stack implementation in C
I haven't separated the stack code into its own header and source yet, this is just a proof of concept for now. Haven't chosen better function names yet because I ultimately want to integrate this ...
1
vote
0answers
37 views
Monotonic stack - complexity analysis
I tried following the official solution of LC975 (https://leetcode.com/problems/odd-even-jump/), using a monotonic stack - i.e., I reimplemented their solution (Approach 1) from python in C++.
TLDR ...
2
votes
1answer
75 views
C++ implementation of a Stack with dynamic C-style array
I implemented a Stack in C++ using a dynamic C-style array. I tried sticking to the most important functions a Stack has to have to be usable. This is meant to only be for integers. I appreciate any ...
6
votes
2answers
58 views
Error Code Stack Trace and Propagation Library in C
I've been programming in Go and I enjoy how easy it is to create descriptive errors and propagate them up the call stack. I wanted that sort of ease and consistency in C so I created a small error ...
4
votes
1answer
56 views
Assemble network packet on stack for iovec
The recurring problem to assemble a network packet out of payload, sequence number, header, and other misc. information is mostly solved either on the heap (e.g. appending to a ...
2
votes
2answers
78 views
Dynamic Array Based Stack in C
I wrote a dynamic stack in C that uses an array as a structure. I tried to maintain O(1) for push and pop and believe I have done so. I want to know what can be written in a cleaner way and if there ...
3
votes
2answers
111 views
Convert infix regular expression notation to postfix
This is a small part of a larger program for implementing a limited syntax regular expression constructor using Ken Thompson's construction algorithm. Converting to postfix before the regular ...
7
votes
1answer
1k views
Optimizing Efficiency in a Stack
I'm solving a problem on HackerRank where I'm required to implement a simple stack.
It's passing all the tests except for the last 4 where it fails due to surpassing the time constraint of 10s. ...
2
votes
1answer
118 views
Kotlin Stack using an ArrayList to compare and remove elements
Is this a performant strategy to implement a stack in Kotlin to compare and delete values?
Expect
Create a stack in order to compare an array of asteroids (ASTs) and handle collisions (deletions), ...
10
votes
2answers
573 views
An attempt at a toy Vm
I usually use c++ so it may not be best practice for c.
This is a stack based toy vm and as a result it is very primitive, and it has no bitwise
instructions
64k might be a bit overkill for a toy vm.
...
1
vote
1answer
59 views
Stack Implementation Using Linked Lists
I have tried Implementing Stacks Using Linked Lists, and would like some tips and tricks regarding optimizations or ways to simplify/compact processes.
It has 3 functions, Push, Pop, and Peek (For ...
1
vote
0answers
19 views
regex on a log file utilizing stacks and queue's in Java
This would be for a MUD client, which has elements of a sort of telnet bot.
Here, there's ...
-3
votes
1answer
46 views
What's wrong with this stack implementation? [closed]
My CS teacher sent me the following code for implementing the stack. I've been programming for a few years in higher-level languages, but have rather weak experience with C++. I mean, this code just ...
9
votes
4answers
2k views
Simple Stack implementation in C++
I've recently started teaching myself basic C++ and decided to implement a simple stack with pointers.
...
1
vote
1answer
96 views
Concurrent Queue with enqueue and dequeue using Stack as underlying data structure
This is a mix of data structure and multi threading concept based question. (This is only for understanding and learning purpose) The language for the solution : Java
A Queue with FIFO behavior needs ...
2
votes
2answers
64 views
Queue By Using Stack
I have implemented a queue by using a stack data structure. How could I improve it?
...
7
votes
3answers
168 views
Dynamic stack implementation in C
I've been studying C for a while, as a programmer coming from a C++ background, I'm used to the standard library, STL, etc., and I quickly realized that I need some kind of a containers library/data ...
1
vote
4answers
463 views
Exercise: Design a Stack
Exercise: Design a Stack
A Stack is a data structure for storing a list of elements in a LIFO
(last in, first out) fashion. Design a class called Stack with three
methods.
...
3
votes
2answers
189 views
3
votes
3answers
526 views
Stack implementation using linked-list
typedef struct Stack {
char v;
int x;
int y;
struct Stack* next;
} Stack;
Above is my stack struct.
...
4
votes
1answer
63 views
C pointer based growable stack rev2
Following on from my previous question: C pointer based growable stack
I have made some improvements (hopefully!) based on the very helpful comments and suggestions.
I have checked that the stack can ...
7
votes
3answers
1k views
C pointer based growable stack
This is my first attempt at writing a C program, it a generic stack that can grow accordingly. It appears to work correctly, however I am worried that is just a fluke and I could be doing something ...
4
votes
1answer
201 views
Codility Fish (queue) programming challenge
Today I have attempted a Codility exercise "Fish" (https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/), and I have to say I found it to be:
1) Not necessarily "easy" as implied by ...
2
votes
2answers
82 views
Designing a stack such that getMin() is both constant time and constant space
I'm attempting the SpecialStack problem on GeeksforGeeks.
Design a data-structure SpecialStack (using the STL of stack) that
supports all the stack operations like push(), pop(), isEmpty(),
...
1
vote
1answer
96 views
Syntax Checker C++ Data Structure - Stack
Syntax Checker is a very common question of c++ programming using the data structure stack. However, this is the advanced level base on this question I found in a reference book. Here is the question:
...
16
votes
7answers
3k views
Stack data structure in Python 3
I have implemented a stack in Python 3, and it works well. If anything needs to be improved, I would appreciate the criticism.
...
6
votes
1answer
758 views
Doubly Linked List using Stack in c++
Just started learning/relearning data structures for the semester exams. Most of the examples found online are too cluttered or bloated.
For myself, I implemented it from theory I learned.
It works....
1
vote
1answer
1k views
Codility's stonewall 100% solution - different paths such as if,else if,etc, do the same thing
Here is the task description for the codility's stonewall problem. I would like to get a feedback on my code, and I specify at the bottom what the title states. (I had to make the figure by hand, ...
3
votes
1answer
123 views
Codility voracious fish are moving along a river, solution in C#
This is the task's description. My code gets 100% task score. Is it good code yet? (I think I'm getting a lot better, since I started solving this codility's exercises and posting here, but I always ...
1
vote
0answers
182 views
LeetCode: Daily Temperatures C#
https://leetcode.com/problems/daily-temperatures/
Given a list of daily temperatures T, return a list such that, for
each day in the input, tells you how many days you would have to wait
until ...
2
votes
2answers
108 views
Creating a stack class in C++
Create a class called Stack for storing integers. The data members are
an integer array for storing the integers and an integer for storing
the top of stack (<...
5
votes
1answer
66 views
An alternative algorithm for sorting a stack using only one additional stack
From "Cracking the Coding Interview":
Write a program to sort a stack in ascending order (with biggest items on top). You may use at most one additional stack to hold items, but you may not copy ...
6
votes
5answers
130 views
Safe stack abstraction
Here is the stack abstraction that I've written. I've designed it to be safe and without undefined behavior.
I use ugly_cast to discourage casting and to make ...
13
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 ...
4
votes
2answers
88 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
473 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
82 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. ...
11
votes
3answers
3k 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
800 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
4k 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 ...
5
votes
2answers
386 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
62 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.
...
5
votes
2answers
560 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
288 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
232 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
586 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
799 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
82 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 ...
25
votes
3answers
6k views
Leetcode: Valid parentheses
https://leetcode.com/problems/valid-parentheses/
Given a string containing just the characters (, ), ...
1
vote
1answer
332 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 ...