Home > Backend Development > C++ > body text

Design a queue data structure to obtain the minimum or maximum value in O(1) time

PHPz
Release: 2023-09-10 14:33:02
forward
1003 people have browsed it

Design a queue data structure to obtain the minimum or maximum value in O(1) time

C has a deque header file that handles properties of stacks and queues. In data structures, solving the problem of O(1) time complexity requires constant time. By using a deque in this program, we get the advantages of using both a stack and a queue.

In this article, we will solve the queue data structure to get the minimum or maximum value of a number in O(1) time.

grammar

deque<data_type> name_of_queue;
Copy after login

parameter

  • deque - This is known as a deque, which orders a set of items or numbers equivalent to a queue.

  • data_type - The data type used, such as int, float, etc.

  • name_of_queue - Any name given to the queue, such as ab, cd, etc.

front()
Copy after login

front() is a predefined function in C STL, which directly refers to the first index position of the queue.

back()
Copy after login

back() is a predefined function in C STL, which directly refers to the last index position of the queue.

push_back()
Copy after login

push_back() is also a predefined function for inserting elements from behind.

algorithm

  • We will use the header files 'iostream' and 'deque' to start the program.

  • We insert into the deque to handle the maximum or minimum value of the number.

    • "deque dq" - By using this we can enable properties of stacks and queues

  • Starting from the for loop, we insert elements in the range 10 to 15. Then use a method called 'push_back[i ]' which accepts 'i' as a parameter and uses a for loop to push the array elements.

  • We then create two variables using the predefined functions front() and back() to find the minimum and maximum values ​​of a number. front() looks for the first index to represent the smallest number, while back() looks for the last index to represent the largest number.

  • Now we are initializing the for loop to iterate over the index number length and use that length to classify the comparison of the smallest and largest elements as 'dq[i]'. So this will find the minimum and maximum number.

  • Finally, we print the output of minimum and maximum length with the help of 'min_element' and 'max_element' variables.

Example

In this program, we will solve the queue data structure to get the minimum and maximum values ​​in O(1) time.

#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq; 
   // double ended queue
   // insert elements into the deque using a loop
   for(int i = 10; i <= 15; i++) {
      dq.push_back(i);
   }
   // find the minimum and maximum elements
   int min_element = dq.front();
   int max_element = dq.back();

   for(int i = 1; i < dq.size(); i++) {
      if(dq[i] < min_element) {
         min_element = dq[i];
      }
      if(dq[i] > max_element) {
         max_element = dq[i];
      }
   }
   //Print the minimum and maximum elements
   cout << "Minimum element: " << min_element << endl;
   cout << "Maximum element: " << max_element << endl;
   return 0;
}
Copy after login

Output

Minimum element: 10
Maximum element: 15
Copy after login

in conclusion

We explored the concept of queue data structures to find the smallest or largest element. We saw how front() and back() can be used to find the minimum and maximum value of an element, and also how to add pushback to the end of an indexed element. By using a deque we can handle the problem in O(1) time complexity.

The above is the detailed content of Design a queue data structure to obtain the minimum or maximum value in O(1) time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!