⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shuxuzhan.cpp

📁 本源码是关于数据结构的顺序栈的操作的源码
💻 CPP
字号:
#include <iostream>
#include <assert.h>
using namespace std;
template <class type>
class stack
{
	public:
		stack(int=10);
		~stack(){delete[]elements;}
		void push(type item);
		type pop();
		type gettop();
		void makeempty(){top=-1;}
		int isempty(){return top==-1;}
		int isfull(){return top==maxsize-1;}
	private:
		int top;
		type *elements;
		int maxsize;
};
template<class type>stack<type>::stack(int s)
{
	top=-1;
	maxsize=s;
	elements=new type[maxsize];
	assert(elements!=0);
}
template <class type>void stack<type>::push(type item)
{
	assert(!isfull());
	elements[++top]=item;
}
template <class type>type stack<type>::pop()
{
	assert(!isempty());
	return elements[top--];
}
template <class type>type stack<type>::gettop()
{
	assert(!isempty());
	return elements[top];
}
void main()
{
	int i,n,l,m,k,p;
	 
	cout<<"请输入你要建的栈的元素个数N."<<endl;
	cin>>n;
	stack<int> st(n);
	cout<<"输入你要进栈的元素个数L<n"<<endl;
	cin>>l;
    for(i=0;i<l;i++)
	{
		cout<<"输入一个你要进栈的元素M"<<endl;
		cin>>m;
        st.push(m);
	}
	cout<<"出栈元素为:"<<endl;
	k=st.pop();
    cout<<k<<endl;
    cout<<"获取栈顶元素为:"<<endl;
	p=st.gettop();
    cout<<p<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -