mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-07 12:54:09 +08:00
更新数据结构
This commit is contained in:
134
Code/CPP-Code/head/sequence_stack.h
Normal file
134
Code/CPP-Code/head/sequence_stack.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 顺序栈
|
||||
class SequenceStack {
|
||||
private:
|
||||
// 栈内元素
|
||||
element_type *_data{};
|
||||
// 栈顶指针
|
||||
int _top{};
|
||||
// 最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData(element_type *data);
|
||||
|
||||
// 栈顶指针自加
|
||||
bool SetTop();
|
||||
|
||||
// 设置栈顶指针
|
||||
bool SetTop(int top);
|
||||
|
||||
// 获取栈顶指针
|
||||
int GetTop() const;
|
||||
|
||||
// 设置最大容量
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
// 构造函数
|
||||
SequenceStack();
|
||||
|
||||
explicit SequenceStack(int max_size);
|
||||
|
||||
// 判空
|
||||
bool Empty() const;
|
||||
|
||||
// 判满
|
||||
bool Full() const;
|
||||
|
||||
// 栈长
|
||||
int Length() const;
|
||||
|
||||
// 进栈
|
||||
bool Push(element_type elem);
|
||||
|
||||
// 出栈
|
||||
element_type Pop();
|
||||
|
||||
// 读栈顶
|
||||
element_type Top();
|
||||
};
|
||||
|
||||
bool SequenceStack::SetData(element_type *data) {
|
||||
this->_data = data;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceStack::SetTop() {
|
||||
this->_top++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceStack::SetTop(int top) {
|
||||
this->_top = top;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceStack::GetTop() const {
|
||||
return this->_top;
|
||||
}
|
||||
|
||||
bool SequenceStack::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceStack::GetMaxSize() const {
|
||||
return this->_max_size;
|
||||
}
|
||||
|
||||
SequenceStack::SequenceStack() {
|
||||
this->SetData(new element_type[MAXSIZE]);
|
||||
this->SetTop(-1);
|
||||
this->SetMaxSize(MAXSIZE);
|
||||
}
|
||||
|
||||
SequenceStack::SequenceStack(int max_size) {
|
||||
this->SetData(new element_type[max_size]);
|
||||
this->SetTop(-1);
|
||||
this->SetMaxSize(max_size);
|
||||
}
|
||||
|
||||
bool SequenceStack::Empty() const {
|
||||
return this->GetTop() == -1;
|
||||
}
|
||||
|
||||
bool SequenceStack::Full() const {
|
||||
return this->GetTop() == this->GetMaxSize() - 1;
|
||||
}
|
||||
|
||||
int SequenceStack::Length() const {
|
||||
return this->GetTop() + 1;
|
||||
}
|
||||
|
||||
bool SequenceStack::Push(element_type elem) {
|
||||
if (this->Full()) {
|
||||
cout << "Push:栈满无法进栈!" << endl;
|
||||
return false;
|
||||
}
|
||||
this->_data[this->SetTop()] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type SequenceStack::Pop() {
|
||||
if (this->Empty()) {
|
||||
cout << "Pop:栈空无法出栈!" << endl;
|
||||
return NULL;
|
||||
}
|
||||
return this->_data[this->SetTop(this->GetTop() - 1)];
|
||||
}
|
||||
|
||||
element_type SequenceStack::Top() {
|
||||
if (this->Empty()) {
|
||||
cout << "Top:栈空无法读栈顶元素!" << endl;
|
||||
return NULL;
|
||||
}
|
||||
return this->_data[this->GetTop() - 1];
|
||||
}
|
||||
Reference in New Issue
Block a user