c ++ realize the order of the stack

Stack operation is a restricted linear table , the latter is advanced out of the data structure is defined only insertions and deletions at one end, called the top of the stack to allow an end of the operation, the operation is referred to as a bottom of the stack is not allowed 

Therefore member variables required are as follows

int   * _stack;     // first address points to the application of space 
int Top;            // record the location of the stack 
int size;          // record size application space

It follows encapsulated in the class

#include <the iostream> 
#include <stdlib.h> 
#include < String .h>
 the using  namespace STD;
 class SeqStack 
{ 
  public : 
    SeqStack ( int size = 10 )        // constructor initializes 
    { 
     _stack The = new new  int [size];      
     to _top = 0 ; 
     _size = size; 
    } 
    SeqStack ( const SeqStack & the src)    // copy constructor (shallow copy preventing) 
    { 
      _stack The= New new  int [src._size];
       int I = 0 ;
       for (; I <src._top; I ++ ) 
      { 
        _stack The [I] = src._stack [I]; 
      } 
      _size = src._size; 
      to _top = src._top ; 
    } 
    SeqStack (SeqStack && the src)        // rvalue copy function to prevent the waste of time of the temporary amount of space 
    { 
      _stack the = src._stack; 
      _size = src._size; 
      to _top = src._top; 
      src._stack = nullptr a; 
    }
    SeqStack & operator = ( const SeqStack & the src)       // assignment constructor returns continuous address assignment 
    {
       IF ( the this == & the src) 
      { 
        return * the this ; 
      } 
      Delete [] _stack The; 
      _stack The = new new  int [src._size];
       int = I 0 ;
       for (; I <src._top; I ++ ) 
      { 
        _stack The [I] = src._stack [I]; 

      } 
      _size = src._size; 
      to _top= Src._top;
       return * the this ; 
    } 
    SeqStack & operator = (SeqStack && the src)   // rvalue temporary assignment constructor prevent the amount of time wasted space 
    {
       Delete [] _stack The; 
      _stack The = src._stack; 
      _size = src._size ; 
      to _top = src._top; 
      src._stack = nullptr a;
       return * the this ; 
    }
    ~ SeqStack ()       // destructor 
   {
      Delete [] _stack The; 
     _stack The =nullptr a; 
   } 
   void Push ( int Val)   // stack insertion 
   {
      IF (Full ()) 
     { 
       a resize ();           // call twice expansion 
     } 
     _stack The [to _top] = Val; 
     to _top ++ ; 
   } 
   void POP ()           / /   stack remove 
   {
      IF (empty ()) 
     { 
      return ; 

     } 
     to _top - ; 
   } 
   int Top ()         // returns the top element 
   {
      returnmpstack [_top- . 1 ]; 
   } 
   BOOL empty ()       // is empty 
   {
     return to _top == 0 ; 
   } 
   BOOL Full ()      //   whether full 
   {
      return to _top == _size; 
   } 
  Private :
     int * _stack The;
     int to _top ;
     int _size;
     void a resize ()     //   2-fold expansion function 
    {
      int * S = new new  int [_size * 2 ];
      int i=0;
     for(;i<_size;i++)
     {
     s[i]= _stack[i];
     }
     delete []_stack;
     _stack=s;
     _size=2*_size;
    }
};
int main()
{
    SeqStack st1(20);
    SeqStack st2(st1);
    SeqStack st3=st1;
    st2=st1;
      st1.push(1);
      cout<<st1.top()<<endl;
    cout<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lc-bk/p/11571728.html