The Wayback Machine - https://web.archive.org/web/20241001015920/https://www.geeksforgeeks.org/array-implementation-of-queue-simple/
Open In App

Array implementation of queue (Simple)

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Please note that a simple array implementation discussed here is not used in practice as it is not efficient. In practice, we either use Linked List Implementation of Queue or circular array implementation of queue. The idea of this post is to give you a background as to why we need a circular array implementation.

To implement a queue using a simple array, 

  • create an array arr of size n and 
  • take two variables front and rear which are initialized as 0 and -1 respectively
  • rear is the index up to which the elements are stored in the array including the rear index itself.  We mainly add an item by incrementing it.
  • front is the index of the first element of the array.  We mainly remove the element at this index in Dequeue operation.
  • Enqueue: Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. If rear == capacity-1, then it is said to be an Overflow condition as the array is full. If array is not full. then store the element at arr[rear] and increment rear by 1 .
  • Dequeue: Removal of an element from the queue. An element can only be deleted when there is at least an element to delete. If rear == -1, then it is said to be an Underflow condition as the array is empty. If the element at arr[front] can be deleted then move all the remaining elements to the left by one position in order to maintain the FIFO (the first element must be deleted).
  • Front: Get the front element from the queue i.e. arr[front] if the queue is not empty.
  • Display: Print all elements of the queue. If the queue is non-empty, traverse and print all the elements from the index front to rear.

Below is the implementation of a queue using an array: 

C++
#include <bits/stdc++.h>
using namespace std;

struct Queue {
    int front, rear, capacity;
    int* queue;
    
    // Constructor to initialize the queue
    Queue(int c)
    {
        front = 0;
        rear = -1;
        capacity = c;
        queue = new int[c];
    }

    // Destructor to free the allocated memory
    ~Queue() { delete[] queue; }

    // Function to insert an element at the rear of the queue
    void queueEnqueue(int data)
    {
        // Check if the queue is full
        if (rear == capacity - 1) {
            printf("\nQueue is full\n");
            return;
        }

        // Insert element at the rear
        queue[++rear] = data;
    }

    // Function to delete an element from the front 
    // of the queue
    void queueDequeue()
    {
        // If the queue is empty
        if (front > rear) {
            printf("\nQueue is empty\n");
            return;
        }

        // Shift all elements from index 1 till rear to 
        // the left by one
        for (int i = 0; i < rear; i++) {
            queue[i] = queue[i + 1];
        }

        // Decrement rear
        rear--;
    }

    // Function to print queue elements
    void queueDisplay()
    {
        if (front > rear) {
            printf("\nQueue is Empty\n");
            return;
        }

        // Traverse front to rear and print elements
        for (int i = front; i <= rear; i++) {
            printf(" %d <-- ", queue[i]);
        }
        printf("\n");
    }

    // Function to print the front of the queue
    void queueFront()
    {
        if (rear == -1) {
            printf("\nQueue is Empty\n");
            return;
        }
        printf("\nFront Element is: %d\n", queue[front]);
    }
};

