23062C++&QTday3

1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

头文件stack.c

#ifndef STACK_H
#define STACK_H
#include 
#include 

using namespace std;
class stack
{
private:
    int data[128];
    int top;
public:
    //构造函数
    stack();
    //析构函数
    ~stack();
    //拷贝构造函数
    stack(const stack &other);
    //入栈
    int stack_push(int e);
    //出栈
    int stack_pop();
    //清空栈
    void stack_clear();
    //判空
    bool stack_empty();
    //判满
    bool stack_full();
    //获取栈顶元素
    int &stack_top();
    //求栈的大小
    int stack_size();
};
#endif // STACK_H

源文件:

#include"stack.h"
//构造函数
stack::stack()
{
    cout<<"构造函数"<

 测试文件:

#include"stack.h"

int main()
{
    stack s1;
    s1.stack_push(1);
    s1.stack_push(2);
    s1.stack_pop();
    cout << "s1.data[top]="<

运行结果:

23062C++&QTday3_第1张图片

2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

 头文件:

#ifndef QUEUE_H
#define QUEUE_H
#include 
using namespace std;

class queue
{
private:
  int data[128];
  int front=0;
  int tail=0;
public:
  //构造函数
  queue();
  //析构函数
  ~queue();
  //拷贝构造函数
  queue(const queue &other );
  //入队
  int queue_push(int e);
  //出队
  int queue_pop();
  //清空队列
  void queue_delete();
  //判空
  bool queue_empty();
  //判满
  bool queue_full();
  //求队列的大小
  int queue_size();
  //遍历
  void queue_show();
};

#endif // QUEUE_H

源文件:

#include"queue.h"

//构造函数
queue::queue()
{
    cout<<"构造函数"<

测试文件:

#include"queue.h"

int main()
{
    queue q1;
    q1.queue_push(520);
    q1.queue_push(1314);
    q1.queue_pop();
    cout<<"q1的大小为"<

运行结果:

23062C++&QTday3_第2张图片

思维导图

23062C++&QTday3_第3张图片

你可能感兴趣的:(c++)