Data Structure 4: Stack Queue and

Always remember stack features: LIFO

Queue: FIFO

Write an example of a stack

 1 //8进制转换
 2 #include<iostream>
 3 #include<stack>
 4 using namespace std;
 5 void conversion(int n)
 6 {
 7     stack<int> result;
 8     while(n)
 9     {
10         result.push(n%8);//入栈
11         n=n/8;
12     }
13     while(!result.empty())
14     {
15         cout<<result.top();
16         result.pop();//出栈
17     }
18 }
19 int main()
20 {
21     conversion(1348);
22     return 0;
23 }

I wrote a matching brackets

1  // matching parenthesis 
2 #include <the iostream>
 . 3 #include <Stack>
 . 4  the using  namespace STD;
 . 5  BOOL Side ( char CH) // judgment is right parenthesis or left parenthesis, true is left parenthesis, false is a right bracket 
6  {
 . 7      IF (CH == ' ( ' || CH == ' [ ' )
 . 8          return  to true ;
 . 9      the else 
10      {
 . 11          return  to false ;
 12 is      }
 13 is      
14  }
 15 BOOL MAT ( char right, char left) // determines whether the two characters of the same type as the left and right brackets 
16  {
 . 17      IF (right == ' ( ' && left == ' ) ' )
 18 is          return  to true ;
 . 19      IF (right == ' [ ' && left == ' ] ' )
 20 is          return  to true ;
 21 is      return  to false ;
 22 is  }
 23 is  BOOL match ( String STR)
 24 {
 25      Stack < char > Re;
 26 is      Auto W = str.begin ();
 27      for (;! Str.end W = (); W ++ )
 28      {
 29          IF (Side (W *)) // left bracket 
30              re.push (* W);
 31 is          the else 
32          {
 33 is              IF (MAT (re.top (), * W)) // the right and left bracket bracket can be matched to the stack 
34 is                  {
 35                      re.pop ();
 36  
37                  }
 38          }
 39          
40      }
41     if(re.empty())
42         return true;
43     else
44     {
45         return false;
46     }
47     
48 }
49 
50 int main()
51 {
52     // cout<<match("[([][])]");
53     cout<<match("[(])");
54     return 0;
55 }

 

Guess you like

Origin www.cnblogs.com/neverland0718/p/11413951.html