// Driver code
int main(void)
{
    // Create a queue of capacity 4
    Queue q(4);

    // Print queue elements
    q.queueDisplay();

    // Insert elements in the queue
    q.queueEnqueue(20);
    q.queueEnqueue(30);
    q.queueEnqueue(40);
    q.queueEnqueue(50);

    // Print queue elements
    q.queueDisplay();

    // Insert element in the queue
    q.queueEnqueue(60);

    // Print queue elements
    q.queueDisplay();

    // Dequeue elements
    q.queueDequeue();
    q.queueDequeue();

    printf("\nAfter two node deletions\n");

    // Print queue elements
    q.queueDisplay();
    
    printf("\nAfter one insertion\n");
    q.queueEnqueue(60);
    
    // Print queue elements
    q.queueDisplay();

    // Print front of the queue
    q.queueFront();

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Queue {
    int front, rear, capacity;
    int* queue;
};

// Function to initialize the queue
struct Queue* createQueue(int capacity) {
    struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
    q->capacity = capacity;
    q->front = 0;
    q->rear = -1;
    q->queue = (int*)malloc(q->capacity * sizeof(int));
    return q;
}

// Function to insert an element at the rear of the queue
void enqueue(struct Queue* q, int data) {
    // Check if the queue is full
    if (q->rear == q->capacity - 1) {
        printf("Queue is full\n");
        return;
    }

    // Insert element at the rear
    q->queue[++q->rear] = data;
}

// Function to delete an element from the front of the queue
void dequeue(struct Queue* q) {
    // If the queue is empty
    if (q->front > q->rear) {
        printf("Queue is empty\n");
        return;
    }

    // Shift all elements from index 1 till rear to the left by one
    for (int i = 0; i < q->rear; i++) {
        q->queue[i] = q->queue[i + 1];
    }

    // Decrement rear
    q->rear--;
}

// Function to print queue elements
void display(struct Queue* q) {
    if (q->front > q->rear) {
        printf("Queue is Empty\n");
        return;
    }

    // Traverse front to rear and print elements
    for (int i = q->front; i <= q->rear; i++) {
        printf("%d <-- ", q->queue[i]);
    }
    printf("\n");
}

// Function to print the front of the queue
void front(struct Queue* q) {
    if (q->rear == -1) {
        printf("Queue is Empty\n");
        return;
    }
    printf("Front Element is: %d\n", q->queue[q->front]);
}

// Driver code
int main() {
    // Create a queue of capacity 4
    struct Queue* q = createQueue(4);

    // Print queue elements
    display(q);

    // Insert elements in the queue
    enqueue(q, 20);
    enqueue(q, 30);
    enqueue(q, 40);
    enqueue(q, 50);

    // Print queue elements
    display(q);

    // Insert element in the queue
    enqueue(q, 60);

    // Print queue elements
    display(q);

    // Dequeue elements
    dequeue(q);
    dequeue(q);

    printf("After two node deletions\n");

    // Print queue elements
    display(q);

    printf("After one insertion\n");
    enqueue(q, 60);

    // Print queue elements
    display(q);

    // Print front of the queue
    front(q);

    // Free the allocated memory
    free(q->queue);
    free(q);

    return 0;
}
Java
import java.util.*;

class Queue {
    int front, rear, capacity;
    int[] queue;

    // Constructor to initialize the queue
    Queue(int c) {
        front = 0;
        rear = -1;
        capacity = c;
        queue = new int[capacity];
    }

    // Function to insert an element at the rear of the queue
    void enqueue(int data) {
        // Check if the queue is full
        if (rear == capacity - 1) {
            System.out.println("Queue is full");
            return;
        }

        // Insert element at the rear
        queue[++rear] = data;
    }

    // Function to delete an element from the front of the queue
    void dequeue() {
        // If the queue is empty
        if (front > rear) {
            System.out.println("Queue is empty");
            return;
        }

        // Shift all elements from index 1 till rear to the left by one
        for (int i = 0; i < rear; i++) {
            queue[i] = queue[i + 1];
        }

        // Decrement rear
        rear--;
    }

    // Function to print queue elements
    void display() {
        if (front > rear) {
            System.out.println("Queue is Empty");
            return;
        }

        // Traverse front to rear and print elements
        for (int i = front; i <= rear; i++) {
            System.out.print(queue[i] + " <-- ");
        }
        System.out.println();
    }

    // Function to print the front of the queue
    void front() {
        if (rear == -1) {
            System.out.println("Queue is Empty");
            return;
        }
        System.out.println("Front Element is: " + queue[front]);
    }

    public static void main(String[] args) {
        // Create a queue of capacity 4
        Queue q = new Queue(4);

        // Print queue elements
        q.display();

        // Insert elements in the queue
        q.enqueue(20);
        q.enqueue(30);
        q.enqueue(40);
        q.enqueue(50);

        // Print queue elements
        q.display();

        // Insert element in the queue
        q.enqueue(60);

        // Print queue elements
        q.display();

        // Dequeue elements
        q.dequeue();
        q.dequeue();

        System.out.println("After two node deletions");

        // Print queue elements
        q.display();

        System.out.println("After one insertion");
        q.enqueue(60);

        // Print queue elements
        q.display();

        // Print front of the queue
        q.front();
    }
}
Python
class Queue:
    def __init__(self, capacity):
        self.front = 0
        self.rear = -1
        self.capacity = capacity
        self.queue = [None] * capacity

    # Function to insert an element at the rear of the queue
    def enqueue(self, data):
        # Check if the queue is full
        if self.rear == self.capacity - 1:
            print("Queue is full")
            return
        
        # Insert element at the rear
        self.rear += 1
        self.queue[self.rear] = data

    # Function to delete an element from the front of the queue
    def dequeue(self):
        # If the queue is empty
        if self.front > self.rear:
            print("Queue is empty")
            return
        
        # Shift all elements from index 1 till rear to the left by one
        for i in range(self.rear):
            self.queue[i] = self.queue[i + 1]

        # Decrement rear
        self.rear -= 1

    # Function to print queue elements
    def display(self):
        if self.front > self.rear:
            print("Queue is Empty")
            return

        # Traverse front to rear and print elements
        for i in range(self.front, self.rear + 1):
            print(self.queue[i], end=" <-- ")
        print()

    # Function to print the front of the queue
    def front_element(self):
        if self.rear == -1:
            print("Queue is Empty")
            return
        print("Front Element is:", self.queue[self.front])

# Driver code
if __name__ == "__main__":
    # Create a queue of capacity 4
    q = Queue(4)

    # Print queue elements
    q.display()

    # Insert elements in the queue
    q.enqueue(20)
    q.enqueue(30)
    q.enqueue(40)
    q.enqueue(50)

    # Print queue elements
    q.display()

    # Insert element in the queue
    q.enqueue(60)

    # Print queue elements
    q.display()

    # Dequeue elements
    q.dequeue()
    q.dequeue()

    print("After two node deletions")

    # Print queue elements
    q.display()

    print("After one insertion")
    q.enqueue(60)

    # Print queue elements
    q.display()

    # Print front of the queue
    q.front_element()
C#
using System;

class Queue {
    int front, rear, capacity;
    int[] queue;

    // Constructor to initialize the queue
    Queue(int c)
    {
        front = 0;
        rear = -1;
        capacity = c;
        queue = new int[capacity];
    }

    // Function to insert an element at the rear of the
    // queue
    void Enqueue(int data)
    {
        // Check if the queue is full
        if (rear == capacity - 1) {
            Console.WriteLine("Queue is full");
            return;
        }

        // Insert element at the rear
        queue[++rear] = data;
    }

    // Function to delete an element from the front of the
    // queue
    void Dequeue()
    {
        // If the queue is empty
        if (front > rear) {
            Console.WriteLine("Queue is empty");
            return;
        }

        // Shift all elements from index 1 till rear to the
        // left by one
        for (int i = 0; i < rear; i++) {
            queue[i] = queue[i + 1];
        }

        // Decrement rear
        rear--;
    }

    // Function to print queue elements
    void Display()
    {
        if (front > rear) {
            Console.WriteLine("Queue is Empty");
            return;
        }

        // Traverse front to rear and print elements
        for (int i = front; i <= rear; i++) {
            Console.Write(queue[i] + " <-- ");
        }
        Console.WriteLine();
    }

    // Function to print the front of the queue
    void Front()
    {
        if (rear == -1) {
            Console.WriteLine("Queue is Empty");
            return;
        }
        Console.WriteLine("Front Element is: "
                          + queue[front]);
    }

    public static void Main(string[] args)
    {
        // Create a queue of capacity 4
        Queue q = new Queue(4);

        // Print queue elements
        q.Display();

        // Insert elements in the queue
        q.Enqueue(20);
        q.Enqueue(30);
        q.Enqueue(40);
        q.Enqueue(50);

        // Print queue elements
        q.Display();

        // Insert element in the queue
        q.Enqueue(60);

        // Print queue elements
        q.Display();

        // Dequeue elements
        q.Dequeue();
        q.Dequeue();

        Console.WriteLine("After two node deletions");

        // Print queue elements
        q.Display();

        Console.WriteLine("After one insertion");
        q.Enqueue(60);

        // Print queue elements
        q.Display();

        // Print front of the queue
        q.Front();
    }
}
JavaScript
class Queue {
    constructor(capacity) {
        this.front = 0;
        this.rear = -1;
        this.capacity = capacity;
        this.queue = new Array(capacity);
    }

    // Function to insert an element at the rear of the queue
    enqueue(data) {
        // Check if the queue is full
        if (this.rear === this.capacity - 1) {
            console.log("Queue is full");
            return;
        }

        // Insert element at the rear
        this.queue[++this.rear] = data;
    }

    // Function to delete an element from the front of the queue
    dequeue() {
        // If the queue is empty
        if (this.front > this.rear) {
            console.log("Queue is empty");
            return;
        }

        // Shift all elements from index 1 till rear to the left by one
        for (let i = 0; i < this.rear; i++) {
            this.queue[i] = this.queue[i + 1];
        }

        // Decrement rear
        this.rear--;
    }

    // Function to print queue elements
    display() {
        if (this.front > this.rear) {
            console.log("Queue is Empty");
            return;
        }

        // Traverse front to rear and print elements
        let result = '';
        for (let i = this.front; i <= this.rear; i++) {
            result += this.queue[i] + " <-- ";
        }
        console.log(result);
    }

    // Function to print the front of the queue
    frontElement() {
        if (this.rear === -1) {
            console.log("Queue is Empty");
            return;
        }
        console.log("Front Element is: " + this.queue[this.front]);
    }
}

// Driver code
const q = new Queue(4);

// Print queue elements
q.display();

// Insert elements in the queue
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);

