Created
February 5, 2012 06:46
-
-
Save antsmartian/1743573 to your computer and use it in GitHub Desktop.
Reversing a string or a number using Stack Data Structure
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
/* | |
These two inputs input and input2 are passed via command line | |
*/ | |
//input as a String for reversal! | |
input2 = args[1] | |
//input for the size of Stack! | |
input = args[0] as Integer | |
top = -1 | |
stack = new String[input] | |
boolean full = false | |
def push(String j) | |
{ | |
stack.putAt(++top,j) | |
} | |
def pop() | |
{ | |
stack.getAt(top--) | |
} | |
boolean isEmpty() | |
{ | |
top == -1 | |
} | |
boolean isFull() | |
{ | |
top == input -1 | |
} | |
input2.each { elements -> | |
if(isFull()) | |
{ | |
if(!full) | |
println "Sorry the stack is full" | |
full = true | |
} | |
else | |
{ | |
if(elements != ' ') | |
push elements | |
} | |
} | |
if(full) | |
{ | |
println "As you have crossed the limit of Stack size, we are only able to add these into the Stack --> ${stack.join()}" | |
} | |
output = [] | |
(top..0).each { removeIndex -> | |
output << stack[removeIndex] | |
} | |
//doing some processing to the list so that it wont show null! Thanks to fluent Api's. . . . | |
println "The given word is ${stack.findAll{it != null}.join()}, the Reversed Word is ${output.join()} " | |
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
/* | |
These two inputs input and input2 are passed via command line | |
*/ | |
//input as a String for reversal! | |
input2 = args[1] | |
//input for the size of Stack! | |
input = args[0] as Integer | |
top = -1 | |
stack = new String[input] | |
boolean full = false | |
def push(String j) | |
{ | |
stack.putAt(++top,j) | |
} | |
def pop() | |
{ | |
stack.getAt(top--) | |
} | |
boolean isEmpty() | |
{ | |
top == -1 | |
} | |
boolean isFull() | |
{ | |
top == input -1 | |
} | |
input2.each { elements -> | |
if(isFull()) | |
{ | |
if(!full) | |
println "Sorry the stack is full" | |
full = true | |
} | |
else | |
{ | |
if(elements != ' ') | |
push elements | |
} | |
} | |
if(full) | |
{ | |
println "As you have crossed the limit of Stack size, we are only able to add these into the Stack --> ${stack.join()}" | |
} | |
output = [] | |
(top..0).each { removeIndex -> | |
output << stack[removeIndex] | |
} | |
//doing some processing to the list so that it wont show null! Thanks to fluent Api's. . . . | |
println "The given word is ${stack.findAll{it != null}.join()}, the Reversed Word is ${output.join()} " | |
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
/* | |
These two inputs input and input2 are passed via command line | |
*/ | |
//input as a String for reversal! | |
input2 = args[1] | |
//input for the size of Stack! | |
input = args[0] as Integer | |
top = -1 | |
stack = new String[input] | |
boolean full = false | |
def push(String j) | |
{ | |
stack.putAt(++top,j) | |
} | |
def pop() | |
{ | |
stack.getAt(top--) | |
} | |
boolean isEmpty() | |
{ | |
top == -1 | |
} | |
boolean isFull() | |
{ | |
top == input -1 | |
} | |
input2.each { elements -> | |
if(isFull()) | |
{ | |
if(!full) | |
println "Sorry the stack is full" | |
full = true | |
} | |
else | |
{ | |
if(elements != ' ') | |
push elements | |
} | |
} | |
if(full) | |
{ | |
println "As you have crossed the limit of Stack size, we are only able to add these into the Stack --> ${stack.join()}" | |
} | |
output = [] | |
(top..0).each { removeIndex -> | |
output << stack[removeIndex] | |
} | |
//doing some processing to the list so that it wont show null! Thanks to fluent Api's. . . . | |
println "The given word is ${stack.findAll{it != null}.join()}, the Reversed Word is ${output.join()} " | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment