0% found this document useful (0 votes)
12 views5 pages

DSD Lab Manual Mine

The document provides implementations of a List Abstract Data Type (ADT) using Python arrays and linked lists. It includes a sample code for creating and manipulating an array, as well as a linked list class with methods for insertion, removal, and display of nodes. The linked list implementation allows users to perform various operations through a menu-driven interface.

Uploaded by

sharlymonica19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

DSD Lab Manual Mine

The document provides implementations of a List Abstract Data Type (ADT) using Python arrays and linked lists. It includes a sample code for creating and manipulating an array, as well as a linked list class with methods for insertion, removal, and display of nodes. The linked list implementation allows users to perform various operations through a menu-driven interface.

Uploaded by

sharlymonica19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX.

NO:3 IMPLEMENT LIST ADT USING PYTHON ARRAYS


'''3.IMPLEMENT LIST ADT USING PYTHON ARRAYS'''
import array
arr = array.array('i', [1, 2, 3])
print ("The new created array is : ",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
print("\r")
arr.append(4);
print("The appended array is : ", end="")
for i in range (0, 4):
print (arr[i], end=" ")
arr.insert(2, 5)
print("\r")
print ("The array after insertion is : ", end="")
for i in range (0, 5):
print (arr[i], end=" ")

EX.NO.4: LINKED LIST IMPLEMENTATIONS OF LIST

class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None

def get_node(self, index):


current = self.head
for i in range(index):
if current is None:
return None
current = current.next
return current

def get_prev_node(self, ref_node):


current = self.head
while (current and current.next != ref_node):
current = current.next
return current

def insert_after(self, ref_node, new_node):


new_node.next = ref_node.next
ref_node.next = new_node

def insert_before(self, ref_node, new_node):


prev_node = self.get_prev_node(ref_node)
self.insert_after(prev_node, new_node)

def insert_at_beg(self, new_node):


if self.head is None:
self.head = new_node
else:
new_node.next = self.head
self.head = new_node

def insert_at_end(self, new_node):


if self.head is None:
self.head = new_node
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_node

def remove(self, node):


prev_node = self.get_prev_node(node)
if prev_node is None:
self.head = self.head.next
else:
prev_node.next = node.next

def display(self):
current = self.head
while current:
print(current.data, end = ' ')
current = current.next

a_llist = LinkedList()
print('Menu')
print('insert <data> after <index>')
print('insert <data> before <index>')
print('insert <data> at beg')
print('insert <data> at end')
print('remove <index>')
print('quit')
while True:
print('The list: ', end = '')
a_llist.display()
print()
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'insert':
data = int(do[1])
position = do[3].strip().lower()
new_node = Node(data)
suboperation = do[2].strip().lower()
if suboperation == 'at':
if position == 'beg':
a_llist.insert_at_beg(new_node)
elif position == 'end':
a_llist.insert_at_end(new_node)
else:
index = int(position)
ref_node = a_llist.get_node(index)
if ref_node is None:
print('No such index.')
continue
if suboperation == 'after':
a_llist.insert_after(ref_node, new_node)
elif suboperation == 'before':
a_llist.insert_before(ref_node, new_node)

elif operation == 'remove':


index = int(do[1])
node = a_llist.get_node(index)
if node is None:
print('No such index.')
continue
a_llist.remove(node)
elif operation == 'quit':
break

You might also like