diff --git a/Chapter05/Stack_using_array.py b/Chapter05/Stack_using_array.py index 20ad138..f1c7f94 100644 --- a/Chapter05/Stack_using_array.py +++ b/Chapter05/Stack_using_array.py @@ -1,41 +1,40 @@ size = 3 -data = [0]*(size) #Initialize the stack +data = [0] * (size) # Initialize the stack top = -1 + def push(x): - global top - if top >= size-1: - print("Stack Overflow") - else: - top = top + 1 - data[top] = x + global top + if top >= size - 1: + print("Stack Overflow") + else: + top = top + 1 + data[top] = x def pop(): - global top - if top == -1: - print("Stack Underflow") - else: - top = top-1 - data[top] = 0 - return data[top+1] + global top + if top == -1: + print("Stack Underflow") + else: + value = data[top] + data[top] = 0 + top = top - 1 + return value def peek(): - global top - if top == -1: - print("Stack is empty") - else: - print(data[top]) - - + if top == -1: + print("Stack is empty") + else: + print(data[top]) -push('egg') -push('ham') -push('spam') -push('new') -push('new2') +push("egg") +push("ham") +push("spam") +push("new") +push("new2") print(data[0 : top + 1]) @@ -44,8 +43,8 @@ def peek(): pop() pop() pop() -print(data[0:top+1]) +print(data[0 : top + 1]) peek() -print(data[0:top+1]) +print(data[0 : top + 1])