3
3
* @details
4
4
* Using 2 Queues inside the Stack class, we can easily implement Stack
5
5
* data structure with heavy computation in push function.
6
- *
7
- * References used: [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue)
6
+ *
7
+ * References used:
8
+ * [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue)
8
9
* @author [tushar2407](https://github.com/tushar2407)
9
10
*/
10
- #include < iostream > // / for IO operations
11
- #include < queue > // / for queue data structure
12
- #include < cassert > // / for assert
11
+ #include < cassert > // / for assert
12
+ #include < iostream > // / for IO operations
13
+ #include < queue > // / for queue data structure
13
14
14
15
/* *
15
16
* @namespace data_strcutres
18
19
namespace data_structures {
19
20
/* *
20
21
* @namespace stack_using_queue
21
- * @brief Functions for the [Stack Using Queue](https://www.studytonight.com/data-structures/stack-using-queue) implementation
22
+ * @brief Functions for the [Stack Using
23
+ * Queue](https://www.studytonight.com/data-structures/stack-using-queue)
24
+ * implementation
22
25
*/
23
26
namespace stack_using_queue {
27
+ /* *
28
+ * @brief Stack Class implementation for basic methods of Stack Data Structure.
29
+ */
30
+ struct Stack {
31
+ std::queue<int64_t > main_q; // /< stores the current state of the stack
32
+ std::queue<int64_t > auxiliary_q; // /< used to carry out intermediate
33
+ // /< operations to implement stack
34
+ uint32_t current_size = 0 ; // /< stores the current size of the stack
35
+
24
36
/* *
25
- * @brief Stack Class implementation for basic methods of Stack Data Structure.
37
+ * Returns the top most element of the stack
38
+ * @returns top element of the queue
26
39
*/
27
- struct Stack
28
- {
29
- std::queue<int64_t > main_q; // /< stores the current state of the stack
30
- std::queue<int64_t > auxiliary_q; // /< used to carry out intermediate operations to implement stack
31
- uint32_t current_size = 0 ; // /< stores the current size of the stack
32
-
33
- /* *
34
- * Returns the top most element of the stack
35
- * @returns top element of the queue
36
- */
37
- int top ()
38
- {
39
- return main_q.front ();
40
- }
40
+ int top () { return main_q.front (); }
41
41
42
- /* *
43
- * @brief Inserts an element to the top of the stack.
44
- * @param val the element that will be inserted into the stack
45
- * @returns void
46
- */
47
- void push (int val)
48
- {
49
- auxiliary_q.push (val);
50
- while (!main_q.empty ())
51
- {
52
- auxiliary_q.push (main_q.front ());
53
- main_q.pop ();
54
- }
55
- swap (main_q, auxiliary_q);
56
- current_size++;
57
- }
58
-
59
- /* *
60
- * @brief Removes the topmost element from the stack
61
- * @returns void
62
- */
63
- void pop ()
64
- {
65
- if (main_q.empty ()) {
66
- return ;
67
- }
42
+ /* *
43
+ * @brief Inserts an element to the top of the stack.
44
+ * @param val the element that will be inserted into the stack
45
+ * @returns void
46
+ */
47
+ void push (int val) {
48
+ auxiliary_q.push (val);
49
+ while (!main_q.empty ()) {
50
+ auxiliary_q.push (main_q.front ());
68
51
main_q.pop ();
69
- current_size--;
70
52
}
53
+ swap (main_q, auxiliary_q);
54
+ current_size++;
55
+ }
71
56
72
- /* *
73
- * @brief Utility function to return the current size of the stack
74
- * @returns current size of stack
75
- */
76
- int size ()
77
- {
78
- return current_size ;
57
+ /* *
58
+ * @brief Removes the topmost element from the stack
59
+ * @returns void
60
+ */
61
+ void pop () {
62
+ if (main_q. empty ()) {
63
+ return ;
79
64
}
80
- };
65
+ main_q.pop ();
66
+ current_size--;
67
+ }
68
+
69
+ /* *
70
+ * @brief Utility function to return the current size of the stack
71
+ * @returns current size of stack
72
+ */
73
+ int size () { return current_size; }
74
+ };
81
75
} // namespace stack_using_queue
82
76
} // namespace data_structures
83
77
84
78
/* *
85
79
* @brief Self-test implementations
86
80
* @returns void
87
81
*/
88
- static void test ()
89
- {
82
+ static void test () {
90
83
data_structures::stack_using_queue::Stack s;
91
- s.push (1 ); // / insert an element into the stack
92
- s.push (2 ); // / insert an element into the stack
93
- s.push (3 ); // / insert an element into the stack
94
-
95
- assert (s.size ()== 3 ); // / size should be 3
96
-
97
- assert (s.top ()== 3 ); // / topmost element in the stack should be 3
98
-
99
- s.pop (); // / remove the topmost element from the stack
100
- assert (s.top ()== 2 ); // / topmost element in the stack should now be 2
101
-
102
- s.pop (); // / remove the topmost element from the stack
103
- assert (s.top ()== 1 );
104
-
105
- s.push (5 ); // / insert an element into the stack
106
- assert (s.top ()== 5 ); // / topmost element in the stack should now be 5
107
-
108
- s.pop (); // / remove the topmost element from the stack
109
- assert (s.top ()== 1 ); // / topmost element in the stack should now be 1
110
-
111
- assert (s.size ()== 1 ); // / size should be 1
84
+ s.push (1 ); // / insert an element into the stack
85
+ s.push (2 ); // / insert an element into the stack
86
+ s.push (3 ); // / insert an element into the stack
87
+
88
+ assert (s.size () == 3 ); // / size should be 3
89
+
90
+ assert (s.top () == 3 ); // / topmost element in the stack should be 3
91
+
92
+ s.pop (); // / remove the topmost element from the stack
93
+ assert (s.top () == 2 ); // / topmost element in the stack should now be 2
94
+
95
+ s.pop (); // / remove the topmost element from the stack
96
+ assert (s.top () == 1 );
97
+
98
+ s.push (5 ); // / insert an element into the stack
99
+ assert (s.top () == 5 ); // / topmost element in the stack should now be 5
100
+
101
+ s.pop (); // / remove the topmost element from the stack
102
+ assert (s.top () == 1 ); // / topmost element in the stack should now be 1
103
+
104
+ assert (s.size () == 1 ); // / size should be 1
112
105
}
113
106
114
107
/* *
@@ -119,8 +112,7 @@ static void test()
119
112
* declared above.
120
113
* @returns 0 on exit
121
114
*/
122
- int main ()
123
- {
115
+ int main () {
124
116
test (); // run self-test implementations
125
117
return 0 ;
126
118
}
0 commit comments