赞
踩
栈(stack)又名堆栈,作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。栈具有先进后出的特性。
也称为压栈,往栈里面添加新元素。
删除栈顶元素。
(1)数组:数组实现的话栈的长度固定。
(2)链表:比较灵活。
为了设计比较灵活,就写一个链表形式的栈,在此定义一个节点。
template <typename _dataType>
struct StackNode {
using _stackNodePtr = StackNode<_dataType>*;
_dataType m_value;
_stackNodePtr m_next;
StackNode() : m_value(_dataType()), m_next(nullptr) {}
StackNode(_dataType value) : m_value(value), m_next(nullptr) {}
StackNode(_dataType value, _stackNodePtr next) : m_value(value), m_next(next) {}
};
变量名 | 类型 | 属性 | 说明 |
---|---|---|---|
m_top | _stackNodePtr | 私有 | 栈顶 |
m_len | size_t | 私有 | 数据个数 |
方法名 | 返回类型 | 参数 | 属性 | 说明 |
---|---|---|---|---|
Stack() | - | - | 公有 | 缺省构造 |
Stack() | - | const Stack& s | 公有 | 拷贝构造 |
operator = () | Stack& | const Stack& s | 公有 | =运算符重载 |
push() | void | _dataType value | 公有 | 入栈 |
pop() | void | - | 公有 | 出栈 |
top() | _dataType& | - | 公有 | 栈顶访问 |
empty() | bool | - | 公有 | 判断栈是否为空 |
size() | size_t | - | 公有 | 栈里面数据个数 |
#include <iostream> #include "Stack.h" void stackTest() { std::cout << "\n构造:" << std::endl; Stack<int> s; std::cout << "\n数据个数:" << s.size() << std::endl; std::cout << "\nempty:" << std::endl; if (s.empty()) { std::cout << "s is empty!" << std::endl; } else { std::cout << "s isn't empty!" << std::endl; } std::cout << "\npush(7)" << std::endl; s.push(7); std::cout << "\n数据个数:" << s.size() << std::endl; std::cout << "top = " << s.top() << std::endl; std::cout << "\npush:1,2,3,4,5,6" << std::endl; int num = 1; while (num < 7) { s.push(num++); } std::cout << "\n数据个数:" << s.size() << std::endl; std::cout << "top = " << s.top() << std::endl; Stack<int> st = s; while (!st.empty()) { std::cout << st.top() << ", "; st.pop(); } std::cout << "\nempty:" << std::endl; if (s.empty()) { std::cout << "s is empty!" << std::endl; } else { std::cout << "s isn't empty!" << std::endl; } std::cout << "\npop" << std::endl; s.pop(); std::cout << "\n数据个数:" << s.size() << std::endl; std::cout << "top = " << s.top() << std::endl; std::cout << "\n清空栈:" << std::endl; while (!s.empty()) { s.pop(); } std::cout << "\n数据个数:" << s.size() << std::endl; std::cout << "\nempty:" << std::endl; if (s.empty()) { std::cout << "s is empty!" << std::endl; } else { std::cout << "s isn't empty!" << std::endl; } }
构造: 数据个数:0 empty: s is empty! push(7) 数据个数:1 top = 7 push:1,2,3,4,5,6 数据个数:7 top = 6 6, 5, 4, 3, 2, 1, 7, empty: s isn't empty! pop 数据个数:6 top = 5 清空栈: 数据个数:0 empty: s is empty!
#pragma once #ifndef _STACK_H_ #define _STACK_H_ #include <cassert> template <typename _dataType> struct StackNode { using _stackNodePtr = StackNode<_dataType>*; _dataType m_value; _stackNodePtr m_next; StackNode() : m_value(_dataType()), m_next(nullptr) {} StackNode(_dataType value) : m_value(value), m_next(nullptr) {} StackNode(_dataType value, _stackNodePtr next) : m_value(value), m_next(next) {} }; template <typename _dataType> class Stack { using _stackNodePtr = StackNode<_dataType>*; public: Stack() : m_top(nullptr), m_len(0) {} Stack(const Stack& s) { if (s.m_top) { m_top = new StackNode<_dataType>(s.m_top->m_value); _stackNodePtr top = m_top; _stackNodePtr stop = s.m_top->m_next; while (stop) { top->m_next = new StackNode<_dataType>(stop->m_value); top = top->m_next; stop = stop->m_next; } m_len = s.m_len; } else { m_top = nullptr; m_len = 0; } } ~Stack() { while (this->m_top) { this->pop(); } } Stack& operator = (const Stack& s) { while (this->m_top) { this->pop(); } if (s.m_top) { m_top = new StackNode<_dataType>(s.m_top->m_value); _stackNodePtr top = m_top; _stackNodePtr stop = s.m_top->m_next; while (stop) { top->m_next = new StackNode<_dataType>(stop->m_value); top = top->m_next; stop = stop->m_next; } m_len = s.m_len; } else { m_top = nullptr; m_len = 0; } return *this; } void push(_dataType value) { m_top = new StackNode<_dataType>(value, m_top); m_len++; } void pop() { assert(m_top); _stackNodePtr top = m_top; m_top = m_top->m_next; delete top; top = nullptr; m_len--; } _dataType& top() { assert(m_top); return m_top->m_value; } bool empty() { return m_top == nullptr; } size_t size() const { return m_len; } private: _stackNodePtr m_top; size_t m_len; }; #endif // !_STACK_H_
只在实现,不在解释说明。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。