ACTIVITY-7
DESIGN AND IMPLEMENT SIMPLE
APPLICATION THAT REQUIRE DLL DATA
STRUCTURE
1. Applications of Doubly Linked List:
It is used by web browsers for backward and forward navigation of web pages
LRU (Least Recently Used) / MRU (Most Recently Used) Cache are
constructed using Doubly Linked Lists.
Used by various applications to maintain undo and redo functionalities.
In Operating Systems, a doubly linked list is maintained by thread scheduler to
keep track of processes that are being executed at that time.
2.Advantages of Doubly Linked List over the singly linked list:
A DLL can be traversed in both forward and backward directions.
The delete operation in DLL is more efficient if a pointer to the node to be
deleted is given.
We can quickly insert a new node before a given node.
In a singly linked list, to delete a node, a pointer to the previous node is needed.
To get this previous node, sometimes the list is traversed. In DLL, we can get
the previous node using the previous pointer.
3.Disadvantages of Doubly Linked List over the singly linked list:
Every node of DLL Requires extra space for a previous pointer. It is possible to
implement DLL with a single pointer though (See this and this).
All operations require an extra pointer previous to be maintained. For example,
in insertion, we need to modify previous pointers together with the next
pointers. For example, in the following functions for insertions at different
positions, we need 1 or 2 extra steps to set the previous pointer.
here is a simple Python application that utilizes a DLL (Doubly Linked List)
data structure. In this example, we will implement a basic doubly linked list and
demonstrate its usage by adding, removing, and printing elements.
CODE:
class Node:
def _init_(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def _init_(self):
self.head = None
self.tail = None
def isEmpty(self):
return self.head is None
def append(self, data):
new_node = Node(data)
if self.isEmpty():
self.head = self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
def insert(self, data, after=None):
new_node = Node(data)
if self.isEmpty():
self.head = self.tail = new_node
elif after is None:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
else:
current = self.head
while current:
if current == after:
new_node.next = current.next
if current.next:
current.next.prev = new_node
new_node.prev = current
current.next = new_node
break
current = current.next
def remove(self, data):
if self.isEmpty():
return
current = self.head
while current:
if current.data == data:
if current == self.head:
self.head = current.next
if self.head:
self.head.prev = None
elif current == self.tail:
self.tail = current.prev
if self.tail:
self.tail.next = None
else:
current.prev.next = current.next
current.next.prev = current.prev
return
current = current.next
def print_list(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
class MusicPlaylist:
def _init_(self):
self.playlist = DoublyLinkedList()
def add_song(self, song_title):
self.playlist.append(song_title)
def insert_song(self, song_title, after_song):
self.playlist.insert(song_title, after=after_song)
def remove_song(self, song_title):
self.playlist.remove(song_title)
def print_playlist(self):
print("Playlist:")
self.playlist.print_list()
# Create a music playlist
playlist = MusicPlaylist()
# Add some songs
playlist.add_song("Bohemian Rhapsody")
playlist.add_song("Stairway to Heaven")
playlist.add_song("Hotel California")
# Insert a song after "Bohemian Rhapsody"
playlist.insert_song("Imagine", after_song="Bohemian Rhapsody")
# Print the playlist
playlist.print_playlist()
# Remove a song
playlist.remove_song("Stairway to Heaven")
# Print the updated playlist
playlist.print_playlist()