C++實現堆棧

    棧(stack)又名堆棧,它是一種運算受限的線性表。其限制是僅容許在表的一端進行插入和刪除運算。經分析,C++實現堆棧,程序應實現入棧、出棧、判斷棧的狀態(主要是判斷棧是否爲空,是否爲滿)、獲取棧頂元素、求棧的長度、清空棧中元素、輸出棧中元素、銷燬棧這八大功能。因而,寫了一個利用數組實現這些功能的簡單的程序。ios

#include<iostream>
using namespace std;
const int maxsize=5;
class Stack
{
	public:
		Stack()   //構造函數,定義一個空棧
		{
			a=new int[maxsize];
			top=0;
		}
		~Stack(){}  //析構函數
		void Push(int e);   //入棧
		void Pop();    //出棧
		void GetTop();   //讀棧頂元素
		int StackSize();   //求棧長
		void ClearStack(Stack s);   //清空棧
		bool IsEmpty();   //判斷棧是否爲空
		bool IsFull();   //判斷棧是否爲滿
		bool Destroy();   //銷燬棧
		void Print();   //輸出棧中元素
	private:
		int *a;
		int top;
};
void Stack::Push(int e)
{
	if(!IsFull())
	{
		a[top++]=e;
	}
	else
        cout<<"棧已滿,"<<e<<"未入棧!"<<endl;
}
void Stack::Pop()
{
	if(!IsEmpty())
	{
		top--;
	}
	else
        cout<<"棧爲空!"<<endl;
}
void Stack::GetTop()
{
	cout<<"棧頂元素爲:"<<a[top-1]<<endl;
}
int Stack::StackSize()
{
	return top;
}
void Stack::ClearStack(Stack s)
{
	while(top!=0)
	{
        s.Pop();
        top--;
	}
}
bool Stack::IsEmpty()
{
	if(top==0)
        return true;
	else
        return false;
}
bool Stack::IsFull()
{
	if(top>=maxsize)
        return true;
	else 
        return false;
}
bool Stack::Destroy()
{
	delete this;
	return true;
}
void Stack::Print()
{
	if(!IsEmpty())
	{
        int i=top-1;
        cout<<"棧內元素爲:";
        while(i>=0)
        {
            cout<<a[i]<<"  ";
            i--;
        }
        cout<<endl;
	}
	else
        cout<<"棧爲空!"<<endl;
}
void function(Stack S)
{
	int n,e,i,j,k=1;
	while(k){
        cout<<"Please choose one function\n1:入棧\n2:求棧長\n3:讀棧頂元素\n4:出棧\n5:判空\n6:判滿\n7:輸出棧\n8:將棧清空\n9:銷燬棧\n10:退出"<<endl;
        cin>>i;
        switch(i)
			{
                case 1:
					j=0;
                    cout<<"Please input the number of elements less than "<<maxsize<<":"<<endl;
                    cin>>n;
                    if(n>maxsize)
                    {
                        cout<<"error,please input again:"<<endl;
                        cin>>n;
                    }
                    while(j<n)
                    {
                        cout<<"Please input new element:"<<endl;
                        cin>>e;
                        S.Push(e);
                        j++;
                    }
                    break;
                case 2:
                    cout<<"棧的長度爲:"<<S.StackSize()<<endl;
                    break;
                case 3:
                    S.GetTop();
                    break;
                case 4:
                    S.Pop();
                    cout<<"已出棧!"<<endl;
                    break;
                case 5:
                    if(S.IsEmpty())
                        cout<<"該棧爲空!"<<endl;
                    else
                        cout<<"該棧不空!"<<endl;
                    break;
                case 6:
                    if(S.IsFull())
                        cout<<"該棧已滿!"<<endl;
                    else
                        cout<<"該棧未滿!"<<endl;
                    break;
                case 7:     
                    S.Print();
                    break;
                case 8:
                    S.ClearStack(S);
                    cout<<"已清空!"<<endl;
                    break;
                case 9:
                    cout<<"棧已銷燬!"<<endl;
                case 10:
                    k=0;
                    break;
            }
   }
}
int main()
{
	Stack St;
	function(St);
	return 0;
}
相關文章
相關標籤/搜索