класс стек на с++

За вознаграждение или нахаляву (если повезёт)

Модераторы: Хыиуду, MOTOCoder, Medved, dr.Jekill

Ответить
Ipse
Сообщения: 1
Зарегистрирован: 26 апр 2007, 23:24

покажите пожалуйста как в классе реализовываются процедуры:
инициализации, Pop, Push.
Absurd
Сообщения: 1228
Зарегистрирован: 26 фев 2004, 13:24
Откуда: Pietari, Venäjä
Контактная информация:

Ipse писал(а):покажите пожалуйста как в классе реализовываются процедуры:
инициализации, Pop, Push.
Например так

[php]

#include <memory>
#include <string>
#include <stdexcept>

template<typename ElType>
class Stack {
struct Node {
Node *_next;
ElType _element;
};
Node *_root;
public:
Stack();
~Stack();
void Push(const ElType& el);
ElType Pop();
bool IsEmpty() const;
};

template<typename ElType>
Stack<ElType>::Stack()
:_root(0)
{}

template<typename ElType>
Stack<ElType>::~Stack()
{
while (!IsEmpty()) {
Pop();
}
}

template<typename ElType>
void Stack<ElType>::Push(const ElType& el)
{
std::auto_ptr<Stack<ElType>::Node> pNode(new Stack<ElType>::Node);
pNode->_next = _root;
pNode->_element = el;
_root = pNode.release();
}

template<typename ElType>
ElType Stack<ElType>::Pop()
{
if (!IsEmpty()) {
std::auto_ptr<Stack<ElType>::Node> pNode(_root);
_root=pNode->_next;
return pNode->_element;
} else {
throw std::logic_error("Stack is epmty");
}
}

template<typename ElType>
bool Stack<ElType>::IsEmpty() const {
return _root == 0;
}
[/php]
2B OR NOT(2B) = FF
Ответить