
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![1. Implement, using only the Queue kernel methods, the static method declared as follows and
answer the questions below:
Reports the smallest integer in the given {@code Queue<Integer>}.
@param q
the queue of integer
@return the smallest integer in the given queue
@requires q /= empty_string
@ensures <pre>
min is in entries (q) and
for all x: integer
where (x is in entries (q))
(min <= x)
</pre>
ivate static int min (Queue<Integer> q) {...}
i. Why do you need the requires clause?
ii. Why is the first line of the ensures clause important (min is in entries (q))?
Explain what the implementation could do if this line was not included in the
postcondition.
2. Implement the static method declared as follows:
Reports an array of two {@code int}s with the smallest and the
* largest integer in the given {@ code Queue<Integer>}.
* @param q
the queue of integer
@return an array of two (@code int}s with the smallest and the
largest integer in the given queue
* @requires q /= empty_string
@ensures <pre>
{ minAndMax [0], minAndMax [1] } is subset of entries (q) and
for all x: integer
where (x in in entries (q))
(minAndMax [0] <= x <= minAndMax [1])
* </pre>
* /
private static int [] minAndMax (Queue<Integer> q) {...}
Note that because in Java a function can return only one value and it is not possible to
return other values through the parameters, we are forced to return an array with the
minimum and maximum integers of the given queue as elements.
3. Consider the minAndMax operation again. Write a second implementation for this operation
that uses the "Noah's Ark" algorithm. This algorithm takes entries from q in pairs, first
comparing them to each other, then comparing the smaller of the pair to the minimum so far
and the larger of the pair to the maximum so far.](https://content.bartleby.com/qna-images/question/da5cf1ef-ed5b-45b4-b720-27437f28fd1b/f165f5cb-ed34-4510-9c0c-edd846f2034a/xuhctjb_thumbnail.jpeg)
Transcribed Image Text:1. Implement, using only the Queue kernel methods, the static method declared as follows and
answer the questions below:
Reports the smallest integer in the given {@code Queue<Integer>}.
@param q
the queue of integer
@return the smallest integer in the given queue
@requires q /= empty_string
@ensures <pre>
min is in entries (q) and
for all x: integer
where (x is in entries (q))
(min <= x)
</pre>
ivate static int min (Queue<Integer> q) {...}
i. Why do you need the requires clause?
ii. Why is the first line of the ensures clause important (min is in entries (q))?
Explain what the implementation could do if this line was not included in the
postcondition.
2. Implement the static method declared as follows:
Reports an array of two {@code int}s with the smallest and the
* largest integer in the given {@ code Queue<Integer>}.
* @param q
the queue of integer
@return an array of two (@code int}s with the smallest and the
largest integer in the given queue
* @requires q /= empty_string
@ensures <pre>
{ minAndMax [0], minAndMax [1] } is subset of entries (q) and
for all x: integer
where (x in in entries (q))
(minAndMax [0] <= x <= minAndMax [1])
* </pre>
* /
private static int [] minAndMax (Queue<Integer> q) {...}
Note that because in Java a function can return only one value and it is not possible to
return other values through the parameters, we are forced to return an array with the
minimum and maximum integers of the given queue as elements.
3. Consider the minAndMax operation again. Write a second implementation for this operation
that uses the "Noah's Ark" algorithm. This algorithm takes entries from q in pairs, first
comparing them to each other, then comparing the smaller of the pair to the minimum so far
and the larger of the pair to the maximum so far.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- This is the souce code for Queues, but it has so many errors. Please update this without errors and warnings. template<class T>int queue <T>::isDecreasing() const{ if (size() < 2) return true; cout << "test" << endl; for (size_t i = 0; i < size() - 1; i++) { cout << (i + 1 + _front) % _capacity << " : " << elements[(i + 1 + _front) % _capacity] << " vvvvv " << (i + _front) % _capacity << " : " << elements[(i + _front) % _capacity] << endl; if (elements[(i + 1 + _front) % _capacity] > elements[(i + _front) % _capacity]) return false; } return true;} template<class T>size_t queue<T>::count(const T& data) const;{ size_t counter = 0; for (size_t i = 0; i < size(); i++) { cout << i << ":" << elements[(_front + i) % _capacity] << endl; if (elements[(_front + i) % _capacity] == data) counter++; } return…arrow_forwardIn python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardcomplete TODO's using Javaarrow_forward
- Can you write a program in python for the dining philosopher problems that do not have deadlock? You have to use semaphores and use the strategy that a philosopher only picks the fork if both (left and right) are available. def philosopher(id: int, fork: list): The main is if __name__ == "__main__": semaphoreList = list() #this list will hold one semaphore per fork Philosophers = 5 for i in range(Philosophers): semaphoreList.append(multiprocessing.Semaphore(1)) philosopherList = list() for i in range(Philosophers): #instantiate all processes representing philosophers philosopherList.append(multiprocessing.Process(target=philosopher, args=(i, semaphoreList))) for j in range(Philosophers): #start all child processes philosopherList[j].start() for k in range(Philosophers): #join all child processes philosopherList[k].join() sample output: DEBUG: philosopher2 has chopstick2 DEBUG: philosopher2 has chopstick3 DEBUG: philosopher2 eating DEBUG: philosopher1 has chopstick1 DEBUG: philosopher0…arrow_forwardWrite a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Cannot use <queue> Library, have to use queueADT class as shown in the below, "Queue Demo." Thank you, will up vote. #include <iostream> #include <cassert>using namespace std; template <class Type>class queueADT{public: virtual bool isEmptyQueue() const = 0; virtual bool isFullQueue() const = 0; virtual void initializeQueue() = 0; virtual Type front() const = 0; //Function to return the first element of the queue. virtual Type back() const = 0; //Function to return the last element of the queue. virtual void addQueue(const Type& queueElement) = 0; //Function to add queueElement to the queue. virtual void deleteQueue() = 0; //Function to remove…arrow_forwardUsing Queues Operation Enqueue,Dequeue, Write a algorithm of method names Contain_Equal that takes the strrings "aaabbb" in queue form as input and returns that the strings contain equal numbers of a's and b's A A A В В В Ouput Must be Like: Both A's and B's are Equal In Quantity Which Is 3.arrow_forward
- Implement a circular queue by writing circular.h and circular.c. Here are the contents of circular.h: #define QUEUE_SIZE 100 typedef struct { int head; int tail; int itemCount; int items[QUEUE_SIZE]; } CircularQueue; void CircularInitialize(CircularQueue * q); void CircularEnqueue (CircularQueue * q, int value); int Circular Dequeue (CircularQueue * q, int *pValue); Using an array, implement a circular queue of up to 100 elements of type integer. Implement the functions CircularEnqueue() and Circular Dequeue () to place numbers in the head of the queue and read them from the tail of the queue tail. A circular queue only saves the last n entries (where n is the number of elements in the queue). Overwrite the oldest entries with newest entries once the queue is full.arrow_forwardUSE PYTHONarrow_forwarddef has_at_least(queue: Queue, n: int) -> bool:"""Return true iff queue contains at least n items. Precondition: n >= 0 >>> queue = Queue()>>> queue.enqueue(1)>>> queue.enqueue(2)>>> queue.enqueue(3)>>> has_at_least(queue, 3)True"""arrow_forward
- Javaarrow_forwardjava Suppose queue is defined as LinkedQueue<Integer> queue = new LinkedQueue<>(); Show what is written by the following segment of code. SHOW YOUR WORK.arrow_forwardimport java.util.*;import java.io.*; public class HuffmanCode { private Queue<HuffmanNode> queue; private HuffmanNode overallRoot; public HuffmanCode(int[] frequencies) { queue = new PriorityQueue<HuffmanNode>(); for (int i = 0; i < frequencies.length; i++) { if (frequencies[i] > 0) { HuffmanNode node = new HuffmanNode(frequencies[i]); node.ascii = i; queue.add(node); } } overallRoot = buildTree(); } public HuffmanCode(Scanner input) { overallRoot = new HuffmanNode(-1); while (input.hasNext()) { int asciiValue = Integer.parseInt(input.nextLine()); String code = input.nextLine(); overallRoot =…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education