Last active
March 23, 2021 20:16
-
-
Save radiantly/c70bd5a3d16c2d6a6d8de0d2ef55783c to your computer and use it in GitHub Desktop.
Revisions
-
radiantly revised this gist
Mar 23, 2021 . 1 changed file with 55 additions and 81 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,109 +8,83 @@ struct Node { Node(int value) : val{value} {} }; class Stack { std::unique_ptr<Node> top; public: void push(int); std::optional<int> pop(); void display(); void reverse(); }; void Stack::push(int value) { auto tmp = std::make_unique<Node>(value); tmp->next = std::move(top); top = std::move(tmp); } std::optional<int> Stack::pop() { if (!top) return {}; auto tmp = std::move(top); top = std::move(tmp->next); return tmp->val; } void Stack::display() { for (auto ptr = std::cref(top); ptr.get(); ptr = std::cref(ptr.get()->next)) std::cout << ptr.get()->val << " "; std::cout << std::endl; } void Stack::reverse() { std::unique_ptr<Node> pointTo; while (top) { auto nextNode = std::move(top->next); top->next = std::move(pointTo); pointTo = std::move(top); top = std::move(nextNode); } top = std::move(pointTo); } int main() { Stack s; s.pop(); s.reverse(); s.display(); s.push(1); s.push(2); s.display(); s.push(3); s.push(4); s.display(); s.pop(); s.push(5); s.push(8); s.display(); s.reverse(); s.display(); s.pop(); s.pop(); s.display(); s.push(2); s.push(1); s.push(1); s.push(0); s.display(); return 0; } /* 2 1 4 3 2 1 8 5 3 2 1 1 2 3 5 8 3 5 8 0 1 1 2 3 5 8 */ -
radiantly created this gist
Mar 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,116 @@ #include<iostream> #include<memory> #include<functional> struct Node { int val; std::unique_ptr<Node> next; Node(int value) : val{value} {} }; class Queue { std::unique_ptr<Node> front; public: void push(int); std::optional<int> pop(); void display(); void reverse(); }; void Queue::push(int value) { std::function<void(std::unique_ptr<Node>&)> recurse; recurse = [&] (std::unique_ptr<Node>& ptr) { if (ptr) recurse(ptr->next); else ptr = std::make_unique<Node>(value); }; recurse(front); } std::optional<int> Queue::pop() { if (!front) return {}; std::function<int(std::unique_ptr<Node>&)> recurse; recurse = [&] (std::unique_ptr<Node>& ptr) { if (ptr->next) return recurse(ptr->next); auto tmp = std::move(ptr); return tmp->val; }; return recurse(front);; } void Queue::display() { std::function<void(std::unique_ptr<Node>&)> recurse; recurse = [&] (const std::unique_ptr<Node>& ptr) { if (ptr) { std::cout << ptr->val << " "; recurse(ptr->next); } }; recurse(front); std::cout << std::endl; } void Queue::reverse() { // Return if the list contains <= 1 elements if (!front or !front->next) return; std::function<const std::unique_ptr<Node>&(std::unique_ptr<Node>)> recurse; recurse = [&] (std::unique_ptr<Node> ptr) { if (!ptr->next->next) { front = std::move(ptr->next); front->next = std::move(ptr); return std::cref(front->next); } auto& nxtRef = recurse(std::move(ptr->next)); nxtRef->next = std::move(ptr); return std::cref(nxtRef->next); }; recurse(std::move(front)); } int main() { Queue q; q.pop(); q.reverse(); q.display(); q.push(1); q.push(2); q.display(); q.push(3); q.push(4); q.display(); q.pop(); q.push(5); q.push(7); q.display(); q.reverse(); q.display(); q.pop(); q.pop(); q.display(); q.push(1); q.display(); return 0; } /* 1 2 1 2 3 4 1 2 3 5 7 7 5 3 2 1 7 5 3 7 5 3 1 */