c_cpp 使用类和数组实现堆栈

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用类和数组实现堆栈相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define MAX_SIZE 10 //defining max size of stack (since array)
using namespace std;

// #Stack #Class #BasicProblem

class Stack{
    public:
       int a[MAX_SIZE];
       int stack_size; //size of stack
       bool isEmpty(); //checks if stack is empty
       bool Full(); //checks if stack is full
       bool Top(int &top); //returns top element
       bool Push(int element); //Pushes element onto stack
       bool Pop(); //Pops top element and updates element
       Stack(){ //constructor
            stack_size=0; //initializing size of stack as zero
       }
};
bool Stack :: isEmpty(){
    if(stack_size==0){
        return true;
    }
    return false;
}
bool Stack :: Full(){
    if(stack_size==MAX_SIZE){
        return true;
    }
    return false;
}
bool Stack :: Top(int &top){
    if(isEmpty()==true){
        return false;
    }else{
        top=a[stack_size-1];
        return true;
    }
}
bool Stack :: Push(int element){
    if(Full()==true){
        return false;
    }else{
        a[stack_size]=element;
        stack_size++;
        return true;
    }
}
bool Stack :: Pop(){
    if(isEmpty()==true){
        return false;
    }else{
        stack_size--;
        return true;
    }
}

int main(){
    cout<<"Instructions: \n";
    cout<<"Type add to push onto stack"<<endl;
    cout<<"Type del to pop from stack"<<endl;
    cout<<"Type top to check the top element in stack"<<endl;
    cout<<"Type exit to stop using the stack"<<endl;
    Stack S;
    int top;
    while(1){
        string instruction;
        cout<<"Instruction: ";
        cin>>instruction;
        if(instruction=="exit"){
            break;
        }else if(instruction=="add"){
            cout<<"Enter the element top be pushed"<<endl;
            int push; //element to be pushed
            cin>>push;
                if(S.Push(push)==true){
                    cout<<"Element successfully pushed"<<endl;
                    if(S.Top(top)==true){
                        cout<<"Top Element is:"<<top<<endl;
                    }
                }else{
                    cout<<"ERROR:  Stack is already full!"<<endl;
                }
        }else if(instruction=="del"){
            if(S.Pop()==true){
                cout<<"Element was successfully popped"<<endl;
                if(S.Top(top)==true){
                    cout<<"Top Element is:"<<top<<endl;
                }else{
                    cout<<"Stack is now Empty!"<<endl;
                }
            }else{
                cout<<"ERROR : Stack is empty!"<<endl;
            }
        }else if(instruction=="top"){
                if(S.Top(top)==true){
                    cout<<"Top Element is:"<<top<<endl;
                }else{
                    cout<<"ERROR : Stack is empty!"<<endl;
                }
        }else{
            cout<<"ERROR : Unknown operation! Please try again"<<endl;
        }
    }
    return 0;
}

以上是关于c_cpp 使用类和数组实现堆栈的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用类和向量进行堆栈实现

c_cpp 使用数组实现堆栈

c_cpp 使用在堆栈上声明的数组(int myArray [3])和堆上的数组(int * myArray = new int [size]

c_cpp 使用Vector PopBack进行堆栈实现

c_cpp 按堆栈实现队列

c_cpp 将队列和堆栈实现添加到NSMutableArray类