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
6
votes
1answer
127 views

Calculator in C - Parsing arithmetic expression using the shunting-yard algorithm

This is a simple arithmetic calculator, which parses mathematical expressions specified in infix notation using the shunting-yard algorithm. This is one of my personal projects and I would love to ...
0
votes
0answers
32 views

UVA 101 - “The Blocks Problem”

I have been solving the problem from UVA 101. In this problem, there will be at most 24 position in a row at beginning, and this array is zero-indexed, and for each ...
1
vote
1answer
40 views

Stack of integers in C

I'm learning C, and currently I follows C99 standard. Here is my implementation of integer-stack, which is just a stack you can do: push, pop, and print the content ...
3
votes
1answer
55 views

Python Console App | Is there a better way to go back a page?

I have a system for going back to a previously-visited page in a Python console application; it works well enough, but I feel that there could be a prettier way of doing it. Does anyone have any ...
1
vote
0answers
62 views

Add missing left parentheses in Java [closed]

I wanted to write a program that takes from standard input an expression without left parentheses and prints the equivalent infix expression with the parentheses inserted. For example, given the input ...
3
votes
2answers
65 views

A toy example of the concurrent stack via atomic variables and CAS pattern

I've written a toy example of the concurrent stack which has only three functions push(), peek(), and ...
0
votes
1answer
52 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
43 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
80 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
68 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
60 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
274 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
188 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
2k 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
189 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
577 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
69 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
3k 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
199 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
186 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
604 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
470 views

Basic 'char' Stack implementation in C

...
3
votes
3answers
529 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
65 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
262 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
84 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
103 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
1k 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
2k 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
151 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
204 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
236 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
67 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
131 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
89 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
614 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
84 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
878 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
536 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
63 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
605 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
373 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
2 3 4 5
8