キューデータ構造を実装するJavaプログラム
この記事では、キューのデータ構造を実装する方法を理解します。キューは、操作が実行される特定の順序に従う線形構造です。注文は先入れ先出し(FIFO)です。
以下は同じのデモンストレーションです-
入力がであると仮定します −
Input Queue: [150, 300, 450, 600]
必要な出力は −
After removing an element, the elements of the queue are: [300, 450, 600]
アルゴリズム
Step 1 - START Step 2 - Declare namely Step 3 - Add elements to it using the ‘offer’ method. Step 4 - Display the queue content Step 5 - Use the ‘poll’ method to delete the element from the queue. Step 6 - Display the elements of the queue after calling the ‘poll’ method. Step 7 - Display the result Step 8 - Stop
例1
ここでは、組み込みの定義済み関数を使用して、すべてのスタック操作を実行します。
import java.util.Queue;
import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
System.out.println("The required packages have been imported");
Queue<Integer> input_queue = new LinkedList<>();
input_queue.offer(150);
input_queue.offer(300);
input_queue.offer(450);
input_queue.offer(600);
System.out.println("The queue is defined as: " + input_queue);
int removedNumber = input_queue.poll();
System.out.println("After removing an element, the elements of the queue are: " +input_queue);
}
} 出力
The required packages have been imported The queue is defined as: [150, 300, 450, 600] After removing an element, the elements of the queue are: [300, 450, 600]
例2
ここでは、ユーザー定義関数を使用してすべてのスタック操作を実行します。
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
void enQueue(int element) {
if (isFull()) {
System.out.println("\nThe queue is full");
}
else {
if (front == -1) {
front = 0;
}
rear++;
items[rear] = element;
System.out.println("\nThe element " + element + " is inserted");
}
}
int deQueue() {
int element;
if (isEmpty()) {
System.out.println("\nThe queue is empty");
return (-1);
}
else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
}
else {
front++;
}
System.out.println("\nThe element " +element + " is deleted");
return (element);
}
}
void display() {
int i;
if (isEmpty()) {
System.out.println("The queue is empty ");
}
else {
System.out.println("\nThe elements of the queue are: ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
}
}
public static void main(String[] args) {
Queue input_queue = new Queue();
for(int i = 1; i < 6; i ++) {
input_queue.enQueue(i * 100);
}
System.out.println("The queue is defined as: " + input_queue);
input_queue.enQueue(6);
input_queue.display();
input_queue.deQueue();
input_queue.display();
}
} 出力
The element 100 is inserted The element 200 is inserted The element 300 is inserted The element 400 is inserted The element 500 is inserted The queue is defined as: Queue@2a139a55 The queue is full The elements of the queue are: 100 200 300 400 500 The element 100 is deleted The elements of the queue are: 200 300 400 500
-
長方形の周囲を見つけるJavaプログラム
この記事では、長方形の周囲を見つける方法を理解します。長方形の周囲長は、長方形のすべての辺の長さを加算して計算されます。 以下は長方形のデモンストレーションです。長方形の周囲は、長方形の2つの長さと2つの幅の全長です- 入力 入力が-であると仮定します The length of the sides of a rectangle are : 5, 8, 5, 8 出力 必要な出力は-になります Perimeter : 26 アルゴリズム Step 1 – START Step 2 – Declare 5 floating point variabl
-
Javaで数を数えるプログラムを実装するにはどうすればよいですか?
プログラムはJLabelを使用します カウントラベルを保持するには、 JTextField 数値を保持するコンポーネントカウント 、 JButton 追加を作成するコンポーネント 、削除 およびリセット ボタン。追加ボタンをクリックすると、JTextFieldのカウントがインクリメントされます 投稿者 1 削除ボタンをクリックすると、カウントが「1」ずつ減らされます。 [リセット]ボタンをクリックすると、リセットされます 0へのカウント 。 例 import java.awt.*; import java.awt.event.*; import javax.swing.*; publ