实现链表打印
This commit is contained in:
parent
29ddbd5a3e
commit
ff08160831
@ -2,8 +2,10 @@ set(APP_TEST_NAME leetcode_test)
|
||||
|
||||
message(STATUS "=========== start testing APP_TEST_NAME: ${APP_TEST_NAME} ==========")
|
||||
set(TEST_SOURCES)
|
||||
string(REGEX REPLACE ";" "\t\n" TMEP_SOURCES "${SOURCES}")
|
||||
file(GLOB_RECURSE TEST_SOURCES "tests/*.cpp")
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp")
|
||||
string(REGEX REPLACE ";" "\t\n" TMEP_SOURCES "${TEST_SOURCES}")
|
||||
|
||||
message(STATUS "=========== test_file =========== \n${TMEP_SOURCES}")
|
||||
message(STATUS "=========== test_file =========== \n")
|
||||
|
||||
|
||||
35
include/jianzhi_offer.h
Normal file
35
include/jianzhi_offer.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
namespace yaha {
|
||||
class CQueue {
|
||||
public:
|
||||
CQueue() {
|
||||
}
|
||||
|
||||
void append_tail(int value);
|
||||
int delete_head();
|
||||
|
||||
private:
|
||||
std::stack<int> _stk_tail;
|
||||
std::stack<int> _stk_head;
|
||||
};
|
||||
|
||||
class String {
|
||||
public:
|
||||
std::string replace_space(std::string s);
|
||||
};
|
||||
|
||||
class ListNode {
|
||||
public:
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
std::vector<int> reverse_print(ListNode *head);
|
||||
void reverse(ListNode *head, std::vector<int>& res);
|
||||
};
|
||||
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
#include <stack>
|
||||
namespace yaha {
|
||||
class CQueue {
|
||||
public:
|
||||
CQueue() {
|
||||
}
|
||||
|
||||
void append_tail(int value);
|
||||
int delete_head();
|
||||
|
||||
private:
|
||||
std::stack<int> _stk_tail;
|
||||
std::stack<int> _stk_head;
|
||||
};
|
||||
}
|
||||
19
src/jianzhi_offer/list_node.cpp
Normal file
19
src/jianzhi_offer/list_node.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
namespace yaha {
|
||||
void ListNode::reverse(ListNode *head, std::vector<int>& res) {
|
||||
if (head==nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
reverse(head->next, res);
|
||||
res.push_back(head->val);
|
||||
}
|
||||
|
||||
std::vector<int> ListNode::reverse_print(ListNode *head) {
|
||||
std::vector<int> res;
|
||||
reverse(head, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
37
src/jianzhi_offer/queue_with_two_stacks.cpp
Normal file
37
src/jianzhi_offer/queue_with_two_stacks.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
namespace yaha {
|
||||
void CQueue::append_tail(int value) {
|
||||
_stk_tail.push(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 如果双栈都为空,则返回 -1
|
||||
* 2. 如果当栈 _stk_head 不为空,直接返回_stk_head
|
||||
* 3. 当_stk_tail不为空,_stk_head为空,则需要完成重排列并返回_stk_head
|
||||
* @return
|
||||
*/
|
||||
int CQueue::delete_head() {
|
||||
|
||||
if (_stk_tail.empty() && _stk_head.empty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!_stk_head.empty()) {
|
||||
int tmp = _stk_head.top();
|
||||
_stk_head.pop();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
if (_stk_head.empty()) {
|
||||
while (!_stk_tail.empty()) {
|
||||
_stk_head.push(_stk_tail.top());
|
||||
_stk_tail.pop();
|
||||
}
|
||||
|
||||
}
|
||||
int tmp = _stk_head.top();
|
||||
_stk_head.pop();
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
15
src/jianzhi_offer/string.cpp
Normal file
15
src/jianzhi_offer/string.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
namespace yaha {
|
||||
std::string String::replace_space(std::string s) {
|
||||
std::string result;
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
if (s[i]!=' ') {
|
||||
result += s[i];
|
||||
} else {
|
||||
result += "%20";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
#include "queue_vec.h"
|
||||
|
||||
namespace yaha {
|
||||
void CQueue::append_tail(int value) {
|
||||
_stk_tail.push(value);
|
||||
}
|
||||
|
||||
int CQueue::delete_head() {
|
||||
if(_stk_head.empty()){
|
||||
while (!_stk_tail.empty()) {
|
||||
_stk_head.push(_stk_tail.top());
|
||||
_stk_tail.pop();
|
||||
}
|
||||
}
|
||||
if (_stk_head.empty()) {
|
||||
return -1;
|
||||
}
|
||||
int tmp = _stk_head.top();
|
||||
_stk_head.pop();
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
13
tests/jianzhi_offer/list_node_test.cpp
Normal file
13
tests/jianzhi_offer/list_node_test.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
TEST(NodeListTest, BasicAssertions) {
|
||||
yaha::ListNode* node = new yaha::ListNode(1);
|
||||
node->next = new yaha::ListNode(3);
|
||||
node->next->next = new yaha::ListNode(2);
|
||||
auto res = node->reverse_print(node);
|
||||
ASSERT_EQ(2, res[0]);
|
||||
ASSERT_EQ(3, res[1]);
|
||||
ASSERT_EQ(1, res[2]);
|
||||
}
|
||||
37
tests/jianzhi_offer/queue_with_two_stacks_test.cpp
Normal file
37
tests/jianzhi_offer/queue_with_two_stacks_test.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
/**
|
||||
* 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
|
||||
* 作者:Krahets
|
||||
* 链接:https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/
|
||||
* 来源:力扣(LeetCode)
|
||||
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
||||
*/
|
||||
TEST(QueueWithTwoStackTest, BasicAssertions) {
|
||||
/**
|
||||
* 示例 1:
|
||||
* 输入:
|
||||
* ["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
|
||||
* [[],[3],[],[],[]]
|
||||
* 输出:[null,null,3,-1,-1]
|
||||
*/
|
||||
yaha::CQueue c_queue_1;
|
||||
c_queue_1.append_tail(3);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), 3);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), -1);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), -1);
|
||||
/**
|
||||
* 输入:
|
||||
* ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
|
||||
* [[],[],[5],[2],[],[]]
|
||||
* 输出:[null,-1,null,null,5,2]
|
||||
*/
|
||||
yaha::CQueue c_queue_2;
|
||||
ASSERT_EQ(c_queue_2.delete_head(), -1);
|
||||
c_queue_2.append_tail(5);
|
||||
c_queue_2.append_tail(2);
|
||||
ASSERT_EQ(c_queue_2.delete_head(), 5);
|
||||
ASSERT_EQ(c_queue_2.delete_head(), 2);
|
||||
}
|
||||
10
tests/jianzhi_offer/string_test.cpp
Normal file
10
tests/jianzhi_offer/string_test.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "jianzhi_offer.h"
|
||||
|
||||
TEST(ReplaceSpaceTest, BasicAssertions) {
|
||||
std::string input = "We are happy.";
|
||||
std::string expected = "We%20are%20happy.";
|
||||
|
||||
yaha::String s;
|
||||
EXPECT_EQ(expected, s.replace_space(input));
|
||||
}
|
||||
@ -1,66 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "queue_vec.h"
|
||||
|
||||
/**
|
||||
* 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
|
||||
* 作者:Krahets
|
||||
* 链接:https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/
|
||||
* 来源:力扣(LeetCode)
|
||||
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
||||
*/
|
||||
TEST(QueueWithTwoStackTest, BasicAssertions) {
|
||||
/**
|
||||
* 示例 1:
|
||||
* 输入:
|
||||
* ["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
|
||||
* [[],[3],[],[],[]]
|
||||
* 输出:[null,null,3,-1,-1]
|
||||
*/
|
||||
yaha::CQueue c_queue_1;
|
||||
c_queue_1.append_tail(3);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), 3);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), -1);
|
||||
ASSERT_EQ(c_queue_1.delete_head(), -1);
|
||||
/**
|
||||
* 输入:
|
||||
* ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
|
||||
* [[],[],[5],[2],[],[]]
|
||||
* 输出:[null,-1,null,null,5,2]
|
||||
*/
|
||||
yaha::CQueue c_queue_2;
|
||||
ASSERT_EQ(c_queue_2.delete_head(), -1);
|
||||
c_queue_2.append_tail(5);
|
||||
c_queue_2.append_tail(2);
|
||||
ASSERT_EQ(c_queue_2.delete_head(), 5);
|
||||
ASSERT_EQ(c_queue_2.delete_head(), 2);
|
||||
}
|
||||
|
||||
TEST(MoreQueueWithTwoStackTest, BasicAssertions) {
|
||||
yaha::CQueue c_queue;
|
||||
|
||||
// 输入示例
|
||||
std::vector<int> inputs =
|
||||
{-1, -1, 97, -1, -1, -1, -1, 15, -1, 1, 43, -1, -1, -1, 18, -1, -1, -1, -1, 36, 69, 21, 91, -1, -1, 22, 40, -1,
|
||||
-1, -1, 81, 65, -1, 77, -1, 63, 96, 5, -1, -1, 35, 90, -1, -1, -1, -1, 77, 83, -1, -1, 52, -1, 2, 66, 87, 90,
|
||||
-1, 2, -1, -1, 33, 16, 72, -1, -1, 14, 78, 8, -1, -1, -1, -1, 3, 83, -1, -1, 13, -1, 79, 44, -1, -1, 33, -1,
|
||||
55, 76, 12, -1, 91, 24, 49, 47, -1, -1, -1, 85, -1, 69, -1, 94, 52};
|
||||
|
||||
// 预期结果示例
|
||||
std::vector<int> expected_results =
|
||||
{-1, -1, 97, -1, -1, -1, -1, 15, -1, 1, 43, -1, -1, -1, 18, -1, -1, -1, -1, 36, 69, 21, 91, -1, -1, 22, 40, -1,
|
||||
-1, -1, 81, 65, -1, 77, -1, 63, 96, 5, -1, -1, 35, 90, -1, -1, -1, -1, 77, 83, -1, -1, 52, -1, 2, 66, 87, 90,
|
||||
-1, 2, -1, -1, 33, 16, 72, -1, -1, 14, 78, 8, -1, -1, -1, -1, 3, 83, -1, -1, 13, -1, 79, 44, -1, -1, 33, -1,
|
||||
55, 76, 12, -1, 91, 24, 49, 47, -1, -1, -1, 85, -1, 69, -1, 94, 52};
|
||||
|
||||
// 处理每个操作序列
|
||||
for (const auto &op : inputs) {
|
||||
if (op==-1) {
|
||||
// 删除头部并验证结果为-1
|
||||
std::cout << c_queue.delete_head() << std::endl;
|
||||
} else {
|
||||
// 添加尾部
|
||||
c_queue.append_tail(op);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,12 +25,12 @@ int main(int argc, char *argv[]) {
|
||||
HeapProfilerStart(heap_name.c_str());
|
||||
#endif
|
||||
|
||||
int result = RUN_ALL_TESTS();
|
||||
auto ret = RUN_ALL_TESTS();
|
||||
|
||||
#ifdef ENABLE_PROFILING
|
||||
ProfilerStop();
|
||||
HeapProfilerStop();
|
||||
#endif
|
||||
|
||||
return result;
|
||||
return ret;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user