c++中的 栈 stack实现

C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。

c++ stl栈stack的头文件为

#include <stack> 

c++ stl栈stack的成员函数介绍

操作 比较和分配堆栈

empty() 堆栈为空则返回真

pop() 移除栈顶元素

push() 在栈顶增加元素

size() 返回栈中元素数目

top() 返回栈顶元素


stack.cpp

#include<iostream>

template<class T>
class stackNode
{
 public:
    stackNode():next(NULL){}
    T data;//值
    stackNode* next;//指向下一个节点的指针
};

template<class T>
class mystack
{
private:
    unsigned int stacklength;
    stackNode<T>* node;//临时节点
    stackNode<T>* headnode;//尾结点
    public:
        mystack();//初始化
        unsigned int length();//栈元素的个数
        void push(T x);//入栈
        bool isEmpty();//判断栈是否为空
        void pop();//出栈
        T top();//获得栈顶元素
        void clear();//清空栈

};


template<class T>
mystack<T>::mystack()
{
    node=NULL;
    headnode=NULL;
    stacklength=0;
}


template<class T>
inline unsigned int mystack<T>::length(){return stacklength;}


template<class T>
void  mystack<T>::push(T x)
{
    node=new stackNode<T>();
    node->data=x;
    node->next=headnode;//把node变成头节点
    headnode=node;
    ++stacklength;
}


template<class T>
bool  mystack<T>::isEmpty()
{
    return stacklength==0;
}


template<class T>
void  mystack<T>::pop()
{
    if(isEmpty()) return;
    node=headnode;
    headnode=headnode->next;//头节点变成它的下一个节点
    delete(node);//删除头节点
    --stacklength;
}


template<class T>
T  mystack<T>::top()
{
    if(!isEmpty())
    return headnode->data;
}


template<class T>
void  mystack<T>::clear()
{
    while(headnode!=NULL)
    {
        node=headnode;
        headnode=headnode->next;
        delete(node);
    }
    node=NULL;
    headnode=NULL;
    stacklength=0;
}

using namespace std;

int main(int argc,char **argv)
{
	mystack<int>  *p = new mystack<int>;

	int a=10;
	p->push(a);
	p->push(a+1);
	p->push(a+2);
	p->push(a+3);
	p->push(a+4);

	for(int i=0;i<5;i++){
		cout << p->top() << "len=" << p->length() << endl;
		p->pop();
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_21792169/article/details/80898747