STLにリストを実装するC++プログラム
リストは、連続しないメモリ割り当てを可能にするシーケンスコンテナです。リストはベクトルに比べてトラバースが遅くなりますが、位置が見つかると、挿入と削除がすばやく行われます。
機能と説明:
From main(), we have called following functions: fl.resize() = Returns the resize of list. fl.push_front() = It is used to push elements into a list from the front. fl.remove() = Deletes elements from list. fl.unique() = Deletes duplicate elements from list. fl.reverse() = Reverses the list. fl.front() = Returns the front element of list.
サンプルコード
#include<iostream> #include <list> #include <string> #include <cstdlib> using namespace std; int main() { list<int> l; list<int>::iterator it; int c, i; while (1) { cout<<"1.Insert Element at the Front"<<endl; cout<<"2.Insert Element at the End"<<endl; cout<<"3.Delete Element at the Front"<<endl; cout<<"4.Delete Element at the End"<<endl; cout<<"5.Front Element of List"<<endl; cout<<"6.Last Element of the List"<<endl; cout<<"7.Size of the List"<<endl; cout<<"8.Resize List"<<endl; cout<<"9.Remove Elements with Specific Values"<<endl; cout<<"10.Remove Duplicate Values"<<endl; cout<<"11.Reverse the order of elements"<<endl; cout<<"12.Display the List"<<endl; cout<<"13.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; switch(c) { case 1: cout<<"Enter value to be inserted at the front: "; cin>>i; l.push_front(i); break; case 2: cout<<"Enter value to be inserted at the end: "; cin>>i; l.push_back(i); break; case 3: i= l.front(); l.pop_front(); cout<<"Element "<<i<<" deleted"<<endl; break; case 4: i= l.back(); l.pop_back(); cout<<"Element "<<i<<" deleted"<<endl; break; case 5: cout<<"Front Element of the List: "; cout<<l.front()<<endl; break; case 6: cout<<"Last Element of the List: "; cout<<l.back()<<endl; break; case 7: cout<<"Size of the List: "<<l.size()<<endl; break; case 8: cout<<"Enter new size of the List: "; cin>>i; if (i <= l.size()) l.resize(i); else l.resize(i, 0); break; case 9: cout<<"Enter element to be deleted: "; cin>>i; l.remove(i); break; case 10: l.unique(); cout<<"Duplicate Items Deleted"<<endl; break; case 11: l.reverse(); cout<<"List reversed"<<endl; break; case 12: cout<<"Elements of the List: "; for (it = l.begin(); it != l.end(); it++) cout<<*it<<" "; cout<<endl; break; case 13: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
出力
1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 2 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 3 Element 2 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 4 Element 1 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 2 Enter value to be inserted at the end: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 6 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 7 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 10 Duplicate Items Deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 5 Front Element of the List: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 11 List reversed 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 5 6 7 5 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 13
-
STLにSet_Differenceを実装するC++プログラム
2つのセットの違いは、2番目のセットではなく、最初のセットに存在する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_difference (set1, set1 + n,
-
隣接リストを実装するC++プログラム
グラフの隣接リスト表現は、リンクリスト表現です。この表現では、リストの配列があります。配列のサイズはVです。ここで、Vは頂点の数です。つまり、V個の異なるリストを格納する配列があると言えます。リストヘッダーが頂点uの場合、uの隣接するすべての頂点を保持することを意味します。 隣接リスト表現の複雑さ この表現は、無向グラフの場合はO(V + 2E)を取り、有向グラフの場合はO(V + E)を取ります。エッジの数を増やすと、必要なスペースも増えます。 入力 : 出力 : アルゴリズム add_edge(adj_list、u、v) 入力 :エッジ{u、v}のuとv、およ