Untitled
unknown
plain_text
4 years ago
2.0 kB
3
Indexable
#include<iostream>
#include<string.h>
#include <stdlib.h>
//#include "function.h"
using namespace std;
//#ifndef FUNCTION_H
#define FUNCTION_H
#include <iostream>
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;
};
void List_stack::pop(){
if(this->tail==nullptr) return;
ListNode *cur=this->tail;
this->tail=this->tail->prevPtr;
delete(cur);
}
void List_stack::push(const int &nu){
if(head==nullptr){
//head=(ListNode)malloc(sizeof(ListNode));
this->head=new ListNode(nu);
this->tail=this->head;
//head->data=nu;
}
else{
//ListNode cur=(ListNode)malloc(sizeof(ListNode));
//cur->data=nu;
ListNode *cur=new ListNode(nu);
this->tail->nextPtr=cur;
cur->prevPtr=this->tail;
this->tail=cur;
}
}
void List_stack::print(){
ListNode *cur=tail;
while(cur->prevPtr!=nullptr){
cout<< cur->data <<" ";
cur=cur->prevPtr;
}
cout<< cur->data <<"\n"<<endl;
}
//#endif // FUNCTION_H
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...