栈队列堆
棧:一種只能在一端進行插入和刪除的特殊線性表,按照先進后出的方式組織數據,先進入的數據被壓入棧底,最后的數據被壓入棧頂,需要讀取數據時從棧頂開始彈出數據
隊列:一種只能在一端進行數據的插入及另一端進行數據的刪除的特殊線性表,按照先進先出的方式組織數據
堆:N個元素{k1, k2, k3, k4, k5, k6 ... kn}組成的集合,若滿足下列關系之一則可稱之為堆:
小根堆的圖示如下:
?
棧的簡單實現:
// Author: Waihui Zheng // 棧 #include <iostream> #include <assert.h>namespace my_space {template <typename T> class stack { public:stack() : _top_index(0) {}~stack() {}void push(const T& value) {assert(_top_index < MAXN);_array[_top_index++] = value;}void pop() {assert(_top_index > 0);--_top_index;}T& top() {assert(_top_index > 0);return _array[_top_index - 1];}const T& top() const {assert(_top_index > 0);return _array[_top_index - 1];}bool empty() const {return _top_index == 0;}bool full() const {return _top_index == MAXN;}private:static const int MAXN = 1000;T _array[MAXN];int _top_index; };}int main() {my_space::stack<int> st;st.push(7);st.push(8);st.push(9);while (!st.empty()) {std::cout << st.top() << " ";st.pop();}std::cout << std::endl;return 0; }?
隊列的簡單實現:
// Author: Waihui Zheng // 隊列 #include <iostream> #include <assert.h>namespace my_space {template <typename T> class queue { public:queue() : _front_index(0), _last_index(0) {}~queue() {}void push(const T& value) {assert(!full());_array[_front_index] = value;_front_index = (_front_index + 1) % MAXN;}void pop() {assert(!empty());_last_index = (_last_index + 1) % MAXN;}T& front() {assert(!empty());int index = _front_index - 1;if (index < 0) {index += MAXN;}return _array[index];}const T& front() const {assert(!empty());int index = _front_index - 1;if (index < 0) {index += MAXN;}return _array[index];}T& back() {assert(!empty());return _array[_last_index];}const T& back() const {assert(!empty());return _array[_last_index];}bool empty() const {return _front_index == _last_index;}bool full() const {return (_front_index + 1) % MAXN == _last_index;}private:static const int MAXN = 1000;T _array[MAXN];int _front_index; // 前方插入int _last_index; // 后方刪除 };}int main() {my_space::queue<int> q;q.push(7);q.push(8);q.push(9);while (!q.empty()) {std::cout << q.back() << " ";q.pop();}std::cout << std::endl;return 0; }堆的簡單實現:
// Author: Waihui Zheng // 堆 // N個元素{k1, k2, k3, k4, k5, k6 ... kn}組成的集合,若滿足下列關系之一則可稱之為堆 // 1. ki <= k2i 且 ki <= k2i+1, (i = 1, 2, 3 ... n/2) // 2. ki >= k2i 且 ki >= k2i+1, (i = 1, 2, 3 ... n/2) #include <iostream> #include <algorithm> #include <assert.h>namespace my_space {template <typename T> class MaxHeap { public:MaxHeap() : _length(0) {}~MaxHeap() {}// 插入操作,則是將將元素追加于數據尾部,然后向上調堆void push(const T& value) {// 下標從1開始,最多只能存放MAXN - 1個元素assert(_length < MAXN - 1);_array[++_length] = value;adjust_heap_up(_array, _length);}// 堆的刪除操作,則是將第一個元素刪除,將最后一個元素放于第一個位置,然后向下調堆void pop() {assert(_length > 0);std::swap(_array[1], _array[_length]);--_length;if (_length <= 0) {return ;}adjust_heap_down(_array, 1, _length);}void print() {for (int i = 1; i <= _length; ++i) {std::cout << _array[i] << " ";}std::cout << std::endl;}// 便于理解,依然元素小標從1開始void heap_sort(T* array, int length) {for (int root = length / 2; root >= 1; --root) {adjust_heap_down(array, root, length);}for (int last_index = length; last_index > 1;) {std::swap(array[1], array[last_index]);--last_index;adjust_heap_down(array, 1, last_index);}}private:// 向下調堆,用于刪除操作void adjust_heap_down(T* data, int root_index, int last_index) {T temp = data[root_index];int cur_index = root_index * 2;while (cur_index <= last_index) {// 選取子節點鍵值最大的節點if (cur_index < last_index && data[cur_index + 1] > data[cur_index]) {++cur_index;}// 若當前節點的鍵值 小于等于 父節點的鍵值,說明調堆成功并結束if (data[cur_index] <= temp) {break;}// 當前節點鍵值大于父節點,當前鍵值上移data[root_index] = data[cur_index];root_index = cur_index;cur_index = 2 * root_index;}data[root_index] = temp;}// 向上調堆,用于插入操作,元素下標從1開始void adjust_heap_up(T* data, int value_index) {T temp = data[value_index];for (int cur_index = value_index / 2; cur_index >= 1; value_index = cur_index, cur_index /= 2) {if (data[cur_index] >= temp) {break;}data[value_index] = data[cur_index];}data[value_index] = temp;}private:static const int MAXN = 1000;T _array[MAXN]; // 為便于理解,數組第一個元素從下標1開始int _length; };}int main() {my_space::MaxHeap<int> max_heap;int a[] = {88888, 3, 1, 5, 7, 10, 2};max_heap.heap_sort(a, sizeof(a) / sizeof(int) - 1);for (int i = 0; i < sizeof(a) / sizeof(int); ++i) {std::cout << a[i] << " ";}std::cout << std::endl;max_heap.push(5);max_heap.push(10);max_heap.push(13);max_heap.push(4);max_heap.push(18);max_heap.print();max_heap.pop();max_heap.print();return 0; }?
轉載于:https://www.cnblogs.com/bugfly/p/5192012.html
總結
- 上一篇: Delphi 从PaintBox拷贝一部
- 下一篇: 算法-动态规划(01背包)