Home > Article > Backend Development > 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.
deque<data_type> name_of_queue;
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()
front() is a predefined function in C STL, which directly refers to the first index position of the queue.
back()
back() is a predefined function in C STL, which directly refers to the last index position of the queue.
push_back()
push_back() is also a predefined function for inserting elements from behind.
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
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.
李>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; }
Minimum element: 10 Maximum element: 15
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!