Created
December 14, 2023 19:17
-
-
Save pursh2002/f79c9207e22d7fc062d995b46c2ff291 to your computer and use it in GitHub Desktop.
100 days of Data Structures and Algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Linked list | |
| class Node: | |
| def __init__(self, data): | |
| self.data = data | |
| self.next = None | |
| class My_linkedlist: | |
| def __init__(self): | |
| self.head = None | |
| def append(self, data): | |
| new_node = Node(data) | |
| if self.head is None: | |
| self.head = new_node | |
| return | |
| last_node = self.head | |
| while last_node.next: | |
| last_node = last_node.next | |
| last_node.next = new_node | |
| def print_list(self): | |
| current_node = self.head | |
| while current_node: | |
| print(current_node.data) | |
| current_node = current_node.next | |
| linked_list = My_linkedlist() | |
| linked_list.append("List weekdays from the start:") | |
| linked_list.append("Monday") | |
| linked_list.append("Tuesday") | |
| linked_list.append("Wednesday") | |
| linked_list.append("Thursday") | |
| linked_list.append("Friday") | |
| linked_list.print_list() | |
| """ | |
| Output: | |
| Weekdays: | |
| Monday | |
| Tuesday | |
| Wednesday | |
| Thursday | |
| Friday | |
| """ | |
| print() | |
| print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment