デキューを実装するC++プログラム
デキューまたは両端キューは、キューデータ構造の一般化されたバージョンであり、両端で挿入と削除が可能です。
デキューの基本的な操作には、次のようなものがあります-
insert_at_beg() : デキューの先頭にアイテムを挿入します。
insert_at_end() : デキューの後ろにアイテムを挿入します。
delete_fr_beg() : デキューの前からアイテムを削除します。
delete_fr_rear() : デキューの後ろからアイテムを削除します。
以下は、デキューを実装するためのC++プログラムです
アルゴリズム
Begin Declare a class dequeue to declare front f and rear r and following functions: function insert_at_beg(int) to insert item at front: If queue is not completely filled up, insert element at the front and update front and rear Otherwise print overflow. function insert_at_end(int) to insert item at rear: If queue is not completely filled up, insert element at the rear and update front and rear Otherwise print overflow. function delete_fr_beg() to delete item from front: If queue is empty, print underflow otherwise delete the front element and update front. function delete_fr_end() to delete item from end: If queue is empty, print underflow otherwise delete the rear element and update rear. End
サンプルコード
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1) {
cout<<"\n insertion is not possible, overflow!!!!";
} else {
if(f==-1) {
f++;
r++;
} else {
r=r+1;
}
a[r]=i;
cout<<"\nInserted item is"<<a[r];
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i;
cout<<"\n inserted element is:"<<i;
} else if(f!=0) {
a[--f]=i;
cout<<"\n inserted element is:"<<i;
} else {
cout<<"\n insertion is not possible, overflow!!!";
}
}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[f];
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[r];
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty";
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
}
}
int main() {
int c,i;
dequeue d;
Do//perform switch opeartion {
cout<<"\n 1.insert at beginning";
cout<<"\n 2.insert at end";
cout<<"\n 3.show";
cout<<"\n 4.deletion from front";
cout<<"\n 5.deletion from rear";
cout<<"\n 6.exit";
cout<<"\n enter your choice:";
cin>>c;
switch(c) {
case 1:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_beg(i);
break;
case 2:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_end(i);
break;
case 3:
d.show();
break;
case 4:
d.delete_fr_front();
break;
case 5:
d.delete_fr_rear();
break;
case 6:
exit(1);
break;
default:
cout<<"invalid choice";
break;
}
} while(c!=7);
} 出力
1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:4 deletion is not possible::dequeue is empty 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:5 deletion is not possible::dequeue is empty 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:1 enter the element to be inserted7 inserted element is:7 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:1 enter the element to be inserted6 insertion is not possible, overflow!!! 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:1 enter the element to be inserted4 insertion is not possible, overflow!!! 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:2 enter the element to be inserted6 Inserted item is6 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:2 enter the element to be inserted4 Inserted item is4 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:3 7 6 4 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:4 the deleted element is:7 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:5 the deleted element is:4 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:1 enter the element to be inserted7 inserted element is:7 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:3 7 6 1.insert at beginning 2.insert at end 3.show 4.deletion from front 5.deletion from rear 6.exit enter your choice:6
-
隣接行列を実装するためのC++プログラム
グラフの隣接行列は、サイズV x Vの正方行列です。VはグラフGの頂点の数です。この行列では、各辺にV個の頂点がマークされています。グラフにiからjの頂点までのエッジがある場合、i thの隣接行列に 行とjth 列は1(または加重グラフの場合はゼロ以外の値)になります。それ以外の場合、その場所は0を保持します。 隣接行列表現の複雑さ: 隣接行列表現は、計算中にO(V2)のスペースを取ります。グラフに最大数のエッジと最小数のエッジがある場合、どちらの場合も必要なスペースは同じになります。 入力: 出力: 0 1 2 3 4
-
隣接リストを実装するC++プログラム
グラフの隣接リスト表現は、リンクリスト表現です。この表現では、リストの配列があります。配列のサイズはVです。ここで、Vは頂点の数です。つまり、V個の異なるリストを格納する配列があると言えます。リストヘッダーが頂点uの場合、uの隣接するすべての頂点を保持することを意味します。 隣接リスト表現の複雑さ この表現は、無向グラフの場合はO(V + 2E)を取り、有向グラフの場合はO(V + E)を取ります。エッジの数を増やすと、必要なスペースも増えます。 入力 : 出力 : アルゴリズム add_edge(adj_list、u、v) 入力 :エッジ{u、v}のuとv、およ