무민은귀여워

stack 구현 본문

IT/기타

stack 구현

moomini 2021. 5. 17. 23:36
반응형
#include<iostream>
using namespace std;
 
template<typename T>
class Node 
{
public:
    Node()
        : Next(nullptr)
    {
 
    }
 
    T Value;
    Node<T>* Next;
};
 
template<typename T>
class MyQueue
{
public:
    MyQueue()
    {
        Last = new Node<T>();
        First = Last;
        First->Next = Last;
    }
 
    void push(const T& in)
    {
        auto Temp = new Node<T>();
        *Last = Node<T>(in);
        Last->Next = Temp;
        Last = Temp;
    }
 
    void push(T&& in)
    {
        auto Temp = new Node<T>();
        Last->Value = in;
        Last->Next = Temp;
        Last = Temp;
    }
 
    T pop()
    {
        T ret = First->Value;
        Node<T>* Temp = First->Next;
        delete First;
        First = Temp;
        return ret;
    }
 
    bool empty()
    {
        return First == Last;
    }
 
    unsigned int size()
    {
        int ret = 0;
        Node<T>* Temp = First;
 
        while (Temp != Last)
        {
            Temp = Temp->Next;
            ++ret;
        }
 
        return ret;
    }
 
public:
    Node<T>* First;
    Node<T>* Last;
};
 
template<typename T>
class MyStack
{
public:
    MyStack()
        :top(-1),
        capecitySize(5),
        Data(nullptr)
    {
 
    }
 
    void push(const T& in)
    {
        if (Data == nullptr)
        {
            Data = new T[capecitySize];
        }
 
        if (top+1 >= capecitySize)
        {
            T* Temp = Data;
            capecitySize *= 2;
            Data = new T[capecitySize];
            memcpy(Data, Temp, sizeof(T) * top);
            delete[] Temp;
        }
 
        Data[++top] = in;
    }
 
    void push(T&& in)
    {
        if (Data == nullptr)
        {
            Data = new T[capecitySize];
        }
 
        if (top+1 >= capecitySize)
        {
            T* Temp = Data;
            capecitySize *= 2;
            Data = new T[capecitySize];
            memcpy(Data, Temp, sizeof(T) * top);
            delete[] Temp;
        }
 
        Data[++top] = in;
    }
 
    T pop()
    {
        return Data[top--];
    }
 
    bool empty()
    {
        return top == -1;
    }
 
    int size()
    {
        return top + 1;
    }
 
public:
    int capecitySize;
    int top;
    T* Data;
};
 
int main()
{
    MyQueue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
 
    cout << "size : " << q.size() << endl;
    cout << "empty? :" << q.empty() << endl;
    for (int i = 0; i < 3; i++)
    {
        cout << q.pop() << endl;
    }
    cout << "empty? :" << q.empty() << endl;
    cout << "ddd" << endl;
 
    MyStack<int> s;
    s.push(1);
    s.push(2);
    s.push(3);
    cout << "empty? :" << s.empty() << endl;
    cout << "size : " << s.size() << endl;
 
    for (int i = 0; i < 3; i++)
    {
        cout << s.pop() << endl;
    }
    cout << "empty? :" << s.empty() << endl;
    return 0;
}
반응형

'IT > 기타' 카테고리의 다른 글

짐벌락(gimbal lock)과 사원수(쿼터니언, quaternion)  (1) 2021.05.18
memo) redis 패턴으로 데이터 삭제  (0) 2021.05.17
list 구현  (0) 2021.05.17
vector 구현  (0) 2021.05.17
xcode 11에서 cocos2d-x v3 빌드 실패 시 해결 방법  (0) 2020.02.25
Comments