#14.Journey to DSA in C++

#14.Journey to DSA in C++

ยท

6 min read

"Hey there! ๐ŸŒŸ It's Joyshree ๐Ÿ‘‹, and I'm absolutely stoked to kickstart my journey into the wonderful world of Data Structures and Algorithms (DSA) using C++.

Lets get started with stack,

Greetings, dear readers! In this blog post, I'm excited to take you on a journey into the fascinating world of stacks in Data Structures and Algorithms (DSA). Stacks are not only essential components of computer science but also incredibly practical in real-life applications. In this article, we will explore the concept of stacks, learn about their crucial techniques and operations, and uncover the myriad real-world scenarios where stacks play a pivotal role. Moreover, I'll emphasize the significance of stacks in DSA and provide you with a practical C++ program to solidify your understanding.

Understanding the Stack Concept:

Let's start with the basics. A stack is a linear data structure that adheres to the Last-In-First-Out (LIFO) principle. Think of it like a stack of books - the last book you put on the stack is the first one you can grab. This seemingly simple concept is at the heart of many algorithms and applications.

Key Stack Operations:

  1. Push: The "push" operation is all about adding an element to the top of the stack. It's like putting a new book on top of the book pile.

  2. Pop: Conversely, "pop" is the operation that removes the top element from the stack and hands it over to you. It's akin to taking the top book from the stack.

  3. isEmpty: Before reaching for a book from the stack, you'd want to ensure there are books to take. The "isEmpty" operation helps us with that. If the stack is empty, it returns true; otherwise, it's false.

  4. isFull: In some cases, a stack might have a fixed capacity. The "isFull" operation checks if you've reached that limit.

  5. Peek: Sometimes, you're just curious about the book on top; you don't want to take it off. The "peek" operation allows you to do just that - it lets you look at the top book without removing it.

Stacks in Real Life:

What's fascinating about stacks is that their principle mirrors real-life situations where LIFO is at play. Let's explore some everyday examples:

  1. Browser History: When you hit the "back" button while browsing the web, it's essentially a stack in action. The last webpage you visited is the first one you can revisit.

  2. Undo/Redo Functionality: In software applications, the undo and redo functions rely on stacks. Each action you take is pushed onto the stack, enabling you to navigate your actions backward and forward.

  3. Call Stack in Function Calls: When you call functions in your code, they are neatly stacked up in a call stack. The one on top is the one currently executing, and as soon as it's done, it's popped off the stack.

  4. Expression Evaluation: Stacks are indispensable for parsing and evaluating expressions, whether they are mathematical equations or programming syntax. Stacks help maintain the order of operations and ensure that parentheses are balanced.

The Significance of Stacks in DSA:

Now, let's delve into why stacks are not just theoretical constructs but vital in DSA:

  • Simplicity: Stacks are one of the most straightforward data structures to understand and implement. They serve as an excellent entry point for anyone learning about data structures.

  • Efficiency: The push and pop operations are incredibly efficient, boasting a constant time complexity of O(1).

  • Recursive Algorithms: Many recursive algorithms can be efficiently implemented using stacks. They provide an iterative approach to solving problems that might otherwise be challenging to solve using recursion alone.

  • Expression Evaluation: Stacks are pivotal for parsing and evaluating expressions, including arithmetic expressions and programming language syntax. They ensure that operations are performed in the correct order, making them fundamental for compilers and interpreters.

  • Memory Management: In programming, stacks are used for memory management, particularly in maintaining information about function calls. They help in allocating and deallocating memory for function parameters, local variables, and return addresses.

C++ Program Example:

To put our newfound knowledge into practice, let's explore a simple C++ program that demonstrates push, pop, isEmpty, and peek operations:

#include <iostream>
#include <stack>

int main() {
    std::stack<int> myStack;

    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    std::cout << "Top element: " << myStack.top() << std::endl;
    myStack.pop();

    if (myStack.empty()) {
        std::cout << "Stack is empty." << std::endl;
    } else {
        stack<std::cout << "Stack is not empty." << std::endl;
    }

    return 0;
}
  1. Header Includes: The code begins by including two header files: <iostream> for input and output operations and <stack> for stack data structure.

  2. Main Function: This is where the program's execution starts. The main function is the entry point for C++ programs.

  3. Stack Creation: Inside the main function, a stack named myStack of integer type (std::stack<int>) is declared. This stack is used to store integer values.

  4. Pushing Elements: Three integer values (10, 20, and 30) are pushed onto the stack using the push method. The order in which they are pushed is important because the last value pushed (30) will be at the top of the stack.

  5. Accessing and Printing the Top Element: The code uses myStack.top() to access and retrieve the top element of the stack, which is 30 in this case. This value is then printed to the console using std::cout, resulting in the output "Top element: 30."

  6. Popping an Element: The myStack.pop() method is called to remove the top element (30) from the stack. After this operation, the top element of the stack will be 20.

  7. Checking if the Stack is Empty: An if statement is used to check whether the stack is empty or not. It does this by calling the myStack.empty() function, which returns true if the stack is empty and false if it contains elements.

    • In this specific case, the stack is not empty since it still contains the element 20 after popping 30. Therefore, the program prints "Stack is not empty."
  8. Return Statement: The main function concludes with return 0;, indicating a successful program execution. It's common practice to return 0 from the main function to indicate that the program ran without errors.

That's the breakdown of the code. It demonstrates basic operations on a stack, such as pushing, popping, checking for emptiness, and accessing the top element. The output of this program should be:

Top element: 30
Stack is not empty.

The key takeaway is understanding how a stack works and how to use the std::stack container from the C++ Standard Library.

In conclusion, understanding stacks is pivotal in the realm of DSA. They are simple yet incredibly versatile data structures with a wide range of real-life applications. By mastering stack operations and comprehending their significance, you'll be better prepared to tackle intricate algorithms and efficiently solve real-world problems. So, don't underestimate the power of this unassuming yet indispensable data structure! Happy coding! ๐Ÿ˜Š๐Ÿ“š๐Ÿš€

ย