class Node: def __init__(self,value): self.value = value self.next = None class LinkedList: def __init__(self): self.head = None def add_front(self,value): new_node = Node(value) new_node.next = self.head self.head = new_node def add_back(self,value): new_node = Node(value) if self.head is None: self.head = new_node else: current = self.head while current.next is not None: current = current.next current.next = new_node def print_list(self): current = self.head while current is not None: print(current.value) current = current.next list1 = LinkedList() list1.add_front(1) list1.add_front(2) list1.add_back(3) list1.print_list()
1. Node Class:
2. LinkedList Class:
3. add_front Method:
4. add_back Method:
5. print_list Method:
6. Usage Example:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3