Stack and Queue

EdPsycho Hub
0

 


Queue

A Queue data structure is a fundamental data structure in computer science. This follows the principle of First In First Out (FIFO) similar to the queue you see in day-to-day life.
In a queue, we can do only a few operations. They are insert (enqueue) and delete (dequeue). 

Enqueue(push) - Insert element to the end of the queue.
Dequeue(pop) - Remove the first element from the queue.

Example: Think of a queue of bank counters. It can only add someone to the last position or remove the first one. No one can come out first until their turn comes. You can get a simple idea by watching the below video.


Now let's see, how the queue works in C++. We can do it for our bank counter-example.
*Reading comment lines is essential to understand the code and try these codes yourself. If you are using a mobile phone to read this, make sure to rotate your screen while reading this code.
#include <iostream>
#include <queue>

using namespace std;

int main() {
// Declaring a queue
queue<string> myQueue;

// Inserting elements into the queue
myQueue.push("John"); //First Comer
myQueue.push("William"); //Second
myQueue.push("James"); //Third
myQueue.push("Robert"); //Fourth
myQueue.push("George"); //Fifth
myQueue.push("Arthur"); //Last

// Accessing the front and back elements of the queue
cout << "Front Client: " << myQueue.front() << endl;
cout << "Back Client: " << myQueue.back() << endl;

// Removing elements from the queue
myQueue.pop(); // remove John

// Accessing the front after removing John
cout << "Front Client after popping: " << myQueue.front() << endl;

// Checking if the queue is empty
if (myQueue.empty()) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue is not empty." << endl;
}

// Getting the size of the queue
cout << "Size of the queue: " << myQueue.size() << endl;

return 0;
}


Stack

This data structure is also fundamental in computer science. This follows the principle of Last In First Out (LIFO) similar to the stack you see in day-to-day life.

In a stack, we can do only a few operations. They are insert (push) and delete (pop). 

Push - Insert element to the end of the stack.
Pop - Remove the last element from the stack.

Example: Think of a stack of books. If you need the book in the middle, you need to remove all the books that are on the needed book. You can get a simple idea by watching the below video.



Now let's see, how the Stack works in C++. We can do it for our Book example.
*Reading comment lines is essential to understand the code and try these codes yourself. If you are using a mobile phone to read this, make sure to rotate your screen while reading this code.
#include <iostream>
#include <stack>

using namespace std;

int main() {
// Declaring a stack
stack<string> myStack;

// Pushing elements onto the stack
myStack.push("Red Book 1");
myStack.push("Red Book 2");
myStack.push("Blue Book");
myStack.push("Red Book 3");
myStack.push("Red Book 4");

// Accessing the top element of the stack
cout << "Book in the Top: " << myStack.top() << endl;

// Popping 2 books from the stack
myStack.pop();
myStack.pop();

// Accessing the top element after popping
cout << "Book in the Top, after popping: " << myStack.top() << endl;

// Checking if the stack is empty
if (myStack.empty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack is not empty." << endl;
}

// Getting the size of the stack
cout << "Size of the stack: " << myStack.size() << endl;

return 0;
}

Conclusion

In this post, we discussed Queues and Stacks. If you think this is valuable, please share this with your friends. Thank you for reading this and have a nice day!

Post a Comment

0Comments

Post a Comment (0)