栈是一种数据结构,“先进后出”是它的特点。一般的,我们会在有“ 先来的数据后使用 ” 的场景里使用“栈结构”。
以下的程序以C/C++的形式,展示了栈的一般操作,其中包括创建、进栈,出栈,判满,判空,排序,扩容等。(使用数组实现,未写测试程序,随后添加)
#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <functional>
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
// 基类
class UnCopyable{
public:
UnCopyable();
~UnCopyable();
private:
UnCopyable(const UnCopyable& e);
UnCopyable& operator=(const UnCopyable& e);
};
UnCopyable::UnCopyable(){
}
UnCopyable::~UnCopyable(){
}
UnCopyable::UnCopyable(const UnCopyable& e) {
*this = e;
}
UnCopyable& UnCopyable::operator=(const UnCopyable&) {
return *this;
}
// ---------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------
// 基类UnCopyable是为了阻止MyStack可以被赋值和拷贝
class MyStack : private UnCopyable
{
public:
MyStack( unsigned int size ) : m_Top( -1 ), m_MAX( -100000 ), m_Min( 100000 )
{
m_VAPACITY = size;
m_Data = (int*)malloc(sizeof(int)*m_VAPACITY);
}
~MyStack();
private:
int *m_Data; // 存取值的数组
int m_Top; // 栈顶索引
int m_MAX; // 数组中的最大值
int m_Min; // 数组中的最小值
unsigned int m_VAPACITY; // 栈的容量
public:
enum StackSort
{
less,
greater
};
unsigned int StackVapacity() const; // 栈的容量
unsigned int StackMax() const; // 栈最大
unsigned int StackMin() const; // 栈最小
public:
bool isEmpty(); // 判断栈是否为空
bool isFull(); // 判断栈是否满了
void pop(); // 出栈(删除栈顶元素)
void push( int value ); // 入栈
int max(); // 栈中最大值(版本1)
int min(); // 栈中最小值(版本1)
int max1(); // 栈中最大值(版本2)
int min1(); // 栈中最小值(版本2)
void StackOrder( StackSort Strategy ); // 将栈中的元素排序
};
unsigned int MyStack::StackVapacity() const {
return m_VAPACITY;
}
unsigned int MyStack::StackMax() const {
return m_MAX;
}
unsigned int MyStack::StackMin() const {
return m_Min;
}
bool MyStack::isEmpty(){
return m_Top == -1;
}
bool MyStack::isFull() {
return m_Top == m_VAPACITY - 1;
}
void MyStack::pop() {
try{
if (!isEmpty()) {
m_Top--;
}
else{
std::cout << "the stack is empty." << std::endl;
}
}
catch (const std::exception& e){
std::cout << e.what() << std::endl;
}
}
void MyStack::push( int value ) {
try{
if (!isFull()) {
m_Data[m_Top++] = value;
}
else{
// realloc函数进行扩容
std::cout << "the stack is full.Do you need to expand?" << std::endl;
m_Data = (int*)realloc(m_Data, sizeof(int)*m_VAPACITY);
}
}
catch (const std::exception& e){
std::cout << e.what() << std::endl;
}
}
int MyStack::max(){
if (!isEmpty()) {
std::sort(m_Data, m_Data + m_Top + 1, std::greater<int>());
return m_Data[0];
}
}
int MyStack::min() {
if (!isEmpty()) {
std::sort(m_Data, m_Data + m_Top + 1, std::less<int>());
return m_Data[0];
}
}
int MyStack::max1() {
if (!isEmpty()) {
m_MAX = m_Data[0];
for ( size_t i = 0; i <= m_Top; i++ ){
if (m_Data[i] > m_MAX) {
m_MAX = m_Data[i];
}
}
return m_MAX;
}
return 0x1000000;
}
void MyStack::StackOrder( StackSort Strategy ) {
switch (Strategy){
case StackSort::greater:
std::sort(m_Data, m_Data + m_Top + 1, std::greater<int>());
case StackSort::less:
std::sort(m_Data, m_Data + m_Top + 1, std::less<int>());
default:
break;
}
}
int MyStack::min1() {
if (!isEmpty()) {
m_Min = m_Data[0];
for ( size_t i = 0; i <= m_Top; i++ ) {
if (m_Data[i] < m_Min) {
m_MAX = m_Data[i];
}
}
}
return 0x1000000;
}
MyStack::~MyStack(){
}
int main() {
//
//
//
}
其实,C++中有封装好的STL(std::stack)可以使用,用来代替手动写栈结构,但是作为一名软件人,还是需要知道栈的实现方式。