This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finding the Largest Area Under a Histogram using Stacks | |
// p9Scripts | |
/* | |
Question: | |
1) Find the Largest Area Under a Histogram. Each bar of Histogram is considered of width 1. | |
Algorithm: | |
Here we use stack for finding Largest Area under a Histogram. | |
1) Assumptions: Each bar of Histogram is of width 1 unit. | |
MaxArea = -1 ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finding the stocks span problems using Stack | |
// p9Scripts | |
/* | |
Question: Find the spans for the given set of values in a list. | |
Span = count of elements just previous to the element and less than the element's value (consecutive). | |
Algorithm: | |
Stacks can be used to solve this problem in a Time complexity of o(n). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementing a Stack using 2 Queues | |
// p9Scripts. | |
/* | |
Here we use standard c++ stl library. | |
We use queue stl and Implement a stack using the queues. | |
*/ | |
/* | |
Question: Implementation of Stack using Queues | |
Algorithm: | |
Let q1 and q2 be two queues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementing a Queue using two Stacks. | |
// p9Scripts | |
/* | |
Q: Implementation of Queue using stacks. | |
Here we use stack c++ stl to build a queue | |
Operations that we design for a queue: | |
Stacks s1 and s2 | |
Enqueue: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reversing the elements of stack using Recursion | |
//p9scripts | |
/* | |
Q: Reverse the elements of stack using Recursion. | |
Algorithm: | |
Given a stack of n elements, we need to reverse the elements of stack. | |
Here we have two functions. | |
1) ReverseFunction : |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Evaluation of Infix Expression using Two Stacks | |
// p9Scripts | |
/* | |
Q: Evalution of Infix Expression using Two Stacks. | |
Algo: Here we have two stacks. | |
1) Operator Stack | |
2) Operand Stack. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Evaluation of a Postfix Expression using Stack | |
// p9Scripts | |
/* | |
Q:Evaluation of a Postfix Expressoin using Stack | |
Algorithm: | |
We process the Postfix String from List | |
We use a Stack Stl (Operand Stack) for our Algorithm | |
If the present character is operand , push to stack. | |
If the present character is operator, pop top two elements of stack. perform opeartion between them and push to stack. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Conversion from an Infix Expression to Postfix Expression using stacks | |
// p9Scripts | |
/* | |
Q) Convert an Infix Expression to Post Expression using Stack. | |
Algorithm: | |
1) We use Stack stl for our purpose. | |
2) Process the Infix Expression from left to right. | |
3) For each character in string: | |
If its an operand, output it and move to next. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Balancing Symbols ---Braces like [],(),{}--- using Stacks | |
// p9scripts | |
/* | |
Q: Find out whether a given string of symbols is balanced or not ? | |
Algorithm: | |
We use C++ stack stl. | |
Move from left to right of the string. | |
1) Push into stack if you encounter any opening brace [ , { , ( | |
2) When you encounter a closing brace, compare it with the top of stack. | |
If its matching with its counter opening brace. continue to next by popping the stack. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stack Using a Linkedlists | |
// p9Scripts | |
// Implementaion of Stack using a Linkedlists | |
#include<iostream> | |
using namespace std; |
NewerOlder