TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
data_structures::stack_using_queue::Stack Struct Reference

Stack Class implementation for basic methods of Stack Data Structure. More...

Collaboration diagram for data_structures::stack_using_queue::Stack:
[legend]

Public Member Functions

int top ()
 
void push (int val)
 Inserts an element to the top of the stack.
 
void pop ()
 Removes the topmost element from the stack.
 
int size ()
 Utility function to return the current size of the stack.
 

Public Attributes

std::queue< int64_t > main_q
 stores the current state of the stack
 
std::queue< int64_t > auxiliary_q
 
uint32_t current_size = 0
 stores the current size of the stack
 

Detailed Description

Stack Class implementation for basic methods of Stack Data Structure.

Definition at line 31 of file stack_using_queue.cpp.

Member Function Documentation

◆ pop()

void data_structures::stack_using_queue::Stack::pop ( )
inline

Removes the topmost element from the stack.

Returns
void

Definition at line 62 of file stack_using_queue.cpp.

62 {
63 if (main_q.empty()) {
64 return;
65 }
66 main_q.pop();
68 }
std::queue< int64_t > main_q
stores the current state of the stack
uint32_t current_size
stores the current size of the stack

◆ push()

void data_structures::stack_using_queue::Stack::push ( int val)
inline

Inserts an element to the top of the stack.

Parameters
valthe element that will be inserted into the stack
Returns
void

Definition at line 48 of file stack_using_queue.cpp.

48 {
49 auxiliary_q.push(val);
50 while (!main_q.empty()) {
51 auxiliary_q.push(main_q.front());
52 main_q.pop();
53 }
54 swap(main_q, auxiliary_q);
56 }

◆ size()

int data_structures::stack_using_queue::Stack::size ( )
inline

Utility function to return the current size of the stack.

Returns
current size of stack

Definition at line 74 of file stack_using_queue.cpp.

74{ return current_size; }

◆ top()

int data_structures::stack_using_queue::Stack::top ( )
inline

Returns the top most element of the stack

Returns
top element of the queue

Definition at line 41 of file stack_using_queue.cpp.

41{ return main_q.front(); }

Member Data Documentation

◆ auxiliary_q

std::queue<int64_t> data_structures::stack_using_queue::Stack::auxiliary_q

used to carry out intermediate operations to implement stack

Definition at line 33 of file stack_using_queue.cpp.

◆ current_size

uint32_t data_structures::stack_using_queue::Stack::current_size = 0

stores the current size of the stack

Definition at line 35 of file stack_using_queue.cpp.

◆ main_q

std::queue<int64_t> data_structures::stack_using_queue::Stack::main_q

stores the current state of the stack

Definition at line 32 of file stack_using_queue.cpp.


The documentation for this struct was generated from the following file: