在剑指offer的练习10中,需利用两个栈实现队列的效果。在第一个版本中,我完成了int类型的效果实现。
9_Use_Two_Stack_Realization_Queue.h:
#ifndef Use_Two_Stack_Realization_Queue_H
#define Use_Two_Stack_Realization_Queue_H
#include "header.h"
class stack_to_queue
{
private:
stack<int> stack_base;
stack<int> stack_queue;
public:
stack_to_queue();
~stack_to_queue();
void appendTail(int value);
void deleteHead();
};
9_Use_Two_Stack_Realization_Queue.cpp:
#include "9_Use_Two_Stack_Realization_Queue.h"
stack_to_queue::stack_to_queue()
{
}
stack_to_queue::~stack_to_queue()
{
}
void stack_to_queue::appendTail(int value)
{
stack_base.push(value);
cout<<"now the queue have "<<stack_base.size()+stack_queue.size()<<" size"<<endl;
}
void stack_to_queue::deleteHead()
{
if (stack_queue.empty())
{
while (!stack_base.empty())
{
stack_queue.push(stack_base.top());
stack_base.pop();
}
}
if (stack_queue.empty())
cout << "there have no message in queue" << endl;
else
{
cout << "delete the head value:" << stack_queue.top() << endl;
stack_queue.pop();
}
cout<<"now the queue have "<<stack_base.size()+stack_queue.size()<<" size"<<endl;
}
这时代码可以正常地被测试:
void test9()
{
stack_to_queue st;
st.appendTail(1);
st.appendTail(2);
st.appendTail(3);
st.deleteHead();
st.deleteHead();
st.appendTail(4);
st.appendTail(5);
st.deleteHead();
st.deleteHead();
st.deleteHead();
st.deleteHead();
}
int main(int argc, char *argv[])
{
clock_t start,end;
start=clock();
test9();
end=clock();
cout<<endl<<"this function using :"<<double(end-start)/CLOCKS_PER_SEC<<"s"<<endl;
return 0;
}
root@cephtest:/home/jzoffer# ./test
now the queue have 1 size
now the queue have 2 size
now the queue have 3 size
delete the head value:1
now the queue have 2 size
delete the head value:2
now the queue have 1 size
now the queue have 2 size
now the queue have 3 size
delete the head value:3
now the queue have 2 size
delete the head value:4
now the queue have 1 size
delete the head value:5
now the queue have 0 size
there have no message in queue
now the queue have 0 size
this function using :0.000263s
然而目前的代码只能接受int类型。进一步的,使用模板template <typename T>来使代码能够接受不同类型。然而修改后编译程序报错:
root@cephtest:/home/jzoffer# make
Scanning dependencies of target test
[ 11%] Building CXX object CMakeFiles/test.dir/10_Fibonacci_Sequence.cpp.o
[ 22%] Building CXX object CMakeFiles/test.dir/4_FindNFrom2Arry.cpp.o
[ 33%] Building CXX object CMakeFiles/test.dir/5_Change_Space_To_Otherchars.cpp.o
[ 44%] Building CXX object CMakeFiles/test.dir/6_EndToFirst_From_LinkNode.cpp.o
[ 55%] Building CXX object CMakeFiles/test.dir/7_Rebuild_BinaryTree.cpp.o
[ 66%] Building CXX object CMakeFiles/test.dir/8_FindNextNode_From_Tree.cpp.o
[ 77%] Building CXX object CMakeFiles/test.dir/9_Use_Two_Stack_Realization_Queue.cpp.o
[ 88%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[100%] Linking CXX executable test
CMakeFiles/test.dir/main.cpp.o: In function `test9()':
main.cpp:(.text+0x497): undefined reference to `stack_to_queue<int>::stack_to_queue()'
main.cpp:(.text+0x4ab): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4bf): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4d3): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4e2): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x4f1): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x505): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x519): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x528): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x537): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x546): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x555): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x564): undefined reference to `stack_to_queue<int>::~stack_to_queue()'
main.cpp:(.text+0x588): undefined reference to `stack_to_queue<int>::~stack_to_queue()'
collect2: error: ld returned 1 exit status
CMakeFiles/test.dir/build.make:276: recipe for target 'test' failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test.dir/all' failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
有三种方法可以解决该问题,分别是:
1、在.cpp文件的最尾,实例化所需要的类型:
template class stack_to_queue<int>;
template class stack_to_queue<char>;
2、将.cpp文件中的代码全部移动到.h文件中。相当于不再见声明和实现分离。
3、将.cpp文件重命名为xxx.h文件,然后在原来的.h文件中包含该xxx.h头文件
以上方法任选一种即可使程序正常运行。