chore(CPlusPlus): add reverse stack (#351)
Co-authored-by: Ujjwal <75884061+UG-SEP@users.noreply.github.com>pull/376/head
parent
6bc975f513
commit
a082e6a494
|
@ -1,6 +1,7 @@
|
||||||
# C++
|
# C++
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
1. [Counting Inversions](Arrays/counting-inversions.cpp)
|
1. [Counting Inversions](Arrays/counting-inversions.cpp)
|
||||||
2. [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp)
|
2. [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp)
|
||||||
3. [Left Rotation](Arrays/left-rotation.cpp)
|
3. [Left Rotation](Arrays/left-rotation.cpp)
|
||||||
|
@ -12,23 +13,27 @@
|
||||||
9. [Fractional Knapsack](Arrays/fractional-knapsack.cpp)
|
9. [Fractional Knapsack](Arrays/fractional-knapsack.cpp)
|
||||||
|
|
||||||
## Graphs
|
## Graphs
|
||||||
|
|
||||||
1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)
|
1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)
|
||||||
2. [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
|
2. [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
|
||||||
3. [Breadth First Search](Graphs/breadth-first-search.cpp)
|
3. [Breadth First Search](Graphs/breadth-first-search.cpp)
|
||||||
|
|
||||||
## Multiplication
|
## Multiplication
|
||||||
|
|
||||||
1. [Karatsuba](Multiplication/karatsuba.cpp)
|
1. [Karatsuba](Multiplication/karatsuba.cpp)
|
||||||
|
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
|
|
||||||
1. [All possible insertions](Linked-Lists/all-possible-insertion.cpp)
|
1. [All possible insertions](Linked-Lists/all-possible-insertion.cpp)
|
||||||
2. [Singly linked lists](Linked-Lists/singly.cpp)
|
2. [Singly linked lists](Linked-Lists/singly.cpp)
|
||||||
3. [doubley linked lists](Linked-Lists/doubly.cpp)
|
3. [doubley linked lists](Linked-Lists/doubly.cpp)
|
||||||
4. [Circular linked lists](Linked-Lists/circular.cpp)
|
4. [Circular linked lists](Linked-Lists/circular.cpp)
|
||||||
5. [Reversing a linked lists](Linked-Lists/reverse.cpp)
|
5. [Reversing a linked lists](Linked-Lists/reverse.cpp)
|
||||||
5. [Merging two sorted linked lists](Linked-Lists/merge.cpp)
|
6. [Merging two sorted linked lists](Linked-Lists/merge.cpp)
|
||||||
6. [Reorder List](Linked-Lists/Reorder-List.cpp)
|
6. [Reorder List](Linked-Lists/Reorder-List.cpp)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
1. [Linear Search](Searching/linear-search.cpp)
|
1. [Linear Search](Searching/linear-search.cpp)
|
||||||
2. [Jump Search](Searching/jump-search.cpp)
|
2. [Jump Search](Searching/jump-search.cpp)
|
||||||
3. [Binary Search](Searching/binary-search.cpp)
|
3. [Binary Search](Searching/binary-search.cpp)
|
||||||
|
@ -39,10 +44,16 @@
|
||||||
8. [Exponential Search](Searching/exponential-search.cpp)
|
8. [Exponential Search](Searching/exponential-search.cpp)
|
||||||
|
|
||||||
## Stacks
|
## Stacks
|
||||||
|
|
||||||
1. [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)
|
1. [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)
|
||||||
2. [Stack using Array](Stacks/stack-using-array.cpp)
|
|
||||||
|
2. [Reversing Stack](Stacks/reverse-stack.cpp)
|
||||||
|
|
||||||
|
3. [Stack using Array](Stacks/stack-using-array.cpp)
|
||||||
|
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
1. [Bubble Sort](Sorting/bubble-sort.cpp)
|
1. [Bubble Sort](Sorting/bubble-sort.cpp)
|
||||||
2. [Insertion Sort](Sorting/insertion-sort.cpp)
|
2. [Insertion Sort](Sorting/insertion-sort.cpp)
|
||||||
3. [Quicksort](Sorting/quick-sort.cpp)
|
3. [Quicksort](Sorting/quick-sort.cpp)
|
||||||
|
@ -58,6 +69,7 @@
|
||||||
13. [Merge Sort](Sorting/merge-sort.cpp)
|
13. [Merge Sort](Sorting/merge-sort.cpp)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
|
|
||||||
1. [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp)
|
1. [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp)
|
||||||
2. [All subsequence of a string (Recursion) ](Strings/sequence.cpp)
|
2. [All subsequence of a string (Recursion) ](Strings/sequence.cpp)
|
||||||
3. [String reversal](Strings/string-reverse.cpp)
|
3. [String reversal](Strings/string-reverse.cpp)
|
||||||
|
@ -65,6 +77,7 @@
|
||||||
5. [Anagram check](Strings/anagram.cpp)
|
5. [Anagram check](Strings/anagram.cpp)
|
||||||
|
|
||||||
## Trees
|
## Trees
|
||||||
|
|
||||||
1. [Creating Binary Tree](Trees/build-binary-tree.cpp)
|
1. [Creating Binary Tree](Trees/build-binary-tree.cpp)
|
||||||
2. [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp)
|
2. [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp)
|
||||||
3. [Level Order Traversal](Trees/level-order-traversal.cpp)
|
3. [Level Order Traversal](Trees/level-order-traversal.cpp)
|
||||||
|
@ -76,6 +89,7 @@
|
||||||
9. [Min Heap](Trees/min-heap.cpp)
|
9. [Min Heap](Trees/min-heap.cpp)
|
||||||
|
|
||||||
# Maths
|
# Maths
|
||||||
|
|
||||||
1. [Kaprekar Number](Maths/Kaprekar-number.cpp)
|
1. [Kaprekar Number](Maths/Kaprekar-number.cpp)
|
||||||
2. [Prime Number](Maths/prime-check.cpp)
|
2. [Prime Number](Maths/prime-check.cpp)
|
||||||
3. [Prime Sieve](Maths/prime-sieve.cpp)
|
3. [Prime Sieve](Maths/prime-sieve.cpp)
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/* the idea is to hold all items in a call stack until the stack becomes empty.
|
||||||
|
Then insert each item in the call stack at the bottom of the stack. */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stack>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Recursive function to insert an item to bottom of a given stack
|
||||||
|
void insertInBottom(stack<int> &s, int item)
|
||||||
|
{
|
||||||
|
// base case: if the stack is empty, insert given item at bottom
|
||||||
|
if (s.empty()) {
|
||||||
|
s.push(item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pop all items from the stack and hold them in call stack
|
||||||
|
int top = s.top();
|
||||||
|
s.pop();
|
||||||
|
insertInBottom(s, item);
|
||||||
|
|
||||||
|
/*After the recursion completes , push each item in the call stack
|
||||||
|
to top of the stack*/
|
||||||
|
s.push(top);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive function to reverse a given stack
|
||||||
|
void reverseStack(stack<int> &s)
|
||||||
|
{
|
||||||
|
// base case: stack is empty
|
||||||
|
if (s.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pop all items from the stack and hold them in call stack
|
||||||
|
int item = s.top();
|
||||||
|
s.pop();
|
||||||
|
reverseStack(s);
|
||||||
|
|
||||||
|
// After the recursion unfolds, insert each item from the call stack to bottom of the stack
|
||||||
|
insertInBottom(s, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
stack<int> s;
|
||||||
|
int n;
|
||||||
|
cout<<"Enter the size of stack"<<"\n";
|
||||||
|
cin>>n;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
int temp;
|
||||||
|
cout<<"enter element"<<"\n";
|
||||||
|
cin>>temp;
|
||||||
|
s.push(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
reverseStack(s);
|
||||||
|
|
||||||
|
cout << "Reversed stack is: ";
|
||||||
|
while (!s.empty()) {
|
||||||
|
cout << s.top() << ' ';
|
||||||
|
s.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*what program does (reverses the stack by using recursion)
|
||||||
|
-> original stack: 1,2,3,4,5
|
||||||
|
-> reversed stack: 5,4,3,2,1*/
|
||||||
|
|
||||||
|
// time complexity : O(n^2)
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue