You are on page 1of 3

Group Members:

Michael Padin,
Allen Alvin Alcantara

ALBUMS – DOUBLY LINKED LIST


""" Names:
Michael G. Padin,
Allen Alvin Alcantara
"""

# ALBUMS - DOUBLE LINKED LIST IMPLEMENTATION"""

# A linked list node


class Node:

# Constructor to create a new node


def __init__(self, data):
self.data = data
self.next = None
self.prev = None

# Class to create a Doubly Linked List


class Albums:

# Constructor for empty Doubly Linked List


def __init__(self):
self.head = None
self.size = 0

# Given a reference to the head of a list and an


# integer, inserts a new node on the front of list
def add_image(self, new_img):

# 1. instantiate node
# 2. Put the data in it
new_node = Node(new_img)

# 3. Make next of new node as head and


# previous as None (already None)
new_node.next = self.head

# 4. change prev of head node to new_node


if self.head is not None:
self.head.prev = new_node

# 5. move the head to point to the new node


self.head = new_node
self.size += 1

# function to display images on a given node starting point


def display_images(self, node):

print('-----IMAGES IN ALBUMS------\n')

print('forward direction')
print('[', end='')
while node:
print(f'{node.data}', end='')
last = node
node = node.next
if node is not None:
print(' --> ', end='') # ignore print only
print(']\n')
print('reverse direction')
print('[', end='')
while last is not None:
print(f'{last.data}', end='')
last = last.prev
if last is not None:
print(' --> ', end='') # ignore print only
print(']\n')

# delete image on a given image name on the parameter


def delete_image(self, img_name):

# Store head node


temp = self.head

# If head node itself holds the image_name to be deleted


if temp is not None:
if temp.data == img_name:
print(f'removing {img_name} in the album')
self.head = temp.next
self.size -= 1
temp = None
return

# Search for the img_name to be deleted, keep track of the


# previous node as we need to change 'prev.next'
while temp is not None:
if temp.data == img_name:
break
prev = temp
temp = temp.next

# if img_name is not present on the album


if temp is None:
print(f" can't find {img_name} on the album. check your image name...")
return

# Unlink the node from linked list


print(f'removing {img_name} in the albums')
prev.next = temp.next
self.size -= 1
temp = None

# function to check if there are images contains on the album linkedlist


def is_album_empty(self):
return self.head is None

def num_of_images(self):
return self.size

if __name__ == "__main__":
choice = None

pictures = Albums()

while choice != 0:
print("\n\n=============================================")
print('Albums - Doubly Linked List Implementation\n')
print('0. exit \t\t2. Delete Image \t4. total number of images\n1. Add Image
\t3. Display Images')
print("=============================================\n")

choice = int(input('Enter number for your choice: '))

# Close program
if choice == 0:
print('closing program...')

# Add Image
elif choice == 1:
numOfImages = int(input('how many images you want to add? '))

for i in range(1, numOfImages + 1):


nameofImg = str(input(f'Enter name for image {i}: '))
pictures.add_image(nameofImg)
print(f'\n=========================================\nImages in your
Albums:\n')
pictures.display_images(pictures.head)
choice = None

# Delete Image
elif choice == 2:
# check if there are images in album
if pictures.is_album_empty():
print('Albums is empty. Please add an image')
else:
pictures.display_images(pictures.head)
imgName = str(input('Enter Image name to delete: '))
pictures.delete_image(imgName)

# Display Images
elif choice == 3:
if pictures.is_album_empty():
print('Albums is empty. Please add an image')
else:
pictures.display_images(pictures.head)
elif choice == 4:
print(f'total number of images on the albums: {pictures.num_of_images()}')

else:
print('Invalid input! please try again....\n')

OUTPUT

You might also like