Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Created February 5, 2012 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/1743573 to your computer and use it in GitHub Desktop.
Save antsmartian/1743573 to your computer and use it in GitHub Desktop.
Reversing a string or a number using Stack Data Structure
/*
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()} "
/*
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()} "
/*
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