Untitled

                Never    
C++
       
#include <string.h>
#include <iostream>
#include "function.h"

using namespace std;

class ListNode
{
    friend class List_stack; //make List_stack a friend
    public:
        ListNode( const int &info ) //constructor
        : data( info ), nextPtr( NULL ), prevPtr( NULL )
        {
        } //end ListNode constructor

    private:
        int data; //data
        ListNode *nextPtr; // next node in list
        ListNode *prevPtr;
}; //end class ListNode

class List_stack {
    public:
        List_stack();
        ~List_stack();
        void push(const int &);
        void pop();
        void print();
    private:
        ListNode *head;
        ListNode *tail;
};

List_stack::List_stack()
{
    head = tail = nullptr;
}

List_stack::~List_stack()
{
    ListNode* p = head;
    ListNode* q = p;
    while (p) {
        p = p->nextPtr;
        delete q;
        q = p;
    }
}

void List_stack::push(const int& n)
{
    if (head == nullptr) {
        head = tail = new ListNode(n);        
    } else {
        tail->nextPtr = new ListNode(n);
        tail->nextPtr->prevPtr = tail;
        tail = tail->nextPtr;
    }
}

void List_stack::pop()
{
    if(tail == nullptr) return;
    if(tail->prevPtr == nullptr) {
        delete tail;
        head = tail = nullptr;
    } else {
        tail = tail->prevPtr;
        delete tail->nextPtr;
        tail->nextPtr = nullptr;
    }
}

void List_stack::print()
{
    if (tail == nullptr) return;
    ListNode* p = tail;
    cout << p->data;
    p = p->prevPtr;
    while (p != nullptr) {
        cout << " " << p->data;
        p = p->prevPtr;
    }
}

int main(){
    List_stack L_stack;
    char command[10];
    int n;
    while(cin>>command){

        if(strcmp(command,"pop")==0){
            L_stack.pop();
        }else if(strcmp(command,"push")==0){
            cin >> n;
            L_stack.push(n);
        }else if(strcmp(command, "print") == 0){
            L_stack.print();
            cout << endl;
        }
    }
    return 0;
}
/*
push 2
push 120
pop
print
pop
print
push 22
push 35
print
*/

Raw Text