Untitled

                Never    
C++
       
#include<iostream>
#include<string.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;
};
//#include<iostream>
//#include<string.h>
//#include "function.h"
//using namespace std;
List_stack::List_stack(){
    head=new ListNode(0);
    //tail=head;
    tail=new ListNode(0);
    head->nextPtr=tail;
    tail->prevPtr=head;
}
List_stack::~List_stack(){
    for(ListNode* tmp=head;tmp!=tail;tmp=head){
        head=head->nextPtr;
        delete[] tmp;
    }
}
void List_stack::push(const int & n){
    ListNode* newNode=new ListNode(n);
    tail->prevPtr->nextPtr=newNode;
    newNode->prevPtr=tail->prevPtr;
    tail->prevPtr=newNode;
    newNode->nextPtr=tail;
}
void List_stack::pop(){
    if(tail->prevPtr==head)return;
    tail->prevPtr->prevPtr->nextPtr=tail;
    ListNode* tmp=tail->prevPtr;
    tail->prevPtr=tail->prevPtr->prevPtr;
    delete[] tmp;
}
void List_stack::print(){
    ListNode* cur=tail->prevPtr;
    if(cur!=head)cout<<cur->data;
    else return;
    cur=cur->prevPtr;
    for(ListNode* tmp=cur;tmp!=head;tmp=cur){
        cur=cur->prevPtr;
        cout<<' '<<tmp->data;
    }
}
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;
}

Raw Text