-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccf0e42
commit 20840e3
Showing
7 changed files
with
52 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,21 @@ | ||
#include "stack.hpp" | ||
#include <memory> | ||
#include <stdexcept> | ||
|
||
void Stack::Push(int value) { | ||
std::shared_ptr<Node> p(new Node(value)); | ||
if (top == nullptr) { | ||
top = p; | ||
} | ||
if (top != nullptr) { | ||
p->next = top; | ||
} | ||
top = p; | ||
} | ||
#include <algorithm> | ||
|
||
void Stack::Push(int value) { data_.push(value); } | ||
|
||
int Stack::Pop() { | ||
if (top == nullptr) { | ||
throw std::logic_error("Stack Underflow"); | ||
} | ||
int val = top->value; | ||
top = top->next; | ||
return val; | ||
auto result = data_.top(); | ||
data_.pop(); | ||
return result; | ||
} | ||
|
||
void MinStack::Push(int value) { | ||
if (st1_.top == nullptr) { | ||
st1_.Push(value); | ||
st2_.Push(value); | ||
return; | ||
} | ||
|
||
if (st1_.top->value > value) { | ||
st2_.Push(value); | ||
} else { | ||
st1_.Push(value); | ||
st2_.Push(st2_.top->value); | ||
} | ||
} | ||
void MinStack::Push(int value) { data_.push_back(value); } | ||
|
||
int MinStack::Pop() { | ||
if (st1_.top == nullptr) { | ||
throw std::logic_error("Stack Underflow"); | ||
} | ||
|
||
int val = st1_.top->value; | ||
st1_.top = st1_.top->next; | ||
st2_.top = st2_.top->next; | ||
return val; | ||
auto result = data_.back(); | ||
data_.pop_back(); | ||
return result; | ||
} | ||
|
||
int MinStack::GetMin() { return st2_.top->value; } | ||
int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,23 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <memory> | ||
|
||
struct Node { | ||
int value; | ||
std::shared_ptr<Node> next; | ||
Node(int value) : value(value) {} | ||
}; | ||
#include <stack> | ||
#include <vector> | ||
|
||
class Stack { | ||
public: | ||
Stack() { top = nullptr; } | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
std::shared_ptr<Node> top; | ||
|
||
private: | ||
std::stack<int> data_; | ||
}; | ||
|
||
class MinStack { | ||
public: | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
|
||
private: | ||
Stack st1_; | ||
Stack st2_; | ||
private: | ||
std::vector<int> data_; | ||
}; |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#include "heap.hpp" | ||
#include <iostream> | ||
|
||
int main() { return 0; } |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,6 @@ | ||
#include "heap.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(heap, Simple) { | ||
ASSERT_EQ(Find_Minimum(std::vector<int>{1, 3, 5, 6, 2, 4}), 1); | ||
ASSERT_EQ(Find_Minimum(std::vector<int>{11, 37, 55, 34, -6, 0, 4}), -6); | ||
ASSERT_EQ(Heap_Ready(std::vector<int>{4, 1, 3, 2, 5}), | ||
(std::vector<int>{1, 2, 3, 4, 5})); | ||
ASSERT_EQ(Heap_Ready(std::vector<int>{3, 1, 5, 6, 2, 4}), | ||
(std::vector<int>{1, 2, 4, 6, 3, 5})); | ||
Heap heap; | ||
heap.Build_heap(std::vector<int>{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4, | ||
758, -579, -4, 0}); | ||
ASSERT_EQ(heap.Find_Min(), -579); | ||
heap.Insert(23); | ||
heap.Insert(-1000); | ||
ASSERT_EQ(heap.Extract_Min(), -1000); | ||
ASSERT_EQ(heap.Find_Min(), -579); | ||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,6 @@ | ||
|
||
#include "sort.hpp" | ||
#include <gtest/gtest.h> | ||
|
||
TEST(Quick_Sort, Simple) { | ||
std::vector<int> vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; | ||
ASSERT_EQ(Quick_Sort(vec1, 0, 9), | ||
(std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); | ||
std::vector<int> vec2{10, 90, 30, 80, 60, 50}; | ||
ASSERT_EQ(Quick_Sort(vec2, 0, 5), | ||
(std::vector<int>{10, 30, 50, 60, 80, 90})); | ||
std::vector<int> vec3{12, 43, 15, 26, -1233, 346, 1325, | ||
-56, -12, 78, 0, 3345, -34}; | ||
ASSERT_EQ(Quick_Sort(vec3, 0, 12), | ||
(std::vector<int>{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346, | ||
1325, 3345})); | ||
vec3.push_back(-10); | ||
vec3.push_back(1000); | ||
vec3.push_back(5); | ||
ASSERT_EQ(Quick_Sort(vec3, 0, 15), | ||
(std::vector<int>{-1233, -56, -34, -12, -10, 0, 5, 12, 15, 26, 43, | ||
78, 346, 1000, 1325, 3345})); | ||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,6 @@ | ||
#include "k_stat.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
#include <stdexcept> | ||
|
||
TEST(k_stat, Simple) { | ||
std::vector<int> vec1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; | ||
ASSERT_EQ(FindOrderStatistic(vec1, 8), 21); | ||
ASSERT_EQ(FindOrderStatistic(vec1, 3), 5); | ||
ASSERT_EQ(FindOrderStatistic(vec1, 10), 93); | ||
EXPECT_THROW(FindOrderStatistic(vec1, 13), std::logic_error); | ||
std::vector<int> vec2{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | ||
ASSERT_EQ(FindOrderStatistic(vec2, 4), 5); | ||
vec2.pop_back(); | ||
vec2.pop_back(); | ||
vec2.push_back(-12); | ||
vec2.push_back(100); | ||
vec2.push_back(134); | ||
ASSERT_EQ(FindOrderStatistic(vec2, 9), 100); | ||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} |