Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Chapter04/append_at_any_location_singly_linkedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
61 changes: 30 additions & 31 deletions Chapter05/Stack_using_array.py
Original file line number Diff line number Diff line change
@@ -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])