Question

In: Computer Science

The following questions are to be done in JAVA. 1) If an array is not considered...

The following questions are to be done in JAVA.

1) If an array is not considered circular, the text suggests that each remove operation must shift down every remaining element of the queue. An alternative method is to postpone shifting until rear equals the last index of the array. When that situation occurs and an attempt is made to insert an element into the queue, the entire queue is shifted down so that the first element of the queue is in the first position of the array. What are the advantages of this method over performing a shift at each remove operation? What are the disadvantages?

2) what does the following code fragment do to the queue q?

ObjectStack s = new ObjuectStack();

while (!q.isEmpty())

s.push(q.remove());

while (!s.isEmpty())

q.insert(s.pop());

3)Describe how you might implement a queue using two stacks. Hint: If you push elements onto a stack and then pop them all, they appear in reverse order. If you repeat this process, they're now back in order.

Thank you for all the help.

Solutions

Expert Solution

1)

Advantages:

Consider a system where all the insert opeartions into the queue will be done first consequtively, and then all the remove operations will be done after that. Implementation of queue using this method will reduce un-needed swaps required in other methode performing a shift at each remove operation.

Dis-Advantages:

Consider the system where a queue is full. Now, remove and insert operations take place one by one. In this case as the array is not circular, we will have to perform a shift at each remove operation. Otherwise we can not insert new element though we have empty space into the array.

2)

Given code fragment reverses the order of elements in queue q.

Step by step:

ObjectStack s = new ObjuectStack();

while (!q.isEmpty())

s.push(q.remove()) ; // push all the elements into stack s such that end element of q is at top of s.

while (!s.isEmpty())

q.insert(s.pop()); // insert all the elements into q such that top of s is at the front of q

3)

We have to implement queue using two stacks s1 and s2. So, untill we get insert into queue requests, we keep pushing the elements into the stack s1, and when we get a remove/front request we pop all the elements from s1 and push them into s2. So now all the elements will be reversed and top of s2 will be the output.


Related Solutions

Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
An experiment was done to test the effectiveness of a drug that is being considered for...
An experiment was done to test the effectiveness of a drug that is being considered for possible use in the treatment of people who experience chronic anxiety. Fifty people who are chronically anxious are identified through a local health clinic, and all 50 people give their informed consent to participate in the experiment. Twenty-five people are randomly assigned to the experimental group, and they receive the new drug. The other 25 people are randomly assigned to the control group, and...
To be done in Java Consider the partially complete book class given.    Make the following...
To be done in Java Consider the partially complete book class given.    Make the following additions to the book class. Add a getAuthor() and a getTitle() method to the book class. They should take no parameters and should return a String. Change the 3-parameter constructor for the class so that it correctly initializes the member variables. Write an accessor method called getPages(). Add a zero parameter getDetails() method to the Book class. It should assemble the information for a...
TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It...
TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered. Create a Random object with a seed of 5. Create an ArrayList numAL of type Integer Populate numAL with 10 numbers randomly selected from 0 to 6 Print out numAL Create a TreeSet numTS of type Integer Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS....
Create a Java file that reads an excel file into an array. The array must be...
Create a Java file that reads an excel file into an array. The array must be binary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT