当前位置: 首页 > news >正文

C++:继承和多态,自定义封装栈,队列

1.栈:

stack.cpp

#include "stack.h"Stack::Stack():top(nullptr),len(0){}
//析构函数
Stack::~Stack()
{while(!empty()){pop();}
}bool Stack::empty() //判断栈是否为空
{return top==nullptr;
}int Stack::size()//获取栈的大小
{return len;
}
//压栈操作
void Stack::push(int element)
{StackNode *newnode=new StackNode(element);//申请空间并初始化newnode->next=top;top=newnode;len++;
}
//出栈操作
int Stack::pop()
{if(empty()){throw out_of_range("空栈");}StackNode *temp=top;int value=top->data;top=top->next;delete temp;len--;return value;
}
//查看栈顶元素
int Stack::look_top()
{if(empty()){throw out_of_range("空栈");}return top->data;
}
//清空栈
void Stack::clear()
{while (!empty()){pop();}
}
Stack& Stack::operator=(const Stack& other)
{if (this != &other){clear(); // 清空当前栈// 复制元素StackNode *current = other.top;while (current != nullptr){push(current->data);current = current->next;}}return *this;
}void Stack::swap(Stack& other)
{StackNode* tempTop = top;top = other.top;other.top = tempTop;int templen = len;len = other.len;other.len = templen;}

stack.h

#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <stdexcept>using namespace std;
//栈节点
class StackNode
{
public:int data;//存储数据StackNode *next;//指向下一个节点//构造函数StackNode(int d):data(d),next(nullptr){}};class Stack
{
private:StackNode *top;//指向栈顶int len;//栈中元素数量public:Stack();//析构函数~Stack();bool empty(); //判断栈是否为空int size();//获取栈的大小//压栈操作void push(int element);//出栈操作int pop();//查看栈顶元素int look_top();//void clear();Stack& operator=(const Stack& other) ;void swap(Stack& other);
};
#endif // STACK_H

2.队列

queue.cpp

#include "queue.h"// 构造函数
Queue::Queue() : front(nullptr), rear(nullptr), count(0) 
{}Queue::~Queue() 
{clear();
}
void Queue::clear()
{while (front != nullptr){QueueNode* temp = front;front = front->next;delete temp;}rear = nullptr;count = 0;
}// 查看队列前端元素
int Queue::frontElement()
{if (empty()){throw std::out_of_range("空队列");}return front->data;
}
// 查看队列尾部元素
int Queue::backElement()
{if (empty()){throw std::out_of_range("空队列");}return rear->data;
}
//判断队列是否为空
bool Queue::empty()
{return front == nullptr;
}
// 获取队列的大小
int Queue::size() 
{return count;
}// 入队操作
void Queue::push(int element)
{QueueNode* newNode = new QueueNode(element);if (rear == nullptr)  // 队列为空{front = rear = newNode;} else {rear->next = newNode;rear = newNode;}count++;
}// 出队操作
int Queue::pop() 
{if (empty()) {throw std::out_of_range("空队列");}QueueNode* temp = front;int dequeuedValue = front->data;front = front->next;if (front == nullptr) {rear = nullptr;}delete temp;count--;return dequeuedValue;
}void Queue::swap(Queue& other) {using std::swap;swap(front, other.front);swap(rear, other.rear);swap(count, other.count);
}

queue.h

#ifndef QUEUE_H
#define QUEUE_H#include <iostream>using namespace std;class QueueNode
{
public:int data;QueueNode *next;//有参构造QueueNode(int d):data(d),next(nullptr){}
};
class Queue {
private:QueueNode* front; // 指向队列头部的指针QueueNode* rear;  // 指向队列尾部的指针int count;        // 队列中元素的数量public:// 构造函数Queue(); // 析构函数~Queue(); //清空队列void clear(); //判断队列是否为空bool empty();// 获取队列的大小int size(); // 入队操作void push(int element);// 出队操作int pop();// 查看队列前端元素int frontElement();// 查看队列尾部元素int backElement();// 交换两个队列的内容void swap(Queue& other); };
#endif // QUEUE_H

http://www.lryc.cn/news/450768.html

相关文章:

  • Python多个set中的交集
  • 百度百科 X-Bk-Token 算法还原
  • RUST语言的初印象-从一个模拟登陆谈起-slint+reqwest+aes
  • HBase批量写入优化
  • 江协科技STM32学习- P19 TIM编码器接口
  • 文件上传、重定向、Gin路由
  • 躺平成长:微信小程序运营日记第二天
  • 三分钟速览:Node.js 版本差异与关键特性解析
  • git创建新分支
  • Chip-seq数据分析处理流程
  • spring boot3.2.x与spring boot2.7.x对比
  • Vue2(十三):路由
  • Java并发:互斥锁,读写锁,公平锁,Condition,StampedLock
  • 在 Linux 中,要让某一个线程或进程排他性地独占一个 CPU
  • 滚雪球学MySQL[7.3讲]:数据库日志与审计详解:从错误日志到审计日志的配置与使用
  • 网关的作用及其高可用性设计详解
  • Vortex GPGPU的github流程跑通与功能模块波形探索
  • 10.2 Linux_并发_进程相关函数
  • 【深度学习基础模型】玻尔兹曼机BM|受限玻尔兹曼机RBM|深度置信网络DBN详细理解并附实现代码。
  • 滑动窗口->dd爱框框
  • Python从入门到高手4.1节-掌握条件控制语句
  • 使用Qt实现实时数据动态绘制的折线图示例
  • 【人人保-注册安全分析报告-无验证方式导致安全隐患】
  • Redis6 多线程模型
  • Python的异步编程
  • 初识Linux · 进程等待
  • 面向对象建模
  • MetaJUI v0.4 遇到的一些问题及解决办法记录
  • 从零开始学习OMNeT++系列第二弹——新建一个OMNeT++的工程
  • 【Android】布局优化—include,merge,ViewStub的使用方法