Stack is queue-based, just need to change a little bit OK, paste the code below
Stack.h
#include <cstdio> #include <cassert> #include <iostream>using namespace Std;class stack{public://* * * Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/* * * * Stack (); Stack (const stack &stack); stack& operator= (const Stack &stack); ~stack ();//********************************************************* Additions and deletions//**************************************************************************void Push ( Const int& data), int Pop (), unsigned short Size (), bool IsEmpty (), void Printstack ();p rivate:void free ();p rivate: typedef struct NODE{NODE (int d):d ata (d), next (NULL) {}int data;struct node* next; Node; node* M_head; node* m_tail;unsigned Short m_size;};/ /**************************************************************************//Private method//**************************** void Stack::free () {if (m_head{node* tmp = Null;while (m_head) {tmp = M_head;m_head = M_head->next;delete tmp;}} M_head = Null;m_tail = Null;m_size = 0;} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************stack::stack () {m_head = NULL;m_tail = null;m_size = 0;} Stack::stack (const Stack &stack) {m_head = Null;m_tail = Null;m_size = Stack.m_size;if (stack.m_head) {Node* tmp = Stack . M_head; node* q = null;while (tmp) {node* node = new node (tmp->data), if (!m_head) {m_head = Node;q = M_head;} Else{q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head = Null;m_tail = NULL;}} stack& stack::operator= (const Stack &stack) {free (); m_size = Stack.m_size;if (stack.m_head) {node* tmp = Stack.m_ Head node* q = null;while (tmp) {node* node = new node (tmp->data), if (!m_head) {m_head = Node;q = M_head;} Else{q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head =Null;m_tail = NULL;} return *this;} Stack::~stack () {if (m_head) {node* TMP = M_head;while (tmp) {m_head = M_head->next;delete tmp;tmp = M_head;}} M_head = Null;m_tail = NULL;} Additions/deletions//*************************** void Stack::P ush (const int& data) {node* TMP = new Node (data); if (!m _head) {m_head = Tmp;m_tail = tmp;} Else{tmp->next = M_head;m_head = tmp;} ++m_size;} int Stack::P op () {assert (M_size > 0); node* tmp = M_head;m_head = M_head->next;int val = Tmp->data;delete tmp;--m_size;return val;} unsigned short stack::size () {return m_size;} BOOL Stack::isempty () {return (m_size = = 0);} void Stack::P rintstack () {cout << "size:" << m_size << ", content:"; if (m_head) {node* tmp = M_head;w Hile (TMP) {cout << tmp->data << ", tmp = Tmp->next;}} cout << "NULL" << Endl;}
Main.cpp
Test for Stack#include "Stack.h" #include <cstdlib> #include <iostream> #include <list>using namespace Std;int Main () {stack stack;for (int i = 1; i < 5; ++i) {stack. Push (i);} Stack. Printstack (); Stack stackcopy (stack); Stackcopy.printstack (); Stack Stackassig;stackassig = Stack;stackassig.printstack (); int pop = stack. Pop (); cout << "pop:" << pop << endl;stack. Printstack (); System ("pause"); return 0;}
Output
Simple implementation of 5:c++ stack based on algorithm and data structure