Professional Documents
Culture Documents
CD3291 Dsa
CD3291 Dsa
Unit-1
Prepared By
R.J.Alice Nineta
AP/IT
STACK ADT
ADT-Abstract Datatype
➔ Mathematical expression that describes how the set
of operation is implemented
➔It is an extension of modular design.
1.push-->insert
2.pop---> delete
➔ Stack is also known as LIFO(Last in First OUT)
TOP:
It is a pointer which keep track of the top
element in the stack.
●returnself.size() == self.capacity
Push:
def push(self, x):
ifself.isFull():
print("Stack Overflow!! Calling exit()…")
exit(1)
print("Inserting", x, "into the stack…")
self.top = self.top + 1
self.arr[self.top] = x
Pop:
def pop(self):
# check for stack underflow
ifself.isEmpty():
print("Stack Underflow!! Calling exit()…")
exit(1)
print("Removing", self.peek(), "from the stack")
#decrease stack size by 1 and (optionally)
return the popped element
top = self.arr[self.top]
self.top = self.top - 1
return top