Question

In: Computer Science

Write a c++program using queue to find the minimum value in a queue. Use the above...

Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

template <typename T>  
class MinQueue {
public:
  
   queue<T> Q;
   deque<T> D;
   void enque_element(T element)
   {
       if (Q.size() == 0) {
           Q.push(element);
           D.push_back(element);
       }
       else {
           Q.push(element);
          
           while (!D.empty() &&
           D.back() > element) {
               D.pop_back();
           }
           D.push_back(element);
       }
   }
  
   void deque_element()
   {
       if (Q.front() == D.front()) {
           Q.pop();
           D.pop_front();
       }
       else {
           Q.pop();
       }
   }
  
   T getMin()
   {
       return D.front();
   }
};

int main()
{
   MinQueue<int> k;
   int sample[10] = {60,98,110,55,34,333,444,67,89,450 };
   for (int i = 0; i < 10; i++) {
       k.enque_element(sample[i]);
   }
  
   k.deque_element();

cout << k.getMin() << "\n";
}


Related Solutions

((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue)...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue) to a dictionary table. Include any necessary fields/properties, but keep the functionality within one single method.
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
Write a program in C++ to find the number of comparisons using binarySearch and the sequential...
Write a program in C++ to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. a. Use a random number generator to fill list. b. Use any sorting algorithm to sort list. c. Search list for some items as follows: i. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT