0% found this document useful (0 votes)
19 views2 pages

Abstract Data Types With Python

Abstract Data Types (ADTs) define data types by their behavior, focusing on operations rather than implementation. Key characteristics include encapsulation, defined operations, and independence from data structures. The document provides Python examples for common ADTs such as List, Stack, Queue, Deque, and Priority Queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Abstract Data Types With Python

Abstract Data Types (ADTs) define data types by their behavior, focusing on operations rather than implementation. Key characteristics include encapsulation, defined operations, and independence from data structures. The document provides Python examples for common ADTs such as List, Stack, Queue, Deque, and Priority Queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Abstract Data Types (ADTs) with Python Examples

Introduction
An Abstract Data Type (ADT) is a theoretical concept in computer science used to define data types
by their behavior rather than their implementation. 'Abstract' means we focus on what operations
can be performed, not how they are implemented. ADTs provide a level of abstraction between the
user and the implementation details.

Key Characteristics
1. Encapsulation – Implementation details are hidden from the user. 2. Defined operations – ADTs
provide a clear set of operations to work with. 3. Independence – The same ADT can be
implemented using different data structures.

Types of ADTs
Below are the common ADTs with Python examples.

List ADT
class ListADT:
def __init__(self):
self.data = []

def insert(self, value):


self.data.append(value)

def delete(self, value):


if value in self.data:
self.data.remove(value)

def display(self):
return self.data

# Usage
my_list = ListADT()
my_list.insert(10)
my_list.insert(20)
my_list.delete(10)
print(my_list.display()) # Output: [20]

Stack ADT
class StackADT:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)

def pop(self):
if not self.isEmpty():
return self.stack.pop()
return "Stack is empty"

def peek(self):
if not self.isEmpty():
return self.stack[-1]
return "Stack is empty"
def isEmpty(self):
return len(self.stack) == 0

# Usage
s = StackADT()
s.push(5)
s.push(10)
print(s.pop()) # Output: 10
print(s.peek()) # Output: 5

Queue ADT
class QueueADT:
def __init__(self):
self.queue = []

def enqueue(self, item):


self.queue.append(item)

def dequeue(self):
if not self.isEmpty():
return self.queue.pop(0)
return "Queue is empty"

def isEmpty(self):
return len(self.queue) == 0

# Usage
q = QueueADT()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1

Deque ADT
from collections import deque

# Deque as ADT
dq = deque()
dq.append(10) # Right end insert
dq.appendleft(5) # Left end insert
dq.pop() # Right end remove
dq.popleft() # Left end remove

Priority Queue ADT


import heapq

# Priority Queue as ADT


pq = []
heapq.heappush(pq, (2, "Task 2"))
heapq.heappush(pq, (1, "Task 1"))
heapq.heappush(pq, (3, "Task 3"))

while pq:
print(heapq.heappop(pq)) # Output in priority order

You might also like