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

📄 lianshizhan.cpp

📁 本源码是关于数据结构的链式栈的操作的源码
💻 CPP
字号:
#include <iostream>
#include <assert.h>
using namespace std;
template <class type> class stack;
template <class type> class stacknode
{
	friend class stack<type>;
	private:
		type data;
		stacknode<type> *link;
		stacknode(type d=0,stacknode<type> *l=NULL)
		{
			data=d;
			link=l;
		}
};
template <class type> class stack
{
	public:
		stack(){top=NULL;}
		~stack();
		void push(type item);
		type pop();
		type gettop();
		void makeempty();
		int isempty(){return top==NULL;}
	private:
		stacknode<type> *top;
};
template <class type> stack<type>::~stack()
{
	stacknode<type> *p;
	while(top!=NULL){p=top;top=top->link;delete p;}
}
template <class type>void stack<type>::push(type item)
{
	top=new stacknode<type> (item,top);
}
template <class type>type stack<type>::pop()
{
	assert(!isempty());
	stacknode<type> *p=top;
	type retvalue=p->data;
	top=top->link;
    delete p;
	return retvalue;
}
template <class type>type stack<type>::gettop()
{
	assert(!isempty());
	return top->data;
}
void main()
{
   int i,m,n,k;
   stack<int> st;
   cout<<"输入你要建栈元素个数M"<<endl;
   cin>>m;
   for(i=1;i<=m;i++)
   st.push(i);
   n=st.pop();
   cout<<"删除栈顶元素为:"<<endl;
   cout<<n<<endl;
   k=st.gettop();
   cout<<"输出栈顶元素为:"<<endl;
   cout<<k<<endl;
}


⌨️ 快捷键说明

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