Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Stack with min element
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code #include <iostream> #include <list> using namespace std; template<class T> class Stack { list<T> elements; list<T> minElements; public: void push(T e) { elements.push_back(e); if (minElements.empty() || e < minElements.back()) minElements.push_back(e); } T pop() { if (elements.empty()) return T(); T tmp = elements.back(); elements.pop_back(); if (tmp == minElements.back()) minElements.pop_back(); return tmp; } T minElement() { if (minElements.empty()) return T(); return minElements.back(); } }; int main() { Stack<int> s; cout << s.minElement() << " " << s.pop() << endl; s.push(5); cout << s.minElement() << endl; s.push(1); cout << s.minElement() << endl; s.push(6); cout << s.minElement() << endl; cout << s.pop() << endl; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.34 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.49 sec
fork mode
|
history
|
discussion
0 0 5 1 1 6