1st Implementation: The following stack implementation assumes that the end of the list will hold the top element of the stack. As the stack grows, new items will be added on the end of the list.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
2nd Implementation : The second implementation assumes that the beginning of the list holds the top element of the stack and new items are added at the index 0.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.insert(0,item)
def pop(self):
return self.items.pop(0)
def peek(self):
return self.items[0]
def size(self):
return len(self.items)
Being a beginner to Data Structures, I would like to know:
1. Which implementation is more efficient with respect to time or space, and why ?
2. Is the time complexity of the insert(0) in the 2nd implementation O(n). If yes, how ?
self.items[-1]gives you the last element with less faff.peekmethod. And call it tos, maybe (Top Of Stack)