Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

linked lists python

"""
linked list

"""

#creating a class of node and creating contents
class Node:
    def __init__(self, contents, next_node=None):
        self.contents = contents
        self.next = next_node

    def __repr__(self):
        return f"This node contains {str(self.contents)}."


# creating the linked list class
class LinkedList:
    #creates data as an empty array
    def __init__(self, data=[]):
        self.head = None
        if data:
            self._init_from_data(data)
    #creating data for the linked list
    def _init_from_data(self, data):
        previous_node = None
        for datum in data:
            new_node = Node(datum)
            if not self.head:
                self.head = new_node
            else:
                previous_node.next = new_node
            previous_node = new_node
    #
    def _get_tail(self):
        current_node = self.head
        while current_node and current_node.next:
            current_node = current_node.next
        return current_node

    def append_left(self, datum):
        new_node = Node(datum, self.head)
        self.head = new_node

    def append_right(self, datum):
        if self.head:
            tail_node = self._get_tail()
            tail_node.next = Node(datum)
        else:
            self.head = Node(datum)

    def pop_left(self):
        old_head = self.head
        new_head = old_head.next
        self.head = new_head
        return old_head


    def pop_right(self):
        previous_node = None
        current_node = self.head
        while current_node and current_node.next:
            previous_node = current_node
            current_node = current_node.next
        if previous_node:
            previous_node.next = None
        else:
            self.head = None
        return current_node

    def __repr__(self):
        current_node = self.head
        nodes_contents = []
        while current_node:
            nodes_contents.append(current_node.contents)
            current_node = current_node.next
        nodes_contents.append("None")
        return " >>> ".join(nodes_contents)

    def reverse(self):
        if(self.head == None):
            return
        else:
            node = self.head
            previous_node = None
            # Step 1 : Reverse Links;
            while(node != None):
                temp = node.next
                node.next = previous_node
                previous_node = node
                node = temp
                # Step 2 : Change Head;
                if(temp == None):
                    self.head = previous_node
            return self.head


empty_list = LinkedList()

linked_list = LinkedList(["a","b","c"])
linked_list.append_left('aa')
linked_list.append_right('cc')
print(linked_list)

linked_list.pop_right()
print(linked_list)

linked_list.pop_left()
print(linked_list)

linked_list.reverse()
print(linked_list)
 
PREVIOUS NEXT
Tagged: #linked #lists #python
ADD COMMENT
Topic
Name
5+3 =