#ifndef _cstack_h_ # define _cstack_h_
Class cstack // Stack class {public: // General constructor, copy constructor, assignment operator, destructor cstack (); Cstack (const cstract cstract); cstack & operator = (const cstack & rhs); ~ cstack () ;
// Fault Push (Double Element); // Find Void Pop (Void); / / View Stack Top Elements Double Top (Void); / / Clear Stack Void Clear (Void); // Determine if the stack is empty BOOL Empty (Void); / / The number of elements in the current stack int size (void);
PRIVATE: STRUCT NODE / / Stack Element Structure {Struct Node * Last; // Finger The value of a pointer Double Element; // stack element of a stack element struct node * next; // Pointer to the rear stack element;
Struct node * m_head; // Save pointer struct node * m_top pointing to the first element of the stack; // Pointer to the top elements of the stack
INT m_SIZE; / / The number of elements in the stack};
#ENDIF
///
// Stack Class #include
Using namespace std;
// General constructor cstack :: cstack (): m_head (null), m_top (null), m_size (0) {m_head = new node [1]; if (m_head! = Null) {m_Head-> Last = NULL; M_Head-> Element = 0; m_head-> next = null;}}
// Copy constructor cstack :: cstack (const cstack & rhs): m_head (null), m_top (null), m_size (0) {m_head = new node [1]; if (m_head! = Null) {m_Head-> Last = NULL; M_HEAD-> Element = 0; M_HEAD-> Next = NULL;
// Define the pointer variable Node * TempNode = null; node * backnode = null;
TempNode = rhs.m_head-> next; backnode = m_head;
While (TempNode! = null) {node * newNode = null; / / This pointer variable points to the newly created node
NewNode = new node [1]; // Create a new node if (newNode == NULL) // creation failed {Return;}
Backnode-> next = newNode;
NewNode-> last = backnode; newnode-> element = tempnode-> element; newnode-> next = null;
Backnode = newNode;
m_top = newNode; // m_top points to the top element m_size ; / / The number of elements of the stack increases 1
TempNode = TempNode-> Next;} // while (TempNode! = null)} // Assignment Operator CSTACK & CSTACK :: Operator = (this! = & r HS) // Check if it is self-assignment { // Define the pointer variable Node * TempNode = null; node * backnode = null;
/ / Delete the original stack element if (m_head! = Null) {backnode = m_head-> next; while (backnode! = Null) {tempnode = backnode-> next; delete [] backnode; backNode = tempnode;
m_top = null; m_size = 0;
m_head-> next = null;} else {m_head = new node [1]; if (m_head == null) {return * this; // Return to this object's reference}
m_head-> last = null; m_head-> element = 0; m_head-> next = NULL;}
/ / Copy the stack element from the RHS object tempnode = rhs.m_head-> next; backnode = m_head;
While (TempNode! = null) {node * newNode = null; / / This pointer variable points to the newly created node
NewNode = new node [1]; // Create a new node if (newNode == null) // creates failed {return * this; // Return to this object's reference}
Backnode-> next = newNode;
NewNode-> last = backnode; newnode-> element = tempnode-> element; newnode-> next = null;
Backnode = newNode;
m_top = newNode; // m_top points to the top element m_size ; / / The number of elements of the stack increases 1
TempNode = TempNode-> next;} // while (tempnode! = null)}
Return * this; // Return to this object's reference}
// Destructor Cstack :: ~ cstack () {i (m_head! = Null) {// Define Pointer Variable Node * TempNode = NULL; Node * BackNode = NULL;
/ / Release the memory space occupied by the stack element backnode = m_head; while (backnode! = Null) {tempnode = backnode-> next; delete [] backnode; backnode = tempnode;}}}
// Final Stack Void Cstack :: Push (Double Element) {node * new = null;
NewNode = new node [1]; // Create a new node if (newNode! = null) {if (m_top == null) // Stack still has no element {m_head-> next = newNode; NewNode-> Last = m_head; } Else {m_top-> Next = newNode; newNode-> last = m_top;} newnode-> element = element; newnode-> next = NULL;
m_top = newnode; // newNode is the new stack top element m_size ; / / The number of elements in the stack is 1}}
/ / Finding void cstack :: POP (void) {// Judging whether the stack is empty if (m_top! = Null) {node * TempNode = NULL;
TempNode = m_top-> last; delete [] m_top; // Remove the top element M_TOP = TempNode;
IF (m_top == m_head) // There is no element {m_top = null in the stack; m_head-> next = null;}
m_size - the number of elements in the stack is 1}}}
/ / View Stack Top Elements Double Cstack :: TOP (Void) {i (m_top! = NULL) {Return M_top-> Element;}
Return 0;}
/ / Clear Stack Void Cstack :: CLEAR (Void) {if (m_head! = NULL) {// Define Pointer Variable Node * TempNode = NULL; Node * BackNode = NULL;
Backnode = m_head-> next; while (backnode! = null) {tempnode = backnode-> next; delete [] backnode; backnode = tempnode;}
m_head-> next = null; m_top = NULL; m_size = 0;}}
/ / Judgment whether the stack is empty BOOL CSTACK :: Empty (void) {if (0 == m_size) {return true; // empty}
Return false; // is not empty}
// Elements in the current stack INT cstack :: size (void) {return m_size;}