C++ 學習筆記1

來源:互聯網
上載者:User

 

Implement and test a class deque, which is a double-ended queue that allows pushing and popping at both ends.

class queue
{
public:
void reset() { top = bottom = max_len/2; top--; }
...
private:
char s[max_len];
int bottom,top;
};

Declare and implement push_t(), pop_t(), push_b(), pop_b(), print_stack(), top_of(), bottom_of(), empty(), and full(). The function push_t() stands for push on top and pop_t() for pop on top; push_b() stands for push on bottom and pop_b() for pop on bottom.
The print_stack() function should output the stack from bottom to top. An empty stack is denoted by having the top fall below the bottom. Test each function.

 

//本程式用VC-SP6編寫,調試通過

#include<iostream>
#include<iomanip>
using namespace std;

const int max_len=20;

class queue
{
public:
 void reset()
 {
  top=bottom=max_len/2;
  top--;
 };
 void push_t(char);
 void pop_t();
 void push_b(char);
 void pop_b();
 void print_stack();
    void top_of();
 void bottom_of();
    int  empty();
 int  full();
private:
 char s[max_len];
    int  bottom,top;
};

int queue::empty()
{
 if(top<bottom)
         return 1;
 
 return 0;
}

int queue::full()
{
 if(top==max_len&&bottom==0)
     return 1;
 
   return 0;
 
}

void queue::bottom_of()
{  
 if(!empty())
       cout<<s[bottom]<<endl;
   else
   cout<<"the queue is empty."<<endl;

}

void queue::top_of()
{  
 if(!empty())
      cout<<s[top]<<endl;
    else
   cout<<"the queue is empty."<<endl;

}

void queue::pop_b()

 if(!empty())
       bottom++;
}

void queue::pop_t()

 if(!empty())
       top--;
}

void queue::push_b(char a )

 if(!full()&&bottom!=0)
 {  
  bottom--;
  s[bottom]=a;
 
 }
 else
  cout<<"sorry,the bottom's direction is full."<<endl;
}

void queue::push_t(char a)
{  
 if(!full()&&top!=max_len-1)
 {   
  top++;
  s[top]=a;
 }
 else
  cout<<"sorry,the top's direction is full."<<endl;
}

void queue::print_stack()
{
 int i;
 if(!empty())
 {
  for(i=bottom;i<=top;i++)
    cout<<s[i]<<setw(2);
    
  cout<<endl;
 }
 else
  cout<<"the queue is empty,nothing is here."<<endl;
}

 

int main()
{    class queue test;
     int type;
    char a;
  
   test.reset();
  
   cout<<"please choose a command."<<endl;
   cout<<"******************************"<<endl;
   cout<<"1:"<<setw(3)<<"   bottom of"<<endl;
   cout<<"2:"<<setw(3)<<"   top of"<<endl;
   cout<<"3:"<<setw(3)<<"   pop top"<<endl;
   cout<<"4:"<<setw(3)<<"   push top"<<endl;
   cout<<"5:"<<setw(3)<<"   pop bottom"<<endl;
   cout<<"6:"<<setw(3)<<"   push bottom"<<endl;
   cout<<"7:"<<setw(3)<<"   empty or not"<<endl;
   cout<<"8:"<<setw(3)<<"   full or not"<<endl;
   cout<<"9:"<<setw(3)<<"   print queue"<<endl;
   cout<<"10:"<<setw(3)<<"  reset"<<endl;
   cout<<"0:"<<setw(3)<<"   exit"<<endl;
   cout<<"******************************"<<endl;
  
   cin>>type;
  
   while(type!=0)
   {
    switch (type)
  
    {
  
    case 1:   test.bottom_of();
   
     break;
  
    case 2:   test.top_of();  
   
     break;
  
    case 3:   test.pop_t();  
   
     break;
  
    case 4:   cout<<"input an element:(the type is char)"<<endl;
     cin>>a;
          
     test.push_t(a);
   
     break;
   
  
    case 5:   test.pop_b(); 
   
     break;
  
    case 6:   cout<<"input an element:(the type is char)"<<endl;
              cin>>a;
           
     test.push_b(a);
   
     break;
   
  
    case 7:   test.empty();
     
     if(test.empty()==1)
  
     {
    cout<<"empty"<<endl;
   
     }
  
     else
    cout<<"not empty"<<endl;
    
     break;
  
    case 8:   test.full();
   
     if(test.full()==1)
    
     {
    
      cout<<"full"<<endl;
    
     }
   
     else
    
      cout<<"not full"<<endl;
   
     break;
  
    case 9:  
   
     test.print_stack();
   
     break;
  
    case 10:  test.reset(); 
   
     break;
  
    default: 
   
     return 1;
  
    }
  
  
    cin>>type;
  
   }
 
   return 0;

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.