Stack is a simple abstract datatype to be implemented in any language and yet it needs careful consideration during the overflowed state. Stack can be applied to any problems where there is a need to backtrack. I have created a simple python program with tk/tcl interface to show the application of the stack.
Does anyone know about Fractals ? Fractals are great way to learn recursion. Since the fractals can be created recursively, there maybe a need for explicit stack. I hate it when there’s too much terminologies. Lets look into the code.
First as you can see in the above link to fractals, there’s certain rules(pattern of recursion) to follow for a program to create a plant like structure.
Code:
http://python3.codes/drawing-fractals-with-lindenmayer-systems/ - For stack operations
# http://algorithmicbotany.org/papers/abop/abop-ch1.pdf (e) - L system algorithms
import turtle
wn = turtle.Screen()
st = []
alex = turtle.Turtle()
alex.speed(0)
#X→F[+X][-X]FX and F→FF
def ff(size,lvl): #Function F
if lvl = 1:
ff(size,lvl – 1) #call F
st.append([alex.xcor(),alex.ycor(),alex.heading()]) # push the current pos and angle
alex.left(25.7)# turn left
plant(size,lvl – 1)# call X
alex.penup()
m = st.pop()# pop the pos and angle
i,j = m[0],m[1]
alex.goto(i,j)
alex.seth(m[2])
alex.pendown()
st.append([alex.xcor(),alex.ycor(),alex.heading()]) # push the current pos and angle
alex.right(25.7) # turn right
plant(size,lvl – 1) #Call X
alex.penup()
m = st.pop() # pop the pos and angle
i,j = m[0],m[1]
alex.goto(i,j)
alex.seth(m[2])
alex.pendown()
ff(size,lvl – 1) #Call F
plant(size,lvl – 1) #Call X
if __name__ == ‘__main__’:
alex.left(90)
alex.penup()
alex.goto(0,-200)
alex.pendown()
# alex.width(1)
plant(int(input(“Enter the Size: “)), int(input(“Enter the depth: “)))
print(“Finished Processing….!”)
alex.hideturtle()
wn.exitonclick()
</code>