一、栈的模板类
- #include
- using namespace std;
- #define MAX 50
-
- template<typename T>
- class Stack
- {
- private:
- T *data;
- int top;
- public:
- Stack():data(new T[MAX]),top(-1)
- {
- cout<<"Stack::无参构造函数"<
- }
- ~Stack()
- {
- delete []data;
- cout<<"Stack::构析函数"<
- }
- Stack(const Stack
&other) - {
- if(data!=NULL)
- {
- delete[]data;
- }
- this->data=new T[MAX];
- memcpy(this->data,other.data,sizeof(T)*MAX);
- this->top=other.top;
- cout<<"Stack::拷贝构造函数"<
- }
- void stack_push(T &&val);
- void stack_pop();
- void stack_clear();
- bool stack_empty();
- bool stack_full();
- T stack_gettop();
- int stack_len();
- void stack_show();
- };
- template<typename T>
- void Stack
::stack_push(T &&val) - {
- this->top++;
- this->data[top]=val;
- }
-
- template<typename T>
- void Stack
::stack_pop() - {
- if(NULL==this->data || stack_empty())
- {
- cout<<"所给顺序栈不合法"<
- }
- cout<<this->data[top]<<"出栈成功"<
- this->top--;
- }
-
- template<typename T>
- void Stack
::stack_clear() - {
- // while(!stack_empty(S))
- // {
- // stack_pop(S);
- // }
- this->top=-1;
- }
-
- template<typename T>
- bool Stack
::stack_empty() - {
- return -1==this->top;
- }
-
- template<typename T>
- bool Stack
::stack_full() - {
- return MAX-1==this->top;
- }
-
- template<typename T>
- T Stack
::stack_gettop() - {
- return this->data[top];
- }
-
- template<typename T>
- int Stack
::stack_len() - {
- return this->top+1;
- }
-
- template<typename T>
- void Stack
::stack_show() - {
- for(int i=top;i>=0;i--)
- {
- cout<<this->data[i]<<" ";
- }
- cout<
- }
- int main()
- {
- Stack<int> S;
- cout<
stack_empty()< - S.stack_push(5);
- S.stack_push(8);
- S.stack_push(6);
- S.stack_push(7);
- S.stack_push(3);
- S.stack_push(9);
- cout<
stack_full()< - cout<<"栈顶元素为"<
stack_gettop()< - cout<<"栈的长度为"<
stack_len()< - S.stack_show();
- S.stack_clear();
- S.stack_push(520);
- S.stack_show();
- return 0;
- return 0;
- }
二、队列的模板类
- #include
-
- #define MAX 50
- using namespace std;
-
- template<typename T>
- class Queue
- {
- private:
- T *data;
- int head;
- int tail;
-
- public:
- Queue():data(new T[MAX]),head(0),tail(0)
- {
- cout<<"queue::无参构造函数"<
- }
- ~Queue()
- {
- delete []data;
- cout<<"queue::析构函数"<
- }
- Queue(const Queue &other):
- data(new T(*other.data)),
- head(other.head),
- tail(other.tail)
- {
- this->data=new T[MAX];
- memcpy(this->data,other.data,sizeof(T)*MAX);
- /*
- for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
- {
- data[i]=other.data[i];
- }
- */
- this->head=other.head;
- this->tail=other.tail;
- cout<<"拷贝构造函数"<
- }
- void queue_push(T &&val);
- void queue_pop();
- void queue_clear();
- bool queue_empty();
- bool queue_full();
- int queue_len();
- void queue_show();
- };
- template<typename T>
- void Queue
::queue_push(T &&val) - {
- data[tail]=val;
- tail=(tail+1)%MAX;
- return;
- }
- template<typename T>
- void Queue
::queue_pop() - {
- cout<"出队成功"<
- head=(head+1)%MAX;
- return;
- }
- template<typename T>
- void Queue
::queue_clear() - {
- while(!queue_empty())
- {
- queue_pop();
- }
- }
- template<typename T>
- bool Queue
::queue_empty() - {
- return head==tail;
- }
- template<typename T>
- bool Queue
::queue_full() - {
- return head==(tail+MAX)%MAX;
- }
- template<typename T>
- int Queue
::queue_len() - {
- return (tail+MAX-head)%MAX;
- }
- template<typename T>
- void Queue
::queue_show() - {
- for(int i=head;i!=tail;i=(i+1)%MAX)
- {
- cout<" ";
- }
-
-
相关阅读:
周五(政治日)(第一次)
Linux常用指令--文件目录常用指令
CC攻击演示
HIVE内置函数hash() -- 源码解析
JavaWeb之jQuery
linux安装idea
Java架构师的底气,是从哪里来的?
Oracle事务
WPF——样式与模板
笔记-Elasticsearch搜索引擎构建入门与实战
-
原文地址:https://blog.csdn.net/wxmchong/article/details/132863737