C ++ study Note 1

Source: Internet
Author: 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 shocould output the stack from bottom to top. An empty stack is denoted by having the top fall below the bottom. test each function.

 

// This program is written in VC-SP6, debugging through

# 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)
{
If (! Full () & bottom! = 0)
{
Bottom --;
S [bottom] =;

}
Else
Cout <"sorry, the bottom's direction is full." <Endl;
}

Void queue: push_t (char)
{
If (! Full () & Top! = Max_len-1)
{
Top ++;
S [Top] =;
}
Else
Cout <"sorry, the top's ction 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;

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>;

Test. push_t ();

Break;


Case 5: Test. pop_ B ();

Break;

Case 6: cout <"input an element :( the type is Char)" <Endl;
Cin>;

Test. push_ B ();

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.