Програма Java за внедряване на структурата от данни на опашката

В този пример ще се научим да прилагаме структурата на данните за опашката в Java.

За да разберете този пример, трябва да имате познанията по следните теми за програмиране на Java:

  • Интерфейс на Java Queue
  • Java Generics

Пример 1: Java програма за внедряване на Stack

 public class Queue ( int SIZE = 5; int items() = new int(SIZE); int front, rear; Queue() ( front = -1; rear = -1; ) // check if the queue is full boolean isFull() ( if (front == 0 && rear == SIZE - 1) ( return true; ) return false; ) // check if the queue is empty boolean isEmpty() ( if (front == -1) return true; else return false; ) // insert elements to the queue void enQueue(int element) ( // if queue is full if (isFull()) ( System.out.println("Queue is full"); ) else ( if (front == -1) ( // mark front denote first element of queue front = 0; ) rear++; // insert element at the rear items(rear) = element; System.out.println("Insert " + element); ) ) // delete element from the queue int deQueue() ( int element; // if queue is empty if (isEmpty()) ( System.out.println("Queue is empty"); return (-1); ) else ( // remove element from the front of queue element = items(front); // if the queue has only one element if (front>= rear) ( front = -1; rear = -1; ) else ( // mark next element as the front front++; ) System.out.println( element + " Deleted"); return (element); ) ) // display element of the queue void display() ( int i; if (isEmpty()) ( System.out.println("Empty Queue"); ) else ( // display the front of the queue System.out.println("Front index-> " + front); // display element of the queue System.out.println("Items -> "); for (i = front; i " + rear); ) ) public static void main(String() args) ( // create an object of Queue class Queue q = new Queue(); // try to delete element from the queue // currently queue is empty // so deletion is not possible q.deQueue(); // insert elements to the queue for(int i = 1; i < 6; i ++) ( q.enQueue(i); ) // 6th element can't be added to queue because queue is full q.enQueue(6); q.display(); // deQueue removes element entered first i.e. 1 q.deQueue(); // Now we have just 4 elements q.display(); ) )

Изход

 Опашката е празна. -> 4

В горния пример ние внедрихме структурата на данните за опашката в Java.

За да научите работата по опашката, посетете Структура на данните за опашката.

Пример 2: Внедрете стека, като използвате интерфейса на опашката

Java предоставя вграден Queueинтерфейс, който може да се използва за внедряване на опашка.

 import java.util.Queue; import java.util.LinkedList; class Main ( public static void main(String() args) ( // Creating Queue using the LinkedList class Queue numbers = new LinkedList(); // enqueue // insert element at the rear of the queue numbers.offer(1); numbers.offer(2); numbers.offer(3); System.out.println("Queue: " + numbers); // dequeue // delete element from the front of the queue int removedNumber = numbers.poll(); System.out.println("Removed Element: " + removedNumber); System.out.println("Queue after deletion: " + numbers); ) )

Изход

 Опашка: (1, 2, 3) Премахнат елемент: 1 Опашка след изтриване: (2, 3)

В горния пример използвахме Queueинтерфейса за реализиране на опашката в Java. Тук използвахме LinkedListкласа, който реализира Queueинтерфейса.

  • numbers.offer () - вмъкване на елементи в задната част на опашката
  • numbers.poll () - премахване на елемент от предната страна на опашката

Забележете, използвахме ъгловите скоби, докато създавахме опашката. Представлява, че опашката е от родов тип.

Можем да използваме и други интерфейси и класове вместо Queueи LinkedList. Например,

  • Deque интерфейс
  • Клас ArrayDeque
  • Клас на приоритетната опашка

Интересни статии...