Question

In: Computer Science

in java, starting with an empty list, show conents of an array (including their index), size...

in java, starting with an empty list, show conents of an array (including their index), size 4, implimenting a circular queue after the following operations are performed:

insert(2)
insert(7)
insert(9)
delete()
delete()
insert(5)
detete()
delete()
insert(15)

Solutions

Expert Solution

Java Code:

import java.util.*;

class queue

{

    int rear,front;

    int size;

    int arr[];

    queue(int n)

    {

        this.front=this.rear=-1;

        this.size=n;

        arr = new int[n];

    }

    void insert(int value)

    {

        //Queue is full

        if((front == 0 && rear == -1) || (rear==(front-1)%(size-1)))

        {

            System.out.println("Queue is full\n");

            return;

        }

        //Queue is empty inserting first element

        else if(front == -1)

        {

            front = rear = 0;

            arr[rear]=value;

        }

        //inserting after deletion

        else if (rear == size-1 && front != 0)

        {

            rear = 0;

            arr[rear] = value;

        }

        //normal insertion

        else

        {    

            rear++;

            arr[rear] = value;

        }

    }

    int delete()

    {

        //Queue is empty if front=-1

        if(front == -1)

        {

            System.out.println("Queue is empty");    

            return -1;

        }

        //storing the deleting element in del

        int del = arr[front];

        //setting the index of deleted item to -1

        arr[front]= -1;

        if (front == rear)

        {

            front=rear=-1;

            //after this Queue is empty

        }

        else if(front == size-1)

            front=0;

        else

            front++;

        return del;

    }

    void display()

    {

        int i;

        if(front == -1)

        //Queue is empty if front=-1

        {

            System.out.println("Queue is empty\n");

            return;

        }

        System.out.println("Elements in circular Queue are:");

        System.out.println("Element  Index");

        if(rear >= front)

        //normally with doing circulation

        {

            for(i=front;i<=rear;i++)

            {

                System.out.println(arr[i]+"        "+i);

            }

        }

        else

        {

            for(i=front;i<size;i++)

                System.out.println(arr[i]+"        "+i);

            for(i=0;i<=rear;i++)

                System.out.println(arr[i]+"        "+i);

        }

    }

}

public class Main

{

    public static void main(String[] args) {

        queue obj = new queue(4);

        //inserting into Queue

        obj.insert(2);

        obj.insert(7);

        obj.insert(9);

        obj.display();

        //Deleting from Queue

        obj.delete();

        obj.delete();

        obj.display();

        //Insertion after deletion

        obj.insert(5);

        obj.display();

        //deleting all the Elements from queue

        obj.delete();

        obj.delete();

        obj.display();

        //insering 15 into empty queue

        obj.insert(15);

        obj.display();

    }

}

Output:

If you have any queries, please comment below.

Please upvote , if you like this answer.


Related Solutions

Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
1. An array has an index of [5] at the starting address of 200. It has...
1. An array has an index of [5] at the starting address of 200. It has 3 words per memory cell, determine loc[3],loc[4] and NE. (3 Marks: 1 mark for each) 2. A 2-D array defined as A[10 , 5] requires 4 words of storage space for each element. Calculate the address of A[4,3] given the base address as 250 • If the array is stored in Row-major form • If the array is stored in Column-major form 3. Write...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed. Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.) Hint: To understand the test cases, note that the size (but not the capacity)...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (in Java)
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT