forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverses the given string using Stack Data Structure. This solves rathoresrikant#576
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
//to represent a stack | ||
class Stack | ||
{ | ||
public: | ||
int top; | ||
unsigned capacity; | ||
char* array; | ||
}; | ||
|
||
// function to create a stack of given | ||
// capacity. It initializes size of stack as 0 | ||
Stack* createStack(unsigned capacity) | ||
{ | ||
Stack* stack = new Stack(); | ||
stack->capacity = capacity; | ||
stack->top = -1; | ||
stack->array = new char[(stack->capacity * sizeof(char))]; | ||
return stack; | ||
} | ||
|
||
// Stack is full when top is equal to the last index | ||
int isFull(Stack* stack) | ||
{ return stack->top == stack->capacity - 1; } | ||
|
||
// Stack is empty when top is equal to -1 | ||
int isEmpty(Stack* stack) | ||
{ return stack->top == -1; } | ||
|
||
// Function to add an item to stack. | ||
// It increases top by 1 | ||
void push(Stack* stack, char item) | ||
{ | ||
if (isFull(stack)) | ||
return; | ||
stack->array[++stack->top] = item; | ||
} | ||
|
||
// Function to remove an item from stack. | ||
// It decreases top by 1 | ||
char pop(Stack* stack) | ||
{ | ||
if (isEmpty(stack)) | ||
return -1; | ||
return stack->array[stack->top--]; | ||
} | ||
|
||
// A stack based function to reverse a string | ||
void reverse(char str[]) | ||
{ | ||
// Create a stack of capacity | ||
//equal to length of string | ||
int n = strlen(str); | ||
Stack* stack = createStack(n); | ||
|
||
// Push all characters of string to stack | ||
int i; | ||
for (i = 0; i < n; i++) | ||
push(stack, str[i]); | ||
|
||
// Pop all characters of string and | ||
// put them back to str | ||
for (i = 0; i < n; i++) | ||
str[i] = pop(stack); | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
char str[] = "Reverse Me"; | ||
|
||
reverse(str); | ||
cout << "Reversed string is " << str; | ||
|
||
return 0; | ||
} |