// Print queue elements
q.display();

// Insert element in the queue
q.enqueue(60);

// Print queue elements
q.display();

// Dequeue elements
q.dequeue();
q.dequeue();

console.log("After two node deletions");

// Print queue elements
q.display();

console.log("After one insertion");
q.enqueue(60);

// Print queue elements
q.display();

// Print front of the queue
q.frontElement();

Output
Queue is Empty
 20 <--  30 <--  40 <--  50 <-- 

Queue is full
 20 <--  30 <--  40 <--  50 <-- 

After two node deletions
 40 <--  50 <-- 

After one insertion
 40 <--  50 <--  60 <-- 

Front Element...

Time Complexity: O(1) for Enqueue (element insertion in the queue) as we simply increment pointer and put value in array and O(N) for Dequeue (element removing from the queue) as we use for loop to implement that.
Auxiliary Space: O(N), as here we are using an N size array for implementing Queue

We can notice that the Dequeue operation is O(N) which is not acceptable. The enqueue and dequeue both operations should have O(1) time complexity. That is why if we wish to implement a queue using array (because of array advantages like cache friendliness and random access), we do circular array implementation of queue.
 



Similar Reads

Can we use Simple Queue instead of Priority queue to implement Dijkstra's Algorithm?
What is Dijkstra's Algorithm? Dijkstra's Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail, about Dijkstra's Algorithm, you can refer to this article. Why Dijkstra's Algorithm uses a Priority Queue? We use min heap in Dijkstra's Algorithm beca
2 min read
Should we declare as Queue or Priority Queue while using Priority Queue in Java?
Queue: Queue is an Interface that extends the collection Interface in Java and this interface belongs to java.util package. A queue is a type of data structure that follows the FIFO (first-in-first-out ) order. The queue contains ordered elements where insertion and deletion of elements are done at different ends. Priority Queue and Linked List are
3 min read
Introduction and Array Implementation of Queue
Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. The order is First In First Out (FIFO). One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginning of the line. It is an ordered list in which in
13 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
Check if a queue can be sorted into another queue using a stack
Given a Queue consisting of first n natural numbers (in random order). The task is to check whether the given Queue elements can be arranged in increasing order in another Queue using a stack. The operation allowed are: Push and pop elements from the stack Pop (Or Dequeue) from the given Queue. Push (Or Enqueue) in the another Queue. Examples : Inp
9 min read
Reversing a Queue using another Queue
Given a queue. The task is to reverse the queue using another empty queue. Examples: Input: queue[] = {1, 2, 3, 4, 5} Output: 5 4 3 2 1 Input: queue[] = {10, 20, 30, 40} Output: 40 30 20 10 Approach: Given a queue and an empty queue.The last element of the queue should be the first element of the new queue.To get the last element there is a need to
5 min read
Advantages of circular queue over linear queue
Linear Queue: A Linear Queue is generally referred to as Queue. It is a linear data structure that follows the FIFO (First In First Out) order. A real-life example of a queue is any queue of customers waiting to buy a product from a shop where the customer that came first is served first. In Queue all deletions (dequeue) are made at the front and a
3 min read
Difference between Queue and Deque (Queue vs. Deque)
Queue: The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue. The operations allowed in the queue are:insert an element at the reardelete element from the frontget the last elementget the first elementcheck the size
3 min read
Why can't a Priority Queue wrap around like an ordinary Queue?
Priority Queue: A priority queue is a special type of queue in which each element is assigned a priority value. And elements are served based on their priority. This means that elements with higher priority are served first. However, if elements with the same priority occur, they will be served in the order in which they were queued. A priority que
3 min read
Turn a Queue into a Priority Queue
What is Queue?Queue is an abstract data type that is open at both ends. One end is always used to insert data (enqueue) which is basically the rear/back/tail end and the other which is the front end is used to remove data (dequeue). Queue follows First-In-First-Out (FIFO) methodology, i.e., "the data item stored first will be accessed first". Decla
9 min read
Why does Queue have front but Priority-queue has top in stl?
Why does the queue have front but the priority queue has top in stl? The main difference between a queue and a priority queue is that a queue follows the FIFO (First-In-First-Out) principle, while a priority queue follows a specific priority order. In other words, the elements in a queue are processed in the order they were added, whereas the eleme
8 min read
What is Circular Queue | Circular Queue meaning
A circular queue is an extended version of regular queue in which the last element of the queue is connected to the first element of the queue forming a cycle. Properties of Circular Queue: Along with the properties of a regular queue the circular queue has som other unique properties as mentioned below: Front and rear pointers: Two pointers, one a
4 min read
Difference Between Linear Queue and Circular Queue
Queues are fundamental data structures in computer science that follow the First-In-First-Out (FIFO) principle. Among the various types of queues, linear queues and circular queues are commonly used. While they share some similarities, they differ significantly in structure and operational efficiency. This article explores the concepts, advantages,
4 min read
Difference between Circular Queue and Priority Queue
Queues are fundamental data structures that are used to store and manage a collection of elements. While both circular queues and priority queues are types of queues, they have distinct characteristics and applications. This article will explore the key differences between circular queues and priority queues. Circular Queue:A Circular Queue is an e
4 min read
What is Priority Queue | Introduction to Priority Queue
A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved or removed before elements with lower priority values. Each element has a priority value associated with it. When we add an item, it is inserted in a position based on its priority value. There are
15+ min read
Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
Given an array of random numbers, find the longest monotonically increasing subsequence (LIS) in the array. If you want to understand the O(NlogN) approach, it's explained very clearly here. In this post, a simple and time-saving implementation of O(NlogN) approach using stl is discussed. Below is the code for LIS O(NlogN): Implementation: C/C++ Co
5 min read
Bellman Ford Algorithm (Simple Implementation)
We have introduced Bellman Ford and discussed on implementation here.Input: Graph and a source vertex src Output: Shortest distance to all vertices from src. If there is a negative weight cycle, then shortest distances are not calculated, negative weight cycle is reported.1) This step initializes distances from source to all vertices as infinite an
13 min read
QuickSelect (A Simple Iterative Implementation)
Quickselect is a selection algorithm to find the k-th smallest element in an unordered list. It is related to the quick sort sorting algorithm.Examples: Input: arr[] = {7, 10, 4, 3, 20, 15} k = 3 Output: 7 Input: arr[] = {7, 10, 4, 3, 20, 15} k = 4 Output: 10 The QuickSelect algorithm is based QuickSort. The difference is, instead of recurring for
7 min read
Largest BST Subtree - Simple Implementation
Given a Binary Tree, write a function that returns the size of the largest subtree which is also a Binary Search Tree (BST). If the complete Binary Tree is BST, then return the size of the whole tree. Examples:Input: 5 / \ 2 4 / \ 1 3 Output: 3 The following subtree is the maximum size BST subtree 2 / \ 1 3 Input: 50 / \ 30 60 / \ / \ 5 20 45 70 /
7 min read
Implementation of Non-Preemptive Shortest Job First using Priority Queue
Read here for Shortest Job First Scheduling algorithm for same arrival times.Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next.In this article, we will implement the Shortest Job First Scheduling algorithm (SJF) using a priority queue, so that we c
12 min read
Indexed Priority Queue with Implementation
Priority queue is a data structure in which data is stored on basis of its priority. In an Indexed Priority Queue, data is stored just like standard priority queue and along with this, the value of a data can be updated using its key. It is called "indexed" because a hash map can be used to store the index in container using the key of key-value pa
8 min read
Implementation of Queue in Javascript
In This article, we will be implementing Queue data structure in JavaScript. A Queue works on the FIFO(First in First Out) principle. Hence, it performs two basic operations which are the addition of elements at the end of the queue and the removal of elements from the front of the queue. Like Stack, Queue is also a linear data structure.  Note: As
6 min read
Circular Linked List Implementation of Circular Queue
Prerequisite – Circular Singly Linked List We have discussed basics and how to implement circular queue using array in set 1. Circular Queue | Set 1 (Introduction and Array Implementation) In this post another method of circular queue implementation is discussed, using Circular Singly Linked List. Operations on Circular Queue: Front:Get the front i
9 min read
Difference between PriorityQueue and Queue Implementation in Java
Java Queue InterfaceThe Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed from the beginning. Being an interface, the queue n
5 min read
Queue - Linked List Implementation
In this article, the Linked List implementation of the queue data structure is discussed and implemented. Print '-1' if the queue is empty. Approach: To solve the problem follow the below idea: we maintain two pointers, front, and rear. The front points to the first item of the queue and rear points to the last item. enQueue(): This operation adds
14 min read
Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)
We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], ....................... x % num[k-1] = rem[k-1] Example: Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1} Output: 11 Explanation: 11 is the sm
11 min read
Sort an array of 0s, 1s and 2s (Simple Counting)
Given an array A[] consisting of 0s, 1s, and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s, and all 2s in last. Examples: Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} Count the number of 0's, 1's, and 2's. After C
11 min read
Minimum Spanning Tree using Priority Queue and Array List
Given a bi-directed weighted (positive) graph without self-loops, the task is to generate the minimum spanning tree of the graph.Examples: Input: N = 9, E = 14, edges = {{0, 1, 4}, {0, 7, 8}, {1, 2, 8}, {1, 7, 11}, {2, 3, 7}, {2, 8, 2}, {2, 5, 4}, {3, 4, 9}, {3, 5, 14}, {4, 5, 10}, {5, 6, 2}, {6, 7, 1}, {6, 8, 6}, {7, 8, 7}} Output: ((A, B), Cost)
5 min read
Difference between Array, Queue and Stack
Array: An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the ar
3 min read
Dumping queue into list or array in Python
Prerequisite: Queue in Python Here given a queue and our task is to dump the queue into list or array. We are to see two methods to achieve the objective of our solution. Example 1: In this example, we will create a queue using the collection package and then cast it into the list C/C++ Code # Python program to # demonstrate queue implementation #
2 min read
Practice Tags :