Skip to content

Commit 971e6e6

Browse files
Merge pull request #180 from AnshumanAtrey/main
Stack vs Queue in CPP
2 parents db3a945 + 630034a commit 971e6e6

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

C++/queue/stack_vs_queue.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <stdexcept>
4+
5+
class Stack {
6+
private:
7+
std::vector<int> elements; // Vector to store stack elements
8+
9+
public:
10+
// Push an element onto the stack
11+
void push(int value) {
12+
elements.push_back(value); // Add value to the end of the vector
13+
}
14+
15+
// Remove the top element from the stack
16+
void pop() {
17+
if (isEmpty()) {
18+
throw std::out_of_range("Stack is empty"); // Throw error if stack is empty
19+
}
20+
elements.pop_back(); // Remove the last element from the vector
21+
}
22+
23+
// Get the top element of the stack
24+
int top() const {
25+
if (isEmpty()) {
26+
throw std::out_of_range("Stack is empty"); // Throw error if stack is empty
27+
}
28+
return elements.back(); // Return the last element of the vector
29+
}
30+
31+
// Check if the stack is empty
32+
bool isEmpty() const {
33+
return elements.empty(); // Return true if vector is empty
34+
}
35+
36+
// Get the size of the stack
37+
size_t size() const {
38+
return elements.size(); // Return the number of elements in the vector
39+
}
40+
};
41+
42+
class Queue {
43+
private:
44+
std::vector<int> elements; // Vector to store queue elements
45+
46+
public:
47+
// Add an element to the back of the queue
48+
void enqueue(int value) {
49+
elements.push_back(value); // Add value to the end of the vector
50+
}
51+
52+
// Remove the front element from the queue
53+
void dequeue() {
54+
if (isEmpty()) {
55+
throw std::out_of_range("Queue is empty"); // Throw error if queue is empty
56+
}
57+
elements.erase(elements.begin()); // Remove the first element from the vector
58+
}
59+
60+
// Get the front element of the queue
61+
int front() const {
62+
if (isEmpty()) {
63+
throw std::out_of_range("Queue is empty"); // Throw error if queue is empty
64+
}
65+
return elements.front(); // Return the first element of the vector
66+
}
67+
68+
// Check if the queue is empty
69+
bool isEmpty() const {
70+
return elements.empty(); // Return true if vector is empty
71+
}
72+
73+
// Get the size of the queue
74+
size_t size() const {
75+
return elements.size(); // Return the number of elements in the vector
76+
}
77+
};
78+
79+
int main() {
80+
Stack stack; // Create a stack instance
81+
Queue queue; // Create a queue instance
82+
83+
// Stack operations
84+
stack.push(1); // Push 1 onto the stack
85+
stack.push(2); // Push 2 onto the stack
86+
std::cout << "Top of stack: " << stack.top() << std::endl; // Get top element
87+
stack.pop(); // Remove top element
88+
std::cout << "Top of stack after pop: " << stack.top() << std::endl; // Get new top element
89+
90+
// Queue operations
91+
queue.enqueue(1); // Enqueue 1 into the queue
92+
queue.enqueue(2); // Enqueue 2 into the queue
93+
std::cout << "Front of queue: " << queue.front() << std::endl; // Get front element
94+
queue.dequeue(); // Dequeue front element
95+
std::cout << "Front of queue after dequeue: " << queue.front() << std::endl; // Get new front element
96+
97+
return 0; // End of program
98+
}

0 commit comments

Comments
 (0)