0% found this document useful (0 votes)
14 views10 pages

Python Data Structures Lab Manual

The document is a lab manual for a Diploma in Computer Engineering course focusing on Data Structures using Python. It includes exercises for implementing various data structures such as lists, singly linked lists, stacks, queues, binary trees, and sorting/searching algorithms, along with their respective Python programs and procedures. Each exercise concludes with a statement confirming successful execution of the programs.

Uploaded by

Raji Ramasamy
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)
14 views10 pages

Python Data Structures Lab Manual

The document is a lab manual for a Diploma in Computer Engineering course focusing on Data Structures using Python. It includes exercises for implementing various data structures such as lists, singly linked lists, stacks, queues, binary trees, and sorting/searching algorithms, along with their respective Python programs and procedures. Each exercise concludes with a statement confirming successful execution of the programs.

Uploaded by

Raji Ramasamy
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

DIPLOMA IN COMPUTER NGINEERING

II YR/IV SEM

1052234230 - DATA STRUCTURES USING PYTHON

LAB MANUAL
Ex. No. 1 Write a program to implement any one python data structure with the
following operations
A) Create B) Add elements C) Access elements D) Remove elements

Aim:
To write a program to implement python data structure with basic operations.

Procedure:
1. Initialize list with elements.
2. Add elements to the list.
3. Access an element in the list by index.
4. Remove an element from the list.

Program:

# [Link] list
my_list = [116,117,118,119,120]
print("Instial List:",my_list)

#[Link] Element
my_list.append(121)
print("After Adding 121:", my_list)

#Adding an Element at a specific index


my_list.insert (2, 200)
print ("After inserting 200 at index 2:",my_list)

#[Link] Element
print ("Element at index 2:",my_list[2])

# [Link] Element
print("Last element:",my_list[-1])

# [Link] Element
my_list.remove(200)
print("After remove 10:",my_list)

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No.2. Write a python program to implement a singly linked list a) create a singly
linked list b) add element to singly linked list c) Remove element from singly linked list

Aim:
To Write a python program to implement singly linked list.

Procedure:
1. Create a singly linked list: Initialize an empty linked list.
2. Add element to singly linked list: Add an element to the end of the linked list.
3. Remove element from singly linked list: Remove an element from the linked list.

Progam:
class Node:
def init (self, data):
[Link] = data
[Link] = None
class SinglyLinkedList:
def init (self):
[Link] = None
def create_list(self, data):
new_node = Node(data)
[Link] = new_node
print(f"List created with the element: {data}")
def add_element(self, data):
new_node = Node(data)
if [Link] is None:
[Link] = new_node
else:
last_node = [Link]
while last_node.next:
last_node = last_node.next
last_node.next = new_node
print(f"Element {data} added to the list.")
def remove_element(self, data):
if [Link] is None:
print("Error: The list is empty!")
return
if [Link] == data:
[Link] = [Link]
print(f"Element {data} removed from the list.")
return

current_node = [Link]
while current_node.next and current_node.[Link] != data:
current_node = current_node.next

if current_node.next is None:
print(f"Error: Element {data} not found in the list.")
else:
current_node.next = current_node.[Link]
print(f"Element {data} removed from the list.")

def display(self):
if [Link] is None:
print("The list is empty.")
return

current_node = [Link]
print("Elements in the list:")
while current_node:
print(current_node.data, end=" -> ")
current_node = current_node.next
print("None")

if name == " main ":


linked_list = SinglyLinkedList()
linked_list.create_list(10)
linked_list.add_element(20)
linked_list.add_element(30)
linked_list.add_element(40)
linked_list.display()
linked_list.remove_element(20)
linked_list.display()
linked_list.remove_element(100)
linked_list.remove_element(10)
linked_list.display()

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 3. Write a python program to implement stack

Aim:

Simple Python program that implements a stack with basic operations

Procedure:

1. Push: Add an element to the stack.


2. Pop: Remove the top element from the stack.
3. Peek: View the top element of the stack.
4. IsEmpty: Check if the stack is empty.

Program:

