配列を使用してキューを実装するC++プログラム
キューは、要素のコレクションを含む抽象的なデータ構造です。キューはFIFOメカニズムを実装します。つまり、最初に挿入された要素も最初に削除されます。つまり、最近追加された要素がキューの最初に削除されます。
配列を使用してキューを実装するプログラムは次のとおりです-
例
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
} 上記のプログラムの出力は次のとおりです
1) Insert element to queue 2) Delete element from queue 3) Display all the elements of queue 4) Exit Enter your choice : 1 Insert the element in queue : 4 Enter your choice : 1 Insert the element in queue : 3 Enter your choice : 1 Insert the element in queue : 5 Enter your choice : 2 Element deleted from queue is : 4 Enter your choice : 3 Queue elements are : 3 5 Enter your choice : 7 Invalid choice Enter your choice : 4 Exit
上記のプログラムでは、関数Insert()が要素をキューに挿入します。リアがn-1に等しい場合、キューはいっぱいで、オーバーフローが表示されます。フロントが-1の場合、1ずつ増加します。その後、リアが1ずつ増加し、要素がリアのインデックスに挿入されます。これを以下に示します-
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
} Delete()関数では、キューに要素がない場合はアンダーフロー状態です。それ以外の場合は、frontの要素が表示され、frontが1つインクリメントされます。これを以下に示します-
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
}
else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
} 関数display()で、frontが-1の場合、キューは空です。それ以外の場合、すべてのキュー要素はforループを使用して表示されます。これを以下に示します-
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
} 関数main()は、ユーザーがキューを挿入、削除、または表示するかどうかを選択できるようにします。ユーザーの応答に応じて、スイッチを使用して適切な関数が呼び出されます。ユーザーが無効な応答を入力すると、それが出力されます。このためのコードスニペットを以下に示します-
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
} -
配列を使用してスタックを実装するC++プログラム
スタックは、要素のコレクションを含む抽象的なデータ構造です。スタックはLIFOメカニズムを実装します。つまり、最後にプッシュされた要素が最初にポップアウトされます。スタック内の主要な操作のいくつかは-です。 プッシュ-これにより、データ値がスタックの最上位に追加されます。 ポップ-これにより、スタックの最上位のデータ値が削除されます ピーク-これはスタックの最上位のデータ値を返します 配列を使用してスタックを実装するプログラムは次のとおりです。 例 #include <iostream> using namespace std; int stack[100]
-
ソートされた配列を実装するC++プログラム
並べ替えられた配列は、各要素が数値、アルファベット順などの順序で並べ替えられた配列です。バブルの並べ替え、挿入の並べ替え、選択の並べ替え、マージの並べ替え、クイック並べ替えなど、数値の配列を並べ替えるアルゴリズムは多数あります。ヒープソートなど。選択ソートを使用した配列のソートの詳細については、以下を参照してください。 選択ソートは、ソートされた配列を生成するソート方法です。これは、配列内の最小の要素を繰り返し見つけて、ソートされていない部分の先頭にある要素と交換することによって行われます。 選択ソートを使用してソートされた配列を実装するプログラムは次のとおりです。 例 #include&