Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "Stack.h"
- template <class T>
- class StackList : public Stack<T>
- {
- public:
- StackList();
- StackList(const StackList<T>& src);
- virtual ~StackList() { clear(); }
- void push(const T& e);
- void push_front(T data);
- const T& pop();
- bool isEmpty() { return top_ == 0; }
- void clear();
- int getSize();
- private:
- struct Node
- {
- Node* next_;
- Node* prev_;
- T value_;
- Node(T data = T(), Node* next = nullptr, Node* prev = nullptr)
- {
- this->value_ = data;
- this->next_ = next;
- this->prev_ = prev;
- }
- };
- int size_;
- Node* head_;
- int top_ = 0;
- };
- //конструктор
- template <class T>
- StackList<T>::StackList()
- {
- size_ = 0;
- head_ = nullptr;
- top_ = 0;
- }
- //конструктор копирования
- template<class T>
- StackList<T>::StackList(const StackList<T>& src)
- {
- size_ = src.size_;
- head_ = new Node(src.head_->value_, nullptr, nullptr);
- Node* unit1 = head_;
- Node* unit2 = src.head_;
- for (int i = 1; i < size_; i++)
- {
- unit2 = unit2->next_;
- unit1->next_ = new Node(unit2->value_, nullptr, unit1);
- unit1 = unit1->next_;
- }
- top_ = src.top_;
- }
- template<class T>
- void StackList<T>::push(const T& e)
- {
- top_++;
- push_front(e);
- }
- template<class T>
- void StackList<T>::push_front(T data)
- {
- head_ = new Node(data, head_);
- size_++;
- }
- template<class T>
- const T& StackList<T>::pop()
- {
- if (top_ == 0)
- {
- throw StackUnderflow();
- }
- Node* temp = head_;
- head_ = head_->next_;
- size_--;
- top_--;
- T result = temp->value_;
- delete temp;
- return result;
- }
- template<class T>
- void StackList<T>::clear()
- {
- while (size_)
- {
- Node* unit = head_;
- head_ = head_->next_;
- delete unit;
- size_--;;
- }
- }
- template<class T>
- int StackList<T>::getSize()
- {
- return size_;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement