用两个栈创建队列
1.第一種方法:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include<stack> 4 using namespace std; 5 6 //Pop , push, front, back, empty; 7 template<class T> 8 class MyQueue 9 { 10 public: 11 //判斷隊列是否為空。 empty 12 bool empty() 13 { 14 if (head.empty() && reak.empty()) 15 return true; 16 return false; 17 } 18 19 //隊列添加元素 push 20 void push(T val) 21 { 22 head.push(val); 23 } 24 25 //刪除隊列的元素: 隊列的性質(zhì):先進先出 pop 26 void Pop() 27 { 28 if (this->empty()) //判斷隊列知否為空 29 throw exception("MyQueue empty"); 30 if (!head.empty()) //隊列不為空, 31 { 32 if (reak.empty()) //中轉(zhuǎn)棧為空 33 { 34 while (!head.empty()) //將數(shù)據(jù)拷進中轉(zhuǎn)棧中,這樣,就可以使第一個元素暴漏出來 35 { 36 reak.push(head.top()); 37 head.pop(); 38 } 39 } 40 } 41 reak.pop(); //刪除中轉(zhuǎn)棧的棧頂元素,即隊列的第一個元素 42 } 43 44 45 //返回隊列的隊頭元素 46 T& Front() 47 { 48 if (this->empty()) //判斷隊列知否為空 49 throw exception("MyQueue empty"); 50 if (!head.empty()) 51 { 52 if (reak.empty()) 53 { 54 while (!head.empty()) 55 { 56 reak.push(head.top()); 57 head.pop(); 58 } 59 } 60 } 61 return reak.top(); 62 } 63 64 65 //返回隊列的隊尾元素 66 T& Back() 67 { 68 if (this->empty()) //判斷隊列知否為空 69 throw exception("MyQueue empty"); 70 if (!reak.empty()) 71 { 72 if (head.empty()) 73 { 74 while (!reak.empty()) 75 { 76 head.push(reak.top()); 77 reak.pop(); 78 } 79 } 80 } 81 82 return head.top(); 83 } 84 85 private: 86 stack<T> head; //隊列的尾 87 stack<T> reak; //隊列的頭 88 }; 89 90 //測試函數(shù) 91 void test01() 92 { 93 MyQueue<int> que; 94 //給隊列添加元素 95 for (int i = 0; i < 10; i++) 96 { 97 que.push(i + 10); 98 } 99 //返回隊頭元素 100 cout<<que.Front()<<endl; 101 //返回隊尾元素 102 cout << que.Back() << endl; 103 104 //刪除隊頭元素 105 que.Pop(); 106 que.Pop(); 107 108 //返回隊頭元素 109 cout << que.Front() << endl; 110 //返回隊尾元素 111 cout << que.Back() << endl; 112 //判斷隊列是否為空 113 if (que.empty()) 114 { 115 cout << "隊列為空" << endl; 116 } 117 else 118 { 119 cout << "隊列不為空" << endl; 120 } 121 122 } 123 124 125 int main() 126 { 127 try 128 { 129 test01(); 130 } 131 catch (...){ 132 cout << "拋出其他異常" << endl; 133 } 134 135 system("pause"); 136 return EXIT_SUCCESS; 137 }2.第二種方法:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include <stack> 4 using namespace std; 5 6 class Myqueue 7 { 8 public: 9 //判斷隊列是否為空。 empty 10 bool empty() 11 { 12 if (s1.empty() && s2.empty()) 13 return true; 14 return false; 15 } 16 17 //隊列添加元素 push 18 void push(int val) 19 { 20 s1.push(val); 21 } 22 23 //刪除隊列的元素: 隊列的性質(zhì):先進先出 pop 24 void Pop() 25 { 26 if (this->empty()) 27 { 28 cout << "隊列為空Error!!!" << endl; 29 return; 30 } 31 //先把元素拷進中轉(zhuǎn)棧 32 if (!s1.empty()) 33 { 34 if (s2.empty()) 35 { 36 while (!s1.empty()) 37 { 38 s2.push(s1.top()); 39 s1.pop(); 40 } 41 } 42 } 43 //刪除完元素后再拷進原來的棧 44 if (!s2.empty()) 45 { 46 s2.pop(); 47 while (!s2.empty()) 48 { 49 s1.push(s2.top()); 50 s2.pop(); 51 } 52 } 53 54 } 55 //返回隊列的隊頭元素 56 int& Front() 57 { 58 int* val = new int; 59 if (this->empty()) 60 { 61 throw exception("MyQueue empty"); 62 } 63 //先把元素拷進中轉(zhuǎn)棧 64 if (!s1.empty()) 65 { 66 if (s2.empty()) 67 { 68 while (!s1.empty()) 69 { 70 s2.push(s1.top()); 71 s1.pop(); 72 } 73 } 74 } 75 if (!s2.empty()) 76 { 77 *val = s2.top(); 78 while (!s2.empty()) 79 { 80 s1.push(s2.top()); 81 s2.pop(); 82 } 83 } 84 return *val; 85 } 86 87 //返回隊列的隊尾元素 88 int& Back() 89 { 90 91 if (this->empty()) 92 { 93 throw exception("MyQueue empty"); 94 95 } 96 return s1.top(); 97 } 98 private: 99 stack<int> s1; //隊列的尾 100 stack<int> s2; //隊列的頭 101 }; 102 103 104 void test02() 105 { 106 Myqueue que; 107 //給隊列添加元素 108 for (int i = 0; i < 10; i++) 109 { 110 que.push(i + 20); 111 } 112 //返回隊頭元素 113 cout << que.Front() << endl; 114 //返回隊尾元素 115 cout << que.Back() << endl; 116 117 //刪除隊頭元素 118 que.Pop(); 119 que.Pop(); 120 121 //返回隊頭元素 122 cout << que.Front() << endl; 123 //返回隊尾元素 124 cout << que.Back() << endl; 125 //判斷隊列是否為空 126 if (que.empty()) 127 { 128 cout << "隊列為空" << endl; 129 } 130 else 131 { 132 cout << "隊列不為空" << endl; 133 } 134 } 135 136 137 int main02() 138 { 139 140 test02(); 141 142 system("pause"); 143 return EXIT_SUCCESS; 144 }總結(jié): 這兩種方法的不同之處在于:
1.第一種方法:只向中轉(zhuǎn)棧容器拷貝一次,直至這個棧容器空。(即自建隊列元素進入的那個棧容器);
2.第二種方法:先向中轉(zhuǎn)棧容器中拷貝,每次操作完后再將中轉(zhuǎn)容器中剩余的元素拷貝回原來的棧容器(即自建隊列元素進入的那個棧容器);
困惑之處:
1.當時看到這道題的困惑之處在于,如何把兩個棧容器綁定在一起,(當時陷入死循環(huán),忘了創(chuàng)建類這中方法)。
2.當創(chuàng)建一個類時,用它定義的對象就可以作為自定義的隊列。(也可以很好的把兩個棧容器集成在一塊)。
轉(zhuǎn)載于:https://www.cnblogs.com/yyx1-1/p/5778005.html
總結(jié)
- 上一篇: 1-1-2 交叉编译工具链
- 下一篇: php访问mysql工具类