数据结构——栈的工作原理

栈的工作原理

  • 主函数

栈的核心原理:先进后出规则

Mystack *pStack = new Mystack(5);//初始化操作
对象创建在堆上直接赋值给指针,结束时需要用delete释放内存,并将pStack指向空

Mystack mystack(4);在栈上创建对象,函数结束时无需释放内存,系统自动退栈

#include <iostream>
#include "Mystack.h"

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	Mystack *pStack = new Mystack(5);//初始化操作 
	

	pStack->push( 'a' ); //底 
	pStack->push( 'b' ); 
	pStack->push( 'c' ); 
	pStack->push( 'd' ); 
	pStack->push( 'e' ); //顶 

	cout << "The length of Stack is "<<pStack->stackLength() << endl << endl; 
 
	pStack->stackTraverse();

	char elem;
	pStack->pop(elem);
	cout << "The out element is " << elem << endl << endl;
	pStack->stackTraverse();
	cout << "The length of Stack is "<<pStack->stackLength() << endl << endl; 
 
		
	if(pStack->stackEmpty())//判断是否为空 
		cout << "Empty" << endl;
	Mystack mystack(4);
	mystack.push('j');
	mystack.stackTraverse();
		cout << endl << endl;
	cout << "The length of the Stack is " << mystack.stackLength();
	
	delete pStack;
	pStack = NULL;
	return 0;
}
  • Mystack头文件
  class Mystack
        {
        public:
        	Mystack(int size);
        	~Mystack();
        	bool stackEmpty();
        	bool stackFull();
        	void clearStack();
        	int stackLength();
        	bool push(char elem);		
        	bool pop(char  &elem);
        	void stackTraverse();
        protected:
        	char *m_p;//栈空间指针
        	int m_iSize;
        	int m_iTop;
        }
  • Mystack cpp文件

    #include <iostream>
     #include "Mystack.h"
     
     using namespace std;
     
     Mystack:: Mystack(int size){
     	m_iSize = size;
     	m_p = new char[size];
     	m_iTop = 0;
     }
     
     Mystack::~Mystack(){
     	delete []m_p;
     }
     
     bool Mystack::stackEmpty(){
     	if( 0 == m_iTop ){ 	
     		return true;
     	}
     	else 
     		return false;
     }
     
     bool Mystack::stackFull(){
     	if( m_iSize == m_iTop )
     		return true;
     	else
     		return false;
     }
     
     void Mystack::clearStack(){
     	m_iTop = 0;
     }
     
     int Mystack::stackLength(){
     	return m_iTop;
     }
     
     bool Mystack::push(char elem){
     	if( stackFull() ){
     		return false;
     	}
     	
     	m_p[m_iTop] = elem;
     	m_iTop++;
     	return true;
     }
     
     bool Mystack::pop( char &elem){
     	if( stackEmpty() )
     		return false;
     	else 
     		elem = m_p[--m_iTop];
     	return true;
     }
     
     void Mystack::stackTraverse(){
     	int temp = 0;
     	while( temp < m_iTop ){
     	cout << m_p[temp] << " ";
     		temp ++;
     	} 
     }
    

演示结果:
在这里插入图片描述

  • 学习慕课栈模版和栈事例

注:本文知识从慕课james_yuan学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玖玖玖_violet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值