![](https://i-blog.csdnimg.cn/blog_migrate/6893fea6a68388eb2b8a46ca1972f920.jpeg)
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
typedef int datatype;
class stack
{
datatype * data;
int top;
public:
stack();
~stack();
stack(const stack &other);
//创建栈
stack *Stack_create();
//判空
int Stack_empty(stack *s);
//判满
int Stack_full(stack *s);
//入栈
int Stack_push(stack *s,datatype e);
//遍历栈
void Stack_show(stack *s);
//出栈
int Stack_pop(stack *s);
//获取栈顶元素
datatype * Stack_top(stack *s);
//求栈的大小
int Stack_size(stack *s);
//销毁栈
void Stack_free(stack *s);
};
#endif // STACK_H
#include"stack.h"
stack::stack():data(new int(111))
{
cout << "stack::无参构造" << endl;
}
stack::~stack()
{
delete data;
cout << "stack::析构函数:" << this << endl;
}
stack::stack(const stack &other):data(new int(*other.data)),top(other.top)
{
cout << "拷贝构造函数" << endl;
}
//创建栈
stack * stack::Stack_create()
{
stack * s = (stack *)malloc(sizeof(stack));
if(NULL == s)
{
printf("申请失败\n");
return NULL;
}
s -> data = (datatype *)malloc(sizeof(datatype) * 10);
if(NULL == s -> data)
{
printf("申请失败\n");
free(s);
return NULL;
}
s -> top = -1;
printf("创建成功\n");
return s;
}
//判空
int stack::Stack_empty(stack *s)
{
if(NULL == s)
{
printf("栈不合法\n");
return 0;
}
return s -> top == -1;
}
//判满
int stack::Stack_full(stack *s)
{
if(NULL == s)
{
printf("栈不合法\n");
return 0;
}
return s -> top == 10 - 1;
}
//入栈
int stack::Stack_push(stack * s,datatype e)
{
if(NULL == s || Stack_full(s))
{
printf("入栈失败\n");
return 0;
}
s -> top++;
s -> data[s -> top] = e;
printf("入栈成功\n");
return 1;
}
//遍历栈
void stack::Stack_show(stack *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");
}
//出栈
int stack::Stack_pop(stack * s)
{
if(NULL == s || Stack_empty(s))
{
printf("出栈失败\n");
return 0;
}
datatype e = s -> data[s -> top];
printf("%d",e);
s -> top--;
return 1;
}
//获取栈顶元素
datatype * stack::Stack_top(stack *s)
{
if(NULL == s || Stack_empty(s))
{
printf("获取失败\n");
return NULL;
}
return &s -> data[s -> top];
}
//求栈的大小
int stack::Stack_size(stack *s)
{
if(NULL == s)
{
printf("求取失败\n");
return 0;
}
return s -> top + 1;
}
//销毁栈
void stack::Stack_free(stack *s)
{
if(NULL == s)
{
printf("不合法\n");
return;
}
free(s -> data);
s -> data = NULL;
free(s);
s = NULL;
printf("释放成功\n");
return ;
}
#include"stack.h"
int main()
{
stack k;
stack *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;
}
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream>
using namespace std;
#define MAX 10
typedef int datatype;
class queue
{
datatype data[MAX];
int front;
int tail;
public:
queue();
~queue();
queue(const queue &other);
//创建循环队列
queue *queue_create();
//判空
int queue_empty(queue *q);
//判满
int queue_full(queue *q);
//入队
int queue_push(queue *q,datatype 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"
queue::queue()
{
cout << "queue:无参构造" << endl;
}
queue::~queue()
{
cout << "queue::析构函数" << this << endl;
}
queue::queue(const queue &other):front(other.front),tail(other.tail)
{
cout << "拷贝构造函数" << endl;
}
//创建循环队列
queue *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;
}
//判空
int queue::queue_empty(queue * q)
{
if(NULL == q)
{
printf("队列错误\n");
return 0;
}
return q -> front == q -> tail;
}
//判满
int queue::queue_full(queue *q)
{
if(NULL == q)
{
printf("队列错误\n");
return 0;
}
return (q -> tail + 1) % MAX == q -> front;
}
//入队
int queue::queue_push(queue *q,datatype e)
{
if(NULL == q)
{
printf("入队失败\n");
return 0;
}
q -> data[q -> tail] = e;
q -> tail = (q ->tail + 1) % MAX;
printf("入队成功\n");
return 1;
}
//遍历
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");
}
//出队
int queue::queue_pop(queue * q)
{
if(NULL == q)
{
printf("出队失败\n");
return 0;
}
printf("%d\t",q -> data[q -> front]);
q -> front = (q -> front + 1) % MAX;
return 1;
}
//求队列的长度
int queue::queue_size(queue * q)
{
if(NULL == q)
{
printf("不合法\n");
return 0;
}
return (q -> tail + MAX - q -> front) % MAX;
}
//销毁
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;
int main()
{
queue u;
queue *q = u.queue_create();
if(NULL == q)
{
return -1;
}
//入队
u.queue_push(q,5);
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;
}