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...
Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
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...
An array has an index of [5] at the starting address of 200. It has 3...
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) 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 Write a method for the following...
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...
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.
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)
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT