C++综合系列之模拟栈模版(数组版及单链表版)
来源:互联网 发布:godaddy域名转移 编辑:程序博客网 时间:2024/06/11 11:46
一:栈模版(数组版):
stack.h
#ifndef MYSTACK_ARRAY_H#define MYSTACK_ARRAY_H#include <iostream>using namespace std;template<class T>class MyStack{public: MyStack(int capacity) : m_capacity(capacity), m_top(0) { m_element = new T[capacity]; } ~MyStack() { delete []m_element; } void push(T value); //入栈 T pop(); //出栈 T top(); //获取栈顶元素 int capacity(); //获取容量 int counts(); //获取栈内元素个数 bool isEmpty(); //判断栈是否为空 void print(); //打印栈private: T *m_element; int m_top; int m_capacity;};template<class T>void MyStack<T>::push(T value){ if(m_top >= m_capacity) { cout << "push failed, the stack is full." << endl; } else { m_element[m_top++] = value; //如何实现new空间的动态增长? }}template<class T>T MyStack<T>::pop(){ if(0 == m_top) { cout << "pop failed, the stack is empty." << endl; T value; return value; } return m_element[--m_top];}template<class T>T MyStack<T>::top(){ if(0 == m_top) { cout << "top failed, the stack is empty." << endl; T value; return value; } return m_element[m_top-1];}template<class T>int MyStack<T>::capacity(){ return m_capacity;}template<class T>int MyStack<T>::counts(){ return m_top;}template<class T>bool MyStack<T>::isEmpty(){ return m_top;}template<class T>void MyStack<T>::print(){ if(0 == m_top) { cout << "print failed, the stack is empty." << endl; } else { for(int i = m_top-1; i >= 0; i--) { cout << m_element[i] << endl; } }}#endif // MYSTACK_ARRAY_H
main.cpp
#include "mystack_array.h"int main(){ MyStack<char> myStack(2); myStack.push('a'); myStack.push('b'); myStack.push('c'); myStack.print(); myStack.pop(); myStack.pop(); myStack.push('e'); myStack.push('f'); myStack.print(); cout << myStack.top() << endl; cout << myStack.counts() << endl; cout << myStack.isEmpty() << endl; return 0;}
二:栈模版(单链表版):
stack.h
#ifndef MYSTACK_LIST_H#define MYSTACK_LIST_H#include <iostream>using namespace std;template<class T>struct Node{ Node() : m_pNext(nullptr) { } Node(T value) : m_value(value), m_pNext(nullptr) { } T m_value; Node<T> *m_pNext;};template<class T>class MyStack{public: MyStack() { m_capacity = 0; m_pHead = new Node<T>; } void push(T value); //入栈 T pop(); //出栈 T top(); //获取栈顶元素 int counts(); //获取栈内元素个数 bool isEmpty(); //判断栈是否为空 void print(); //打印栈private: int m_capacity; //容量 Node<T> *m_pHead;//栈头};template<class T>void MyStack<T>::push(T value){ Node<T> *pnode = new Node<T>(value); pnode->m_pNext = m_pHead->m_pNext;//先将头节点所指向的下一个节点(也就是最后的空节点)赋值给新建节点的下一个节点,使得新建节点的next指针指向最后节点 m_pHead->m_pNext = pnode;//然后将新建节点赋值给头节点的next指针,使得头结点的next指针指向新建节点 m_capacity++;}template<class T>T MyStack<T>::pop(){ if( nullptr == m_pHead->m_pNext ) { cout << "pop failed, the stack is empty." << endl; T value; return value; } else { Node<T> *temp = m_pHead->m_pNext;//先获取头结点的下一个节点(即所需要出栈的节点) m_pHead->m_pNext = m_pHead->m_pNext->m_pNext;//然后将出栈的节点的下一节点赋值给头节点的next指针,即头指针指向出栈节点的下一节点,这样出栈节点就从链表中剔除了 T value = temp->m_value; delete temp; temp = NULL; m_capacity--; return value; }}template<class T>T MyStack<T>::top(){ if( nullptr == m_pHead->m_pNext ) { cout << "top failed, the stack is empty." << endl; T value; return value; } else { return m_pHead->m_pNext->m_value; }}template<class T>int MyStack<T>::counts(){ return m_capacity;}template<class T>bool MyStack<T>::isEmpty(){ return m_capacity;}template<class T>void MyStack<T>::print(){ if( nullptr == m_pHead->m_pNext ) { cout << "print failed, the stack is empty." << endl; return; } else { Node<T> *temp = m_pHead; while(temp->m_pNext != nullptr) { temp = temp->m_pNext; cout << temp->m_value << endl; } }}#endif // MYSTACK_LIST_H
main.cpp
#include "mystack_list.h"int main(){ MyStack<char> myStack; myStack.push('a'); myStack.push('b'); myStack.push('c'); myStack.print(); myStack.pop(); myStack.pop(); myStack.push('e'); myStack.push('f'); myStack.print(); cout << myStack.top() << endl; cout << myStack.counts() << endl; cout << myStack.isEmpty() << endl; return 0;}
阅读全文
0 0
- C++综合系列之模拟栈模版(数组版及单链表版)
- C++综合系列之模拟单链表模版
- C++综合系列之模拟事件机制
- 栈C++模版实现(数组版)
- (C语言)简单明了的 数组模拟栈+ (C++)数组模拟栈
- 树状数组 模版 及 解释
- C语言笔记系列之六——数组及冒泡排序
- C/C++面试之算法系列--二维动态数组定义及二维静态数组与**P的区别
- c语言实现数据结构中的栈(数组模拟)
- 栈(数组模拟)
- 栈的模拟(内核为数组).c
- 【C++】步步为营之综合应用
- C语言系列之C语言如何返回字符串数组
- C语言学习之数组及指针
- PHP laravel系列之Blade模版
- 第七周项目六C/C++数据结构实践——停车场模拟(栈和队列综合)
- 树状数组(讲解+模版)
- 后缀数组(SA)模版
- BZOJ 3622 已经没有什么好害怕的了
- Objective-C 语法二(对象初始化)
- PKI系统深入介绍
- twitter工程师的一点经验
- 程序员必读书籍书单
- C++综合系列之模拟栈模版(数组版及单链表版)
- Objective-C 语法三(属性值)
- 获取当前显示的viewController
- C++ STL系列(一)简介
- 服务器上无权限源代码MySQL安装
- c语言编译过程详解
- 深入mysql "ON DUPLICATE KEY UPDATE" 语法的分析
- Socket 之TCP UDP
- ubuntu 设置wifi共享