0% found this document useful (0 votes)
3 views

DS Lab Assignment2

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS Lab Assignment2

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

Write a program to implement doubly linked list as an ADT that


supports the following operations:
i. Insert an element x at the beginning of the doubly linked list
ii. Insert an element x at the end of the doubly linked list
iii. Remove an element from the beginning of the doubly linked list
iv. Remove an element from the end of the doubly linked list

CODE
class Node:
"""A class to represent a node in a doubly linked list."""
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

class DoublyLinkedList:
"""A class to represent a doubly linked list."""
def __init__(self):
self.head = None
self.tail = None

def insert_at_beginning(self, x):


"""Insert an element at the beginning of the doubly linked
list."""
new_node = Node(x)
if self.head is None: # List is empty
self.head = self.tail = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
print(f"Inserted {x} at the beginning.")

def insert_at_end(self, x):


"""Insert an element at the end of the doubly linked list."""
new_node = Node(x)
if self.tail is None: # List is empty
self.head = self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
print(f"Inserted {x} at the end.")

def remove_from_beginning(self):
"""Remove an element from the beginning of the doubly linked
list."""
if self.head is None: # List is empty
print("List is empty! Nothing to remove.")
return

removed_data = self.head.data
if self.head == self.tail: # Single element in the list
self.head = self.tail = None
else:
self.head = self.head.next
self.head.prev = None
print(f"Removed {removed_data} from the beginning.")

def remove_from_end(self):
"""Remove an element from the end of the doubly linked list."""
if self.tail is None: # List is empty
print("List is empty! Nothing to remove.")
return

removed_data = self.tail.data
if self.head == self.tail: # Single element in the list
self.head = self.tail = None
else:
self.tail = self.tail.prev
self.tail.next = None
print(f"Removed {removed_data} from the end.")

def display(self):
"""Display the elements of the doubly linked list."""
if self.head is None:
print("The list is empty.")
return

elements = []
current = self.head
while current is not None:
elements.append(current.data)
current = current.next
print("List:", " <-> ".join(map(str, elements)))

# Example usage of the doubly linked list


if __name__ == "__main__":
dll = DoublyLinkedList()
dll.insert_at_beginning(10)
dll.insert_at_beginning(5)
dll.insert_at_end(20)
dll.insert_at_end(30)
dll.display()
dll.remove_from_beginning()
dll.display()
dll.remove_from_end()
dll.display()

OUTPUT

You might also like