c_cpp 使用类和链接列表实现堆栈

Posted

tags:

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

#include<bits/stdc++.h>
using namespace std;

// #Stack #LinkedList #Class #BasicProblem
// Push, Pop operations are performed at head of ll ie., head is considered top

class node{
    public:
    int data;
    node *next;
    node(){ //constructor
        data=0;
        next=NULL;
    }
    node(int x){ //constructor with integer
        data=x;
        next=NULL;
    }
};
class Stack{
    public:
    node* top; //taking top as head of ll
    Stack(){ //constructor
        top=NULL;
    }
    bool isEmpty(); //checks if the Stack is empty
    bool Top(int &t); //returns top element of stack
    void Push(int x); //adds an element at the top of stack
    bool Pop(); //deletes the top element of stack
};
bool Stack :: isEmpty(){
    if(top==NULL){
        return true;
    }
    return false;
}
bool Stack :: Top(int &t){
    if(top==NULL){
        return false;
    }else{
        t=top->data;
        return true;
    }
}
void Stack :: Push(int x){
    node* n1=new node(x);
    if(top==NULL){
        top=n1;
    }else{
        n1->next=top;
        top=n1;
    }
}
bool Stack :: Pop(){
    if(top==NULL){
        return false;
    }else{
        node* temp=top;
        top=temp->next;
        //free(temp);
        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;
            S.Push(push);
                cout<<"Element successfully pushed"<<endl;
                if(S.Top(top)==true){
                    cout<<"Top Element is:"<<top<<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 使用Struct实现链接列表

c_cpp 链接列表实现与类

c_cpp 链接列表的线性队列实现

c_cpp 队列 - C ++中的链接列表实现