CafeM0ca

[자료구조]Deque(C++ std::list) 본문

Programming/자료구조|알고리즘

[자료구조]Deque(C++ std::list)

M0ca 2018. 6. 8. 15:32
반응형
std::list를 사용해 구현한 deque

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstring>
#include <list>
using namespace std;
 
 
template <typename T>
class Deque
{
private:
    list<T> element;
public:
    Deque(){} 
   ~Deque(){}  
    void push_front(T& elmnt)
    { 
       element.push_front(elmnt); 
   }   
    void push_back(T& elmnt)
    {    
        element.push_back(elmnt);  
    }    
    auto empty()
    {   
        return element.empty();
    }
    auto pop_front()
    {
        if(!element.empty()) 
        { 
           auto tmp = element.front(); 
           element.pop_front();
            return tmp;
        }    
        else    
        return -1;
    }    
    auto pop_back()   
    { 
       if(!element.empty())
       {
            auto tmp = element.back();
            element.pop_back();
            return tmp;
       }
       else
            return -1;  
    } 
    auto front()
    {
        if(!element.empty())
            return element.front();
        else
            return -1;
    }
    auto back()
    {
         if(!element.empty()) 
           return element.back();
        else
            return -1;
    }
    auto size()
    {
        return element.size();
    }
};
int main(){ 
   Deque<int> deque;
   int n;
   cin >> n;
   char s[100];
   for(auto i = 0; i < n; i++){
        cin >> s;
        if(!strcmp(s,"push_back")){
            int n;
            cin >> n;
            deque.push_back(n);
        }
        else if(!strcmp(s,"push_front")){
            int n;
            cin >> n;
            deque.push_front(n);
        }
        else if(!strcmp(s,"pop_front"))
            cout << deque.pop_front() << endl;
        else if(!strcmp(s,"pop_back"))
            cout << deque.pop_back() << endl;
        else if(!strcmp(s,"front"))
            cout << deque.front() << endl;
        else if(!strcmp(s,"back"))
            cout << deque.back() << endl;
        else if(!strcmp(s,"size"))
            cout << deque.size() << endl;
        else if(!strcmp(s,"empty"))
            cout << deque.empty() << endl;
    }
    return 0;
}
 
 
cs







반응형
Comments