class Stack:
def init (self):
[Link] = []
def push(self, item):
[Link](item)
print(f"{item} pushed to stack")
def pop(self):
if not self.is_empty():
popped_item = [Link]()
print(f"{popped_item} popped from stack")
else:
print("Stack is empty, cannot pop")
def is_empty(self):
return len([Link]) == 0

if name == " main ":


stack = Stack()
[Link](10)
[Link](20)
[Link](30)
[Link]()
[Link]()

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 4 Write a python program to implement queue

Aim:
Simple Python program to implement queue with basic operations.

Procedure:
Enqueue: Add an element to the end of the queue.
Dequeue: Remove the front element from the queue.
IsEmpty: Check if the queue is empty.

Program:

class Queue:
def init (self):
[Link] = []
def enqueue(self, item):
[Link](item)
print(f"{item} enqueued to queue")
def dequeue(self):
if not self.is_empty():
dequeued_item = [Link](0)
print(f"{dequeued_item} dequeued from queue")
else:
print("Queue is empty, cannot dequeue")
def is_empty(self):
"""Check if the queue is empty."""
return len([Link]) == 0
if name == " main ":
queue = Queue()
[Link](10)
[Link](20)
[Link](30)
[Link]()
[Link]()
[Link]()
[Link]()

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 5 Write the python program for pre-order traversal of a binary tree

Aim:
To write the python program for pre-order traversal of a binary tree.

Procedure:
1. Start.
2. If the root is empty, return.
3. Traverse the root node. //print value at node
4. Traverse left subtree of the root.// preorder([Link])
5. Traverse the right subtree of the root.// preorder([Link])
6. End.

Program:
class BinaryTreeNode:
def init (self, data):
[Link] = data
[Link] = None
[Link]=None
def insert(root,newValue):
if root is None:
root=BinaryTreeNode(newValue)
return root
if newValue<[Link]:
[Link]=insert([Link],newValue)
else:
[Link]=insert([Link],newValue)
return root
def preorder(root):
if root==None:
return
print([Link])
preorder([Link])
preorder([Link])
root= insert(None,15)
insert(root,10)
insert(root,25)
insert(root,6)
insert(root,14)
insert(root,20)
insert(root,60)
print("Printing values of binary tree in preorder Traversal.")
preorder(root)
Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 6. Write a python program to implement bubble sort

Aim:
To write a python program to implement bubble sort

Procedure:
 Compare the first and second elements of the list.
 If the first element is greater than the second, swap them.
 Continue comparing and swapping adjacent elements throughout the list.
 After each pass, the largest element "bubbles up" to its correct position.
 Repeat this process for all elements until no swaps are needed (i.e., the list is sorted).

Program:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
if name == " main ":
arr = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", arr)
sorted_arr = bubble_sort(arr)
print("Sorted array:", sorted_arr)

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 7 Write a python program to implement linear search

Aim:
To write a python program to implement linear search

Procedure:
1. Start from the first element and compare it with the target element.
2. If the element matches the target, return its index.
3. If the element does not match, move to the next element.
4. If the element is not found by the time the end of the list is reached, return -1 to
indicate that the element is not present in the list
Program:
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
if name == " main ":
arr = [10, 20, 30, 40, 50]
target = 30
result = linear_search(arr, target)

if result != -1:
print(f"Element {target} found at index {result}.")
else:
print(f"Element {target} not found in the list.")

Output:

Result:
Thus tha above program was created and executed sucessfully.
Ex. No. 8 Write a python program to implement binary search

Aim:
To write a python program to implement binary search

Procedure:

1. Begin with the middle element of the list.


2. If the target element is equal to the middle element, the search is complete.
3. If the target is less than the middle element, repeat the search on the left half of the
list.
4. If the target is greater than the middle element, repeat the search on the right half of
the list.
5. If the search interval becomes empty, the target is not present in the list.

Program:
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
right = mid - 1
else:
left = mid + 1
return -1
if name == " main ":
arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
target = 30
result = binary_search(arr, target)
if result != -1:
print(f"Element {target} found at index {result}.")
else:
print(f"Element {target} not found in the list.")

Output:

Result:
Thus tha above program was created and executed sucessfully.

You might also like