#include <bits/stdc++.h> using namespace std; // Stack implementation => LIFO which stands for Last-in-first-out // Reverse a string using stack const int MAX_SIZE = 10000; class Stack { private: string array[MAX_SIZE]; int top; public: Stack() { top = 0; } //Pushing (storing) an element on the stack. void push(int x) { array[top] = x; top++; } // Removing (accessing) an element from the stack. string pop() { top--; return array[top]; } // get the top data element of the stack, without removing it. string peek() { return array[top]; } // check if stack is empty. bool isEmpty() { if (top == 0) return true; else return false; } }; //reverse a string using stack int main() { Stack s; char str[]="Stack"; int len = strlen(str),i; for(i=0;i<len;i++) s.push(str[i]); for(i=0;i<len;i++) cout << s.pop(); return 0; }
Monday, 19 December 2016
Reversing a String using Stack
Labels:
Data Structure
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment