Singly Final

 avatar
shinta0x01
python
7 months ago
3.7 kB
2
Indexable
Never
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Singly:
    def __init__(self):
        self.head = None
    def insert_begin(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
        else:
            new_node.next = self.head
            self.head = new_node
    def insert_end(self, data):
        new_node = Node(data)
        current = self.head
        if self.head is None:
            self.head = new_node
        else:
            while current.next != None:
                current = current.next
            current.next = new_node
    def insert_before(self, key, data):
        new_node = Node(data)
        current = self.head
        if self.head is None:
            print('List is Empty')
            return
        if current.data == key:
            self.head = new_node
            new_node.next = current
            return
        while current.data != key:
            pre_current = current
            current = current.next
            if current.data != key and current.next is None:
                print('Key not found!')
                return
        pre_current.next = new_node
        new_node.next = current
    def insert_after(self, key, data):
        new_node = Node(data)
        current = self.head
        if self.head is None:
            print('List is Empty')
            return
        if current.data == key:
            self.head.next = new_node
            post_current = current.next
            new_node.next = post_current
            return
        while current.data != key:
            current = current.next
            post_current = current.next
            if current.data != key and current.next is None:
                print('Key not found!')
                return
        current.next = new_node
        new_node.next = post_current

    def del_begin(self):
        current = self.head
        if self.head is None:
            print('List is empty')
        else:
            current = current.next
            self.head = current

    def del_end(self):
        current = self.head
        if self.head is None:
            print('List is empty')
        else:
            while current.next != None:
                pre_current = current
                current = current.next
            pre_current.next = None

    def del_given(self, key):
        current = self.head
        if self.head is None:
            print('List is empty')
            return
        if current.data == key:
            self.head = current.next
            return
        while current.data != key:
            pre_current = current
            current = current.next
            if current.data != key and current.next is None:
                print('Key not found!')
                return
        pre_current.next = current.next

    def display(self):
        current = self.head
        if self.head is None:
            return
        while current != None:
            print(current.data, '', end='')
            current = current.next

s = Singly()

s.insert_begin(10)
s.insert_begin(4)
s.insert_begin(6)
s.insert_begin(9)

#9 6 4 10
s.display()
print('\nInsert end')
s.insert_end(11)
s.display()
print('\nInsert new node before a given node')
s.insert_before(9, 33)
s.display()
print('\nInsert new node after a given node')
s.insert_after(9, 12)
s.display()
print('\nDelete start node')
s.del_begin()
s.display()
print('\nDelete end node')
s.del_end()
s.display()
print('\nDelete given node')
s.del_given(933)
s.display()
Leave a Comment