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