간단한 Stack 구현.
#include <iostream>
#include <stdexpect>
#include <memory>
template<typename T>
class Node{
public:
T data; // .
Node<T>* next; // .
template<typename Ty>
Node(const Ty& data_, Node<T>* n=nullptr);
~Node()=default;
};
template<typename T>
template<typename Ty>
Node<T>::Node(const Ty& data_, Node<T>* n)
:data(data_)
next(n)
{
//
}
template<typename T>
class AStack{ // .
private:
unsigned int maxSize; // stack .
Node<T>* top; // .
Node<T>* rear; // .
unsigned int counter; // , stack .
void clear(Node<T>* root)noexcept; // stack .
public:
template<typename Ty, typename = std::enable_if< std::is_unsigned<Ty>::value>::type>
AStack(const Ty& size);
~AStack();
template<typename Ty>
void push(const Ty& value);
const Node<T>& top()const noexcept;
void pop()noexcept;
void clear()noexcept;
};
template<typename T>
template<typename Ty, typename>
AStack<T>::AStack(const Ty& size)
:maxSize(size), //size
top(nullptr), // 。
rear(nullptr),
counter(0)
{
//
}
template<typename T>
AStack<T>::~AStack()
{
this->clear();
}
template<typename T>
void AStack<T>::clear(Node<T>* ptr)noexcept
{
if(ptr == nullptr){
return;
}
if(ptr != nullptr){
this->clear(ptr->next);
}
delete ptr;
ptr = nullptr;
}
template<typename T>
const Node<T>& AStack<T>::top()const noexcept // stack .
{
return (this->rear)->data;
}
template<typename T>
void AStack<T>::pop()noexcept // stack .
{
Node<T>* headNext = (this->head)->next;
delete this->head;
this->head = headNext;
headNext = nullptr;
}
template<typename T>
void AStack<T>::push(const Ty& value) // stack.
{
if(this->head == nullptr){
++(this->counter); //this->counter = 1;
this->head = new Node<Ty>(value);
this->rear = (this->head)->next;
}else{
if(this->counter <= this->maxSize)
{
this->rear = new Node<Ty>(value);
this->rear = (this->rear)->next;
}else{
throw std::runtime_error("Can not bigger than the maxSize!");
}
}
}
template<typename T>
void AStack<T>::clear()noexcept
{
this->clear(this->head);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.