Abstract data type in python

By | 4th March 2019

Abstract data type in python

 

Abstract data type in python

Abstract data type in python

 

Abstraction is one of the most powerful ideas in computer science. It separates the what from the how.
Abstraction provides modularity. Classes are the Python represented: on for “Abstract Data Types,” (ADT) a very useful no: on in any programming language An ADT involves both data and operations on that data. .

Example: Stack

A stack is a LIFO (last in, first out) list with the following operations Push, Pop, Create (Init), Top, IsEmpty, Size. (Such a list is called a Signature or the Interface to the ADT.)

Stack ADT

Stack() creates a new stack that is empty.It wants no parameters ANd returns an empty stack.

push(item) adds a replacement item to the highest of the stack. It needs the item and returns nothing.

pop() removes the top item from the stack. It needs no parameters and returns the item. The stack is modified.

top() returns the highest item from the stack however doesn’t take away it. It needs no parameters. The stack is not modified.

isEmpty() tests to see whether the stack is empty. It wants no parameters and returns a Boolean price.

size() returns the amount of things on the stack. It needs no parameters and returns an integer.

Stack ADT as a Class

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 top(self):!

Example: Queue

A Queue is a FIFO (first in, first out) list with the following operations Enqueue, Dequeue, Size, Font.
Queue ADT

Queue() creates a new queue that is empty. It wants no parameters ANd returns an empty queue.

Enqueue(item) adds a replacement item to the rear of the queue. It needs the item and returns nothing.
Dequeue() removes the item from the front of the queue. It needs no parameters and returns the item. The queue is modified.
Front() returns the front item from the queue however doesn’t remove it. It needs no parameters. The queue is not modified.
isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
size() returns the amount of things on the queue. It needs no parameters and returns an integer

Queue ADT as a Class

Class Queue:!

def init (self):!
self.items = []!
def IsEmpty(self):!
return self.items == []!
def enqueue(self,item):!
self.items.append(item)!
def dequeue(self):!
return self.items.pop(0)!
def front(self):!

Rational ADT as a Class

class Rational:!

def init (self,n,d):!
if d==0:!
print ‘The denominator is ZERO, can not proceed’!
else:! g=gcd(n,d)! self.numer=n/g! self.denom=d/g!
def show(self):!
print self.numer,’/’,self.denom!
def add(self,a):!
n=self.numer*a.denom + self.denom*a.numer!
d=self.denom*a.denom!
return Rational(n,d)!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *