Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.8 kB
5
Indexable
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class LinkedList:
    def __init__(self, data):
        self.head = Node(data)

    def insert(self, data, index=-1):
        cur = self.head
        curr_index = 0

        if index == 0:
            new_node = Node(data)
            new_node.next = cur
            self.head = new_node
            return

        while cur.next is not None and (index == -1 or curr_index < index-1):
            cur = cur.next
            curr_index += 1
        new_node = Node(data)
        new_node.next = cur.next
        cur.next = new_node

    def search(self, x):
        cur = self.head
        index = 0
        while cur is not None:

            if cur.data == x:
                return index
            cur = cur.next
            index += 1
        return -1

    def print(self):
        cur = self.head
        while cur is not None:
            print(cur.data)
            cur = cur.next

    def reverse(self):
        # just making next of current element to prev element
        cur = self.head
        prev = None
        while cur is not None:
            temp = cur.next
            cur.next = prev
            prev = cur
            cur = temp
            if temp is None:
                self.head = prev

    def delete(self, index=-1):
        cur = self.head
        cur_index = 0
        if index == 0:
            self.head = self.head.next
            return
        while cur.next.next is not None and (cur_index < index-1 or index == -1):
            cur = cur.next
            cur_index += 1


        cur.next = cur.next.next



    def sort(self):
        pass


head = LinkedList(1)
head.insert(2)
head.insert(3)
head.insert(4,1)
head.print()
head.reverse()
head.print()
print(head.search(6))
head.delete(2)
head.print()

#################################
#
# class Tree:
#
#     def __init__(self, data):
#         self.data = data
#         self.left = None
#         self.right = None
#
#     def insert(self, data):
#
#         if data > self.data:
#             if self.right is None:
#                 self.right = Tree(data)
#             else:
#                 # it will recursively adjust itself
#                 self.right.insert(data)
#         elif data < self.data:
#             if self.left is None:
#                 self.left = Tree(data)
#             else:
#                 self.left.insert(data)
#
#     def dfs_recursive_trav(self):
#         print(self.data)
#         if self.left:
#             self.left.dfs_recursive_trav()
#         if self.right:
#             self.right.dfs_recursive_trav()
#
#     def dfs_iterative_trav(self):
#         # we will use stack as stack represent recursions
#         stack = [self]
#         while len(stack):
#             x = stack.pop(-1)
#             if x:
#                 print(x.data)
#                 stack.append(x.right)
#                 stack.append(x.left)
#
#     def bfs_iter_trav(self):
#         queue = [self]
#         while len(queue):
#             temp = len(queue)
#             for x in range(temp):
#                 current = queue.pop(0)
#                 print(current.data)
#                 if current.left:
#                     queue.append(current.left)
#                 if current.right:
#                     queue.append(current.right)
#
#
# root = Tree(5)
# root.insert(12)
# root.insert(7)
# root.insert(18)
# root.insert(69)
# root.insert(4)
# root.insert(13)
#
#
#
# root.dfs_recursive_trav()
# print('-----------------')
# root.dfs_iterative_trav()
# print('-----------------')
# root.bfs_iter_trav()
#
#
#