Stack: Difference between revisions
Content added Content deleted
Line 8: | Line 8: | ||
'''Library:''' STL |
'''Library:''' STL |
||
#include <stack> |
|||
==[[C plus plus|C++]]== |
|||
'''Library:''' STL |
|||
#include <stack> |
#include <stack> |
||
template <class T, class Sequence = deque<T> > |
template <class T, class Sequence = deque<T> > |
||
class stack { |
class stack { |
||
friend bool operator== (const stack&, const stack&); |
friend bool operator== (const stack&, const stack&); |
||
Line 27: | Line 19: | ||
typedef typename Sequence::size_type size_type; |
typedef typename Sequence::size_type size_type; |
||
typedef Sequence container_type; |
typedef Sequence container_type; |
||
typedef typename Sequence::reference reference; |
typedef typename Sequence::reference reference; |
||
typedef typename Sequence::const_reference const_reference; |
typedef typename Sequence::const_reference const_reference; |
||
Line 35: | Line 26: | ||
stack() : seq() {} |
stack() : seq() {} |
||
explicit stack(const Sequence& s0) : seq(s0) {} |
explicit stack(const Sequence& s0) : seq(s0) {} |
||
bool empty() const { return seq.empty(); } |
bool empty() const { return seq.empty(); } |
||
size_type size() const { return seq.size(); } |
size_type size() const { return seq.size(); } |
||
Line 49: | Line 39: | ||
return x.seq == y.seq; |
return x.seq == y.seq; |
||
} |
} |
||
template <class T, class Sequence> |
template <class T, class Sequence> |
||
bool operator<(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
bool operator<(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
||
Line 61: | Line 50: | ||
return !(x == y); |
return !(x == y); |
||
} |
} |
||
template <class T, class Sequence> |
template <class T, class Sequence> |
||
bool operator>(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
bool operator>(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
||
Line 67: | Line 55: | ||
return y < x; |
return y < x; |
||
} |
} |
||
template <class T, class Sequence> |
template <class T, class Sequence> |
||
bool operator<=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
bool operator<=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
||
Line 73: | Line 60: | ||
return !(y < x); |
return !(y < x); |
||
} |
} |
||
template <class T, class Sequence> |
template <class T, class Sequence> |
||
bool operator>=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
bool operator>=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) |
Revision as of 03:18, 24 February 2007
Stack
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
C++
Library: STL
#include <stack>
template <class T, class Sequence = deque<T> > class stack { friend bool operator== (const stack&, const stack&); friend bool operator< (const stack&, const stack&); public: typedef typename Sequence::value_type value_type; typedef typename Sequence::size_type size_type; typedef Sequence container_type; typedef typename Sequence::reference reference; typedef typename Sequence::const_reference const_reference; protected: Sequence seq; public: stack() : seq() {} explicit stack(const Sequence& s0) : seq(s0) {} bool empty() const { return seq.empty(); } size_type size() const { return seq.size(); } reference top() { return seq.back(); } const_reference top() const { return seq.back(); } void push(const value_type& x) { seq.push_back(x); } void pop() { seq.pop_back(); } };
template <class T, class Sequence> bool operator==(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return x.seq == y.seq; } template <class T, class Sequence> bool operator<(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return x.seq < y.seq; }
template <class T, class Sequence> bool operator!=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return !(x == y); } template <class T, class Sequence> bool operator>(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return y < x; } template <class T, class Sequence> bool operator<=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return !(y < x); } template <class T, class Sequence> bool operator>=(const stack<T,Sequence>& x, const stack<T,Sequence>& y) { return !(x < y); }
Java
public class Stack { private Node first = null; public boolean isEmpty { return (first == null); } public Object Pop() { if (first == null) throw new Exception("Can't Pop from an empty Stack."); else { Object temp = first.Value; first = first.Next; return temp; } } public void Push(Object o) { first = new Node(o, first); } class Node { public Node Next; public Object Value; public Node(Object value) { this(value, null); } public Node(Object value, Node next) { Next = next; Value = value; } } }
Compiler: JDK 1.5 with Generics
public class Stack<T> { private Node first = null; public boolean isEmpty { return (first == null); } public T Pop() { if (first == null) throw new Exception("Can't Pop from an empty Stack."); else { T temp = first.Value; first = first.Next; return temp; } } public void Push(T o) { first = new Node(o, first); } class Node { public Node Next; public T Value; public Node(T value) { this(value, null); } public Node(T value, Node next) { Next = next; Value = value; } } }
C#
public class Stack { private Node first = null; public bool Empty { get { return (first == null); } } public object Pop() { if (first == null) throw new Exception("Can't Pop from an empty Stack."); else { object temp = first.Value; first = first.Next; return temp; } } public void Push(object o) { first = new Node(o, first); } class Node { public Node Next; public object Value; public Node(object value): this(value, null) {} public Node(object value, Node next) { Next = next; Value = value; } } }