Question

In: Computer Science

Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data...

Data Structures and Algorithms Activity

Requirement: Implement a queue using an array as its underline data structure. Your queue should fully implemnted the following methods: first, push_back (enqueue), pop_front (dequeue), size, and isEmpty. Make sure to include a driver to test your newly implemented queue

Solutions

Expert Solution

Algorithm:

  1. First of all, we are creating an array and initializing its front and rear points.
  2. Whenever we adding any value to the queue, the rear value gets increased.
  3. When we pop any value, front value also gets increased but the difference of rear and front can only be equal to the initialized size of the queue at max.
  4. We are using the constructor to initialize the capacity of the Queue.
  5. Note: Please take care of the package while running the program on your system.
  6. Read the program from the starting line-by-line for better understanding.

Please refer to the comments of the program for more clarity.

Main.java / Driver code:

package com.company;

public class Main {

    public static void main(String[] args) {
        Queue q = new Queue(4);

        // print Queue elements
        q.queueDisplay();

        // inserting elements in the queue
        q.push_back (50);
        q.push_back (10);
        q.push_back (6);
        q.push_back (588);

        // print Queue elements
        q.queueDisplay();

        // insert element in the queue
        q.push_back (60);

        // print Queue elements
        q.queueDisplay();

        q.pop_front();
        q.pop_front();
        System.out.printf("\n\nAfter two node deletion\n\n");

        q.push_back (60); // Adding a new element
        // print Queue elements
        q.queueDisplay();

        // print front of the queue
        q.first();

        // Checking queue is empty or not
        System.out.println("\n\nIs Queue empty: " + q.isEmpty());

        // Checking size of the queue
        System.out.println("nSize of the queue: " + q.size());
    }
}

Queue.java:

package com.company;

// Queue class to implement a queue using array
class Queue {

    // Declaring variables
    private static int front, rear, capacity;
    private static int queue[];


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

    // function to insert an element
    // at the rear of the queue
    static void push_back(int data) {
        // check queue is full or not
        if (capacity == rear) {
            System.out.printf("\nQueue is full\n");
            return;
        }

        // insert element at the rear
        else {
            queue[rear] = data;
            rear++;
        }
        return;
    }

    // function to delete an element
    // from the front of the queue
    static void pop_front() {
        // if queue is empty
        if (front == rear) {
            System.out.printf("\nQueue is empty\n");
            return;
        }

        // shift all the elements from index 2 till rear
        // to the right by one
        else {
            for (int i = 0; i < rear - 1; i++) {
                queue[i] = queue[i + 1];
            }

            // store 0 at rear indicating there's no element
            if (rear < capacity)
                queue[rear] = 0;

            // decrement rear
            rear--;
        }
        return;
    }

    // print queue elements
    static void queueDisplay()
    {
        int i;
        if (front == rear) {
            System.out.printf("\nQueue is Empty\n");
            return;
        }

        // traverse front to rear and print elements
        for (i = front; i < rear; i++) {
            System.out.printf(" %d < ", queue[i]);
        }
        return;
    }

    // print front of queue
    static void first()
    {
        if (front == rear) {
            System.out.printf("\nQueue is Empty\n");
            return;
        }
        System.out.printf("\nFront Element is: %d", queue[front]);
        return;
    }

    // Check if Queue is empty or not

    static boolean isEmpty(){
        if(front == rear)
            return true;
        else
            return false;
    }

    static int size(){
        return rear - front;
    }
}

Sample Input-Output/CodeRun:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
This question is in reference to BFS and DFS for data structures and algorithms Consider a...
This question is in reference to BFS and DFS for data structures and algorithms Consider a graph algorithm with a growth function on V and E: f(V, E). How would you convert f(V,E) to f'(V) such that f(V,E)=O(g(n))=f(V)? (That is, convert a growth function of two variables to be of one variable in such a way that the Big-Oh bound for the one variable function will hold for the two variable function.) Explain the steps in creating f', and explain...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT