본문 바로가기

Data Structure

Stack 구현

#include <iostream> 

using namespace std; 

struct node
{
       int data;
       node* p;
};

class stack
{
public:
       node* head;
       node* tail;      
       int sz;      
  
       stack()
       {
               head = NULL;
               sz = 0;
               tail = NULL;
       }       
       void push(int num)
       {
              node* new_node = new node; 
              new_node ->data=num;
              new_node ->p=tail;
              tail=new_node;
              sz++;
       }      
       int pop()
       {
               if(isEmpty())
               {
                     return -1;
                }
               int tmp = tail->data;
              
               node* del_node = tail;
               tail = tail->p;
               delete del_node;
               sz--;
            
               return tmp;
         }       
        int top()
        {
               if(isEmpty())
               {
                     return -1;
               }
               return tail->data;
        }        
        int size()
        {
              return sz;
        }        
        bool isEmpty()
        {
              if(sz==0)
              {
                      return true;
              }
              else
              {
                      return false;
              }
         }
};

int main()
{
         stack a;
         int sel;
         int num;        
   
         while(1)
         {
               cout << "1.push"<<endl;
               cout << "2.pop"<<endl;
               cout << "3.top"<<endl;
               cout << "4.size"<<endl;
               cout << "5.all pop"<<endl;
               cout << "6.quit" << endl;
               cin >> sel;              
               if(sel==1)
               {
                      cout << "please input number" <<endl;
                      cin >> num;                      
               a.push(num);
               }
               else if(sel==2)
               {
                      num = a.pop();
                      if(num==-1)
                      {
                              cout <<"pop error!"<<endl<< endl;
                      }
                      else
                      {
                              cout <<"pop num is :" << num << ndl<<endl;
                      }
               }
               else if(sel==3)
               {
                      num=a.top();
                      if(num==-1)
                      {
                              cout << "top error!" <<endl<< endl;
                      }
                      else
                      {
                              cout <<"top num is : "<<num << endl << endl;
                      }
               }
               else if(sel==4)
               {
                      cout << "size is : " << a.size() <<endl << endl;
               }
               else if(sel==5)
               {
                      while(!a.isEmpty())
                      {
                              cout << a.pop() << " ";
                      }
                      cout << endl << endl;
                }
               else if(sel==6)
               {
                      break;
                }
         }        

         return 0;
}