diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index c2e1f2f..fa8d1b7 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -28,19 +28,19 @@ def append_at_a_location(self, data, index): node = Node(data) count = 1 while current: - if count == 1: + if index == 1: node.next = current self.head = node print(count) return - elif index == index: + elif count == index: node.next = current prev.next = node return count += 1 prev = current current = current.next - if count < index: + if count <= index: print("The list has less number of elements") diff --git a/Chapter05/Stack_using_array.py b/Chapter05/Stack_using_array.py index 20ad138..b76997f 100644 --- a/Chapter05/Stack_using_array.py +++ b/Chapter05/Stack_using_array.py @@ -1,51 +1,50 @@ 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: + item = data[top] + data[top] = 0 + top = top - 1 + return item 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]) print(data[0 : top + 1]) +print(pop()) +print(pop()) +print(pop()) pop() -pop() -pop() -pop() -print(data[0:top+1]) +print(data[0 : top + 1]) peek() -print(data[0:top+1]) +print(data[0 : top + 1])