实验三(3).cpp

来自「2007数据结构课程设计报告+源代码」· C++ 代码 · 共 93 行

CPP
93
字号
#include<iostream.h>
struct node
{	
int data;  node *next;
 };
enum error_code {success,underflow,overflow};
class stack
{
public:
	stack();
	~stack();
	bool kongde()const;
	error_code get_Top(int &x)const;
	error_code Push(const int x);
	error_code Pop();
private:
	int count;
	node *Top;
};
stack::stack()
{
	count=0;
	Top=NULL;
}
stack::~stack()
{
	while(!kongde())
		Pop();
}

bool stack::kongde()const
{
	return count==0;
}

error_code stack::get_Top(int &x)const
{
	if(kongde()) return underflow;
	else
		x=Top->data;
	return success;
}
error_code stack::Push(const int x)
{
    node *s=new node;
	s->data=x;
	s->next=Top;
	Top=s;
	count++;
	return success;
}
error_code stack::Pop()
{
	if(kongde())
		return underflow;
	else
	{
		node *u=Top;
	    Top=Top->next;
	    delete u;
		count--;
	return success;
	}
}

/*void p(int w)
{
	if(w>0)
	{cout<<w;
	p(w-1);	
	p(w-1);
	}
}*/

void p(int i)
{
	stack s;
	while(i>0||!s.kongde())
	{
		while(i>0)
		{			
			s.Push(i);
			i=i-1;
		}
		if(!s.kongde())
		{s.get_Top(i);s.Pop();cout<<i;i=i-1;}
	}
}
void main()
{
	p(5);
}

⌨️ 快捷键说明

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