• 链式栈


    StackNode.h

    template<typename Type> class LinkStack;
    
    template<typename Type> class StackNode{
    private:
    	friend class LinkStack<Type>;
    	StackNode(Type dt,StackNode<Type> *next=NULL):m_data(dt),m_pnext(next){} 
    
    private:
    	Type m_data;
    	StackNode<Type> *m_pnext;
    };
    

    LinkStack.h

    #include "StackNode.h"
    
    template<typename Type> class LinkStack{
    public:
    	LinkStack():m_ptop(NULL){}
    	~LinkStack(){
    		MakeEmpty();
    	}
    
    public:
    	void MakeEmpty();           //make the stack empty
    	void Push(const Type item); //push the data
    	Type Pop();                 //pop the data
    	Type GetTop() const;        //get the data
        void Print();               //print the stack
            
    	bool IsEmpty() const{
    		return m_ptop==NULL;
    	}
    	
    private:
    	StackNode<Type> *m_ptop;
    };
    
    template<typename Type> void LinkStack<Type>::MakeEmpty(){
    	StackNode<Type> *pmove;
    	while(m_ptop!=NULL){
    		pmove=m_ptop;
    		m_ptop=m_ptop->m_pnext;
    		delete pmove;
    	}
    }
    
    template<typename Type> void LinkStack<Type>::Push(const Type item){
    	m_ptop=new StackNode<Type>(item,m_ptop);
    }
    
    template<typename Type> Type LinkStack<Type>::GetTop() const{
    	if(IsEmpty()){
    		cout<<"There is no elements!"<<endl;
    		exit(1);
    	}
    	return m_ptop->m_data;
    }
    
    template<typename Type> Type LinkStack<Type>::Pop(){
    	if(IsEmpty()){
    		cout<<"There is no elements!"<<endl;
    		exit(1);
    	}
    	StackNode<Type> *pdel=m_ptop;
    	m_ptop=m_ptop->m_pnext;
    	Type temp=pdel->m_data;
    	delete pdel;
    	return temp;
    }
    
    template<typename Type> void LinkStack<Type>::Print(){
    	StackNode<Type> *pmove=m_ptop;
    	cout<<"buttom";
    	while(pmove!=NULL){
    		cout<<"--->"<<pmove->m_data;
    		pmove=pmove->m_pnext;
    	}
    	cout<<"--->top"<<endl<<endl<<endl;
    }
    

    Test.cpp

    #include <iostream>
    using namespace std;
    
    #include "LinkStack.h"
    
    int main(){
    	LinkStack<int> stack;
    	int init[10]={1,3,5,7,4,2,8,0,6,9};
    	for(int i=0;i<10;i++){
    		stack.Push(init[i]);
    	}
    	stack.Print();
    
    	cout<<stack.Pop()<<endl;
    	stack.Print();
    	
    	cout<<stack.GetTop()<<endl;
    	stack.Print();
    
    	cout<<stack.Pop()<<endl;
    	stack.Print();
    
    	stack.MakeEmpty();
    	stack.Print();
    	
    	stack.Pop();
    
    
    	return 0;
    }
    
  • 相关阅读:
    为什么全局变量一定要初始化?
    SecureCRT 使用密钥登录 Ubuntu
    ubuntu samba 服务器搭建
    Linux 软硬链接的区别及目录权限对软硬链接的影响
    【转载】解析 java 按值传递还是按引用传递
    (转)如何学好C语言
    (转)五个方法成为更好的程序员
    (转)SQLite数据库的加密
    鸟哥Linux私房菜(基础篇)——第十一章:认识与学习Bash
    鸟哥Linux私房菜(基础篇)——第五章:首次登入与在线求助 man page笔记
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2438199.html
Copyright © 2020-2023  润新知