Untitled
unknown
plain_text
3 years ago
2.0 kB
2
Indexable
#include<iostream> #include<string.h> //#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(){ this->head=NULL; this->tail=NULL; } List_stack::~List_stack(){ delete(head); delete(tail); } void List_stack::pop(){ if(this->tail==NULL) return; ListNode *cur=this->tail; this->tail=this->tail->prevPtr; delete(cur); if(this->tail==NULL) this->head=NULL; } void List_stack::push(const int &nu){ if(this->head==NULL){ this->head=new ListNode(nu); this->tail=this->head; } else{ ListNode *cur=new ListNode(nu); this->tail->nextPtr=cur; cur->prevPtr=this->tail; this->tail=cur; } } void List_stack::print(){ ListNode *cur=this->tail; if(cur==NULL) return; while(cur->prevPtr!=NULL){ cout<< cur->data <<" "; cur=cur->prevPtr; } cout<< cur->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; }
Editor is loading...