Question: tacks and queues are basic data structures that all programmers should be familiar with. Each has many uses -- if you'd like to browse through
tacks and queues are basic data structures that all programmers should be familiar with. Each has many uses -- if you'd like to browse through some of these uses, visit these sites:
- https://www.geeksforgeeks.org/stack-data-structure/ (Links to an external site.)
- https://www.geeksforgeeks.org/queue-data-structure/ (Links to an external site.)
Java has convenient implementations of both these abstract data structures. For the most part, as a user of these Java classes, it is not important for you to know exactly how your stack or queue is implemented, provided it provides the functionality you expect from a stack or a queue. For your information, however, stacks are usually implemented with arrays, while queues are often implemented using another structure called a linked list (which you will learn about later in the quarter).
As programmers, you probably should have a sense of how these structures could be implemented. The following .java files give array implementations for both:
-see below for code
The ArrayStack is just implemented to accept integers, while the ArrayQueue can accept any type of Java object (i.e. it is implemented using generics). I suggest reading through the Stack implementation first.
Similar to the practice exercise for Week 6, I want you to read through the code and answer the following questions in a .pdf or a .doc file.
When answering the questions, be detailed about the changes that take place -- specify array sizes and index numbers where appropriate. If items need to be copied or moved around, discuss this. What methods are being called in order to complete the task? What do the methods do? Show that you understand how the data is being manipulated in each case to provide the desired results and functionality.
5. Look at the pre-condition and post-condition for nextInt (reproduced below):
private int nextIndex(int i) // Precondition: 0
- What method uses nextIndex( )?
- Why is the precondition necessary?
- Why does the behavior described postcondition make sense?
6. Work on one of the tasks below for no more than 1 hour (use the STARTER CODE):
TASKS:
- You are only allowed to use integer arrays and ArrayStack data structures. You can step through the array from index 0 to n only. Given an array of numbers from 1 to 9, in order, reverse the array so that the numbers are now in decreasing order.
- You have been given an ArrayQueue to which the following numbers have been added: 3, 5, 2, 1, 4. Using only the commands to add and remove values from the queue, sort the ArrayQueue (you can use variables to store some information and you may create more ArrayQueue structures, but no other data storage structures). I consider this the harder of the two tasks.
The output of the program with both problems solved should be:
-see below
7. After one hour, look at one possible solution for your chosen task. Comment on where you got stuck (if you did). Try to understand what the code in the solution provided is doing and explain this in English (not code) here. If you got the solution within the hour allotted, explain your solution instead.
Page Postcondition: . This queue's capacity has been changed to its current size. @exception out of everyError Indicates insufficient nenpry for altering the capacity. **/ @suppresswarnings ("unchecked) public void tran Toize!! El trindarray: int 11, 2; if (data length == ranyitens) 1/ No change needed return; else if (hanyIters == 0) // Just change the size of the array to a because the queue is empty. data - (ECT) new object[a] else if (front c= rear) { // Create trimmed array and copy data front)...data[rear] into it. trimmedArray) neu Object (ranya! Systen.arraycopy data, front, trimedarray, front, many Iters): data = trintedArray: } else { // Create a trimmed array, hut be careful about copying icers into it. The queue iters 17 occur in tu segrets. The first segment goes from date [front] to the end of the 17 array, and the second segrent goes froe data[6] to data(rear). The variables ni 1 and 2 will be set to the number of iters in these two segments. We will copy. // these segments to triedarray[e...any Items-1). triedarray()) neu object[rany]: n1 = data. Length front; n2 = rear + 1; Systen.arraycopy data, frant, trimmedarray, 6, 2); Systen.arraycopyidata, o, trivredArray, ni, n2); front = 0; rear = nany Iters-1; data - trivedArray: front, , 6, 13)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
