STLにスタックを実装するC++プログラム
スタックは、操作が実行される特定の順序に従う線形データ構造です。順序は、FILO(先入れ先出し)またはLIFO(後入れ先出し)の場合があります
アルゴリズム
Begin Declare stack vector. Take the input as per choice. Call the functions within switch operation: s.size() = Returns the size of stack. s.push() = It is used to insert elements to the stack. s.pop() = To pop out the value from the stack. s.top() = Returns a reference to the top most element of stack. End.
サンプルコード
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
stack<int> s;
int c, i;
while (1) {
cout<<"1.Size of the Stack"<<endl;
cout<<"2.Insert Element into the Stack"<<endl;
cout<<"3.Delete Element from the Stack"<<endl;
cout<<"4.Top Element of the Stack"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Size of the stack: ";
cout<<s.size()<<endl;
break;
case 2:
cout<<"Enter value to be inserted: ";
cin>>i;
s.push(i);
break;
case 3:
i = s.top();
if (!s.empty()) {
s.pop();
cout<<i<<" Deleted"<<endl;
}else {
cout<<"Stack is Empty"<<endl;
}
break;
case 4:
cout<<"Top Element of the Stack: ";
cout<<s.top()<<endl;
break;
case 5:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} 出力
1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Size of the stack: 0 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 1 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 10 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Size of the stack: 5 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 3 4 Deleted 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 4 Top Element of the Stack: 10 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 5 Exit code: 1
-
STLにSet_Intersectionを実装するC++プログラム
2つのセットの共通部分は、両方のセットに共通する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_intersection (set1, set1 + n, set2, s
-
STLにSet_Differenceを実装するC++プログラム
2つのセットの違いは、2番目のセットではなく、最初のセットに存在する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_difference (set1, set1 + n,