-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_deque.h
173 lines (145 loc) · 4.47 KB
/
c_deque.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef C_DEQUE_H
#define C_DEQUE_H
// A circular, double-linked list in c
#include <stdbool.h>
#include <stdint.h>
// A pure-c implementation could hide the node definition, but to use
// the linked-list iterator, we need to have access to the raw node
// pointers
typedef struct deque_node deque_node;
struct deque_node
{
deque_node* next;
deque_node* prev;
int payload;
};
deque_node* init_deque_node_in_place(deque_node* node, int value);
deque_node* create_deque_node_on_heap(int value);
// Out implementation will use a always present sentinal node, so we
// need a constructor/destructor
typedef struct dl_deque dl_deque;
struct dl_deque
{
deque_node *sentinal;
};
dl_deque* init_deque_in_place(dl_deque* deque);
dl_deque* create_deque_on_heap();
void reap_deque(dl_deque* deque);
// Container interface
void clear_deque(dl_deque* deque);
bool deque_empty(dl_deque* deque);
void deque_push_back(int value, dl_deque* deque);
void deque_push_front(int value, dl_deque* deque);
int deque_pop_back(dl_deque* deque);
int deque_pop_front(dl_deque* deque);
int deque_peek_back(dl_deque* deque);
int deque_peek_front(dl_deque* deque);
/* // A class to build a circular list of dl_nodes using a sentinal */
/* // */
/* // The list that's built up is a wholely C data structure (NB: */
/* // malloc/free for allocation control). I'm just using a class here to */
/* // lump all the scaffolding together. */
/* class dl_circular */
/* { */
/* public: */
/* using iterator = LinkedListIterator<dl_node>; */
/* using reverse_iterator = LinkedListIterator<dl_node, default_prev<dl_node>>; */
/* dl_circular() = default; */
/* dl_circular(const dl_circular&) = delete; */
/* dl_circular& operator=(const dl_circular&) = delete; */
/* dl_circular(dl_circular&&) = default; */
/* dl_circular& operator=(dl_circular&&) = default; */
/* ~dl_circular() */
/* { */
/* if (_head == nullptr) */
/* return; */
/* while (_head->next != _head) */
/* { */
/* pop_front(); */
/* } */
/* destruct_with_free(_head); */
/* } */
/* void pop_back() */
/* { */
/* if (_head == nullptr) */
/* return; */
/* const auto first_node = _head->prev; */
/* const auto second_node = first_node->prev; */
/* _head->prev = second_node; */
/* second_node->next = _head; */
/* destruct_with_free(first_node); */
/* } */
/* void pop_front() */
/* { */
/* if (_head == nullptr) */
/* return; */
/* const auto first_node = _head->next; */
/* const auto second_node = first_node->next; */
/* _head->next = second_node; */
/* second_node->prev = _head; */
/* destruct_with_free(first_node); */
/* } */
/* void push_back(int payload) */
/* { */
/* if (_head == nullptr) */
/* init(); */
/* const auto old_back = _head->prev->prev; */
/* const auto new_node = construct_with_malloc(_head, old_back, payload); */
/* old_back->next = new_node; */
/* _head->prev = new_node; */
/* } */
/* void push_front(int payload) */
/* { */
/* if (_head == nullptr) */
/* init(); */
/* const auto old_front = _head->next->next; */
/* const auto new_node = construct_with_malloc(old_front, _head, payload); */
/* old_front->prev = new_node; */
/* _head->next = new_node; */
/* } */
/* iterator begin() */
/* { */
/* if (_head==nullptr) */
/* init(); */
/* return iterator(_head->next); */
/* } */
/* iterator end() */
/* { */
/* if (_head == nullptr) */
/* init(); */
/* return iterator(_head); */
/* } */
/* reverse_iterator rbegin() */
/* { */
/* if (_head == nullptr) */
/* init(); */
/* return reverse_iterator(_head->prev); */
/* } */
/* reverse_iterator rend() */
/* { */
/* if (_head == nullptr) */
/* init(); */
/* return reverse_iterator(_head); */
/* } */
/* private: */
/* void init() */
/* { */
/* _head = construct_with_malloc(nullptr, nullptr, 0xDEADBEEF); */
/* _head->next = _head->prev = _head; */
/* } */
/* dl_node* construct_with_malloc(dl_node* next, dl_node* prev, int payload) */
/* { */
/* const auto buffer = calloc(sizeof(dl_node), 1); */
/* dl_node* ptr = new (buffer) dl_node(); */
/* ptr->next = next; */
/* ptr->prev = prev; */
/* ptr->payload = payload; */
/* return ptr; */
/* } */
/* void destruct_with_free(dl_node* ptr) */
/* { */
/* free(ptr); */
/* } */
/* dl_node* _head = nullptr; */
/* }; */
#endif//C_DEQUE_H