- #ifndef QUEUE_H
- #define QUEUE_H
-
- #include
-
- using namespace std;
-
- #define MAX 10
-
- typedef int datatype;
-
- template <typename T>
- class queue
- {
-
- T data[MAX];
- T front;
- T tail;
-
-
- public:
- queue();
-
- ~queue();
-
- queue(const T &other);
-
- //创建循环队列
- T *queue_create();
-
- //判空
- int queue_empty(queue *q);
-
- //判满
- int queue_full(queue *q);
-
- //入队
- int queue_push(queue *q,T e);
-
- //遍历
- void queue_show(queue *q);
-
- //出队
- int queue_pop(queue *q);
-
- //求队列的长度
- int queue_size(queue *q);
-
- //销毁
- void queue_free(queue *q);
-
- };
-
-
- #endif // QUEUE_H
- #include"queue.h"
-
- template <typename T>
- queue
::queue() - {
- cout << "queue:无参构造" << endl;
- }
-
- template <typename T>
- queue
::~queue() - {
-
- cout << "queue::析构函数" << this << endl;
- }
-
- template <typename T>
- queue
::queue(const T &other):front(other.front),tail(other.tail) - {
- cout << "拷贝构造函数" << endl;
- }
-
- //创建循环队列
- template <typename T>
- T *queue
::queue_create() - {
- queue * q = (queue *)malloc(sizeof(queue));
- if(NULL == q)
- {
- printf("创建失败\n");
- return NULL;
- }
-
-
- q -> front = q -> tail = 0;
-
- printf("创建成功\n");
- return q;
- }
-
-
- //判空
- template <typename T>
- int queue
::queue_empty(queue * q) - {
- if(NULL == q)
- {
- printf("队列错误\n");
- return 0;
- }
-
- return q -> front == q -> tail;
- }
-
-
- //判满
- template <typename T>
- int queue
::queue_full(queue *q) - {
- if(NULL == q)
- {
- printf("队列错误\n");
- return 0;
- }
-
- return (q -> tail + 1) % MAX == q -> front;
- }
-
-
- //入队
- template <typename T>
- int queue
::queue_push(queue *q,T e) - {
- if(NULL == q)
- {
- printf("入队失败\n");
- return 0;
- }
-
- q -> data[q -> tail] = e;
- q -> tail = (q ->tail + 1) % MAX;
- printf("入队成功\n");
- return 1;
- }
-
-
-
-
- //遍历
- template <typename T>
- void queue
::queue_show(queue *q) - {
- if(NULL == q || queue_empty(q))
- {
- printf("遍历失败\n");
- return;
- }
-
- printf("从队头到队尾元素分别是: ");
- for(int i = q -> front; i != q -> tail; i = (i + 1) % MAX)
- {
- printf("%d\t",q -> data[i]);
- }
- printf("\n");
- }
-
-
-
- //出队
- template <typename T>
- int queue
::queue_pop(queue * q) - {
- if(NULL == q)
- {
- printf("出队失败\n");
- return 0;
- }
-
- cout << "出队的元素是: ";
- printf("%d\n",q -> data[q -> front]);
- q -> front = (q -> front + 1) % MAX;
-
- return 1;
- }
-
- //求队列的长度
- template <typename T>
- int queue
::queue_size(queue * q) - {
- if(NULL == q)
- {
- printf("不合法\n");
- return 0;
- }
-
- return (q -> tail + MAX - q -> front) % MAX;
- }
-
-
-
- //销毁
- template <typename T>
- void queue
::queue_free(queue * q) - {
- if(NULL == q)
- {
- printf("销毁失败\n");
- return ;
- }
-
- free(q);
- q = NULL;
- printf("释放成功\n");
- return ;
- }
- #include "queue.h"
-
- using namespace std;
-
- template <typename T>
- int main()
- {
- queue
u; - queue
*q = u.queue_create(); - if(NULL == q)
- {
- return -1;
- }
-
-
- //入队
-
- u.queue_push(q,"aafa");
- u.queue_push(q,9);
- u.queue_push(q,3);
- u.queue_push(q,7);
- u.queue_push(q,4);
- u.queue_push(q,1);
-
-
- u.queue_show(q);
-
- //出队
- u.queue_pop(q);
-
- u.queue_show(q);
-
- //求队列大小
- u.queue_size(q);
-
- //清空且销毁
- u.queue_free(q);
-
-
- return 0;
- }
- #ifndef STACK_H
- #define STACK_H
-
- #include
- #include
-
- using namespace std;
-
- typedef int datatype;
-
- template <typename T>
- class stack
- {
- T * data;
- T top;
-
- public:
- stack();
-
- ~stack();
-
- stack(const T &other);
-
- //创建栈
- T *Stack_create();
-
-
- //判空
- T Stack_empty(T *s);
-
-
- //判满
- T Stack_full(T *s);
-
-
- //入栈
- T Stack_push(T *s,T e);
-
-
- //遍历栈
- T Stack_show(T *s);
-
-
- //出栈
- T Stack_pop(T *s);
-
-
- //获取栈顶元素
- T * Stack_top(T *s);
-
-
- //求栈的大小
- T Stack_size(T *s);
-
-
- //销毁栈
- T Stack_free(T *s);
-
-
-
- };
-
- #endif // STACK_H
- #include"stack.h"
-
- template <typename T>
- stack
::stack():data(new T(111)) - {
- cout << "stack::无参构造" << endl;
- }
-
- template <typename T>
- stack
::~stack() - {
- delete data;
- cout << "stack::析构函数:" << this << endl;
- }
-
-
-
-
- //创建栈
- template <typename T>
- T * stack
::Stack_create() - {
- stack * s = (stack *)malloc(sizeof(stack));
- if(NULL == s)
- {
- cout << "申请失败" << endl;
- return NULL;
- }
-
- s -> data = (datatype *)malloc(sizeof(datatype) * 10);
- if(NULL == s -> data)
- {
- cout << "申请失败" << endl;
- free(s);
- return NULL;
- }
-
- s -> top = -1;
- printf("创建成功\n");
- return s;
-
- }
-
-
- //判空
- template <typename T>
- T stack
::Stack_empty(T *s) - {
- if(NULL == s)
- {
- printf("栈不合法\n");
- return 0;
- }
- return s -> top == -1;
-
- }
-
-
- //判满
- template <typename T>
- T stack
::Stack_full(T *s) - {
- if(NULL == s)
- {
- printf("栈不合法\n");
- return 0;
- }
- return s -> top == 10 - 1;
-
- }
-
-
- //入栈
- template <typename T>
- T stack
::Stack_push(T * s,T e) - {
- if(NULL == s || Stack_full(s))
- {
- printf("入栈失败\n");
- return 0;
- }
-
- s -> top++;
- s -> data[s -> top] = e;
- printf("入栈成功\n");
- return 1;
-
- }
-
-
- //遍历栈
- template <typename T>
- T stack
::Stack_show(T *s) - {
- if(NULL == s || Stack_empty(s))
- {
- printf("遍历失败\n");
- return ;
- }
-
- printf("从栈顶到栈底的元素分别是: \n");
- for(int i = s -> top; i >= 0; i--)
- {
- printf("%d\t",s -> data[i]);
- }
- printf("\n");
-
- }
-
-
- //出栈
- template <typename T>
- T stack
::Stack_pop(T * s) - {
- if(NULL == s || Stack_empty(s))
- {
- printf("出栈失败\n");
- return 0;
- }
-
- datatype e = s -> data[s -> top];
- printf("%d",e);
- s -> top--;
- return 1;
-
- }
-
-
- //获取栈顶元素
- template <typename T>
- T * stack
::Stack_top(T *s) - {
- if(NULL == s || Stack_empty(s))
- {
- printf("获取失败\n");
- return NULL;
- }
-
- return &s -> data[s -> top];
-
- }
-
-
- //求栈的大小
- template <typename T>
- T stack
::Stack_size(T *s) - {
- if(NULL == s)
- {
- printf("求取失败\n");
- return 0;
- }
- return s -> top + 1;
-
- }
-
-
- //销毁栈
- template <typename T>
- T stack
::Stack_free(T *s) - {
- if(NULL == s)
- {
- printf("不合法\n");
- return;
- }
-
- free(s -> data);
- s -> data = NULL;
-
- free(s);
- s = NULL;
-
- printf("释放成功\n");
- return ;
-
- }
- #include"stack.h"
-
- template <typename T>
- int main()
- {
-
- stack
k; - T *s = k.Stack_create();
- if(NULL == s)
- {
- return -1;
- }
- datatype e;
-
- //入栈
- cout << "请输入一位栈底元素: ";
- cin >> e;
- k.Stack_push(s,e);
- k.Stack_push(s,2);
- k.Stack_push(s,5);
- k.Stack_push(s,6);
- k.Stack_push(s,4);
- k.Stack_push(s,8);
-
-
- //遍历栈
- k.Stack_show(s);
-
- //出栈
- k.Stack_pop(s);
-
- k.Stack_show(s);
-
- //求栈顶元素
- k.Stack_top(s);
-
- //求栈的大小
- k.Stack_size(s);
-
- //清空栈且销毁
- k.Stack_free(s);
- s = NULL;
-
- return 0;
- }
-