singly linked list in python

Solutions on MaxInterview for singly linked list in python by the best coders in the world

showing results for - "singly linked list in python"
Sara
05 Jan 2019
1class Node:
2    def __init__(self, data, next1):
3        self.data = data
4        self.next = next1
5
6
7class Linkedlist:
8    def __init__(self):
9        self.head = None
10        self.size = 0
11
12    def length(self):
13        return self.size
14
15    def is_empty(self):
16        return self.size == 0
17
18    def insert_at_the_beginning(self, data):
19        self.insert_with_index(0, data)
20
21    def insert_at_the_ending(self, data):
22        self.insert_with_index(self.size, data)
23
24    def insert_with_index(self, index, data):
25        if index > self.size or index < 0:
26            print("check given", index, "index value and enter again")
27            return False
28        if index == 0:
29            self.head = Node(data, self.head)
30        else:
31            current = self.head
32            for i in range(index - 1):
33                current = current.next
34            current.next = Node(data, current.next)
35        self.size += 1
36
37    def peek_top(self):
38        return self.peek_index(0)
39
40    def peek_bottom(self):
41        return self.peek_index(self.size - 1)
42
43    def peek_index(self, index):
44        if index >= self.size or index < 0:
45            print("check given", index, "index value and enter again")
46            return False
47        current = self.head
48        for i in range(index):
49            current = current.next
50        return current.data
51
52    def peek_element(self, data):
53        current = self.head
54        while current.data != data:
55            if current.next is None:
56                print("element", data, "not found")
57                return False
58            current = current.next
59        print("element", data, "is found")
60        return True
61
62    def delete_top_element(self):
63        return self.delete_with_index(0)
64
65    def delete_bottom_element(self):
66        return self.delete_with_index(self.size - 1)
67
68    def delete_with_index(self, index):
69        if index >= self.size or index < 0:
70            print("check given", index, "index value and enter again")
71            return False
72        self.size -= 1
73        if index == 0:
74            temp = self.head
75            self.head = self.head.next
76            return temp.data
77        current = self.head
78        for i in range(index - 1):
79            current = current.next
80        temp = current.next
81        current.next = current.next.next
82        return temp.data
83
84    def delete_with_value(self, data):
85        current = self.head
86        previous = current
87        while current.data != data:
88            if current.next is None:
89                print("element", data, "not found")
90                return False
91            previous = current
92            current = current.next
93        temp = previous.next
94        previous.next = current.next
95        print("element", data, "is found and deleted")
96        self.size -= 1
97        return temp.data
98
99    def print_val(self):
100        current = self.head
101        while current:
102            print(current.data, "\b--->", end="")
103            current = current.next
104        print()
105
106
107linked_list = Linkedlist()
108
109
110def trail1():
111    linked_list.insert_at_the_beginning(45)
112    linked_list.insert_at_the_beginning(65)
113    linked_list.insert_at_the_beginning(34)
114    linked_list.insert_at_the_beginning(56)
115    linked_list.insert_at_the_beginning(78)
116    linked_list.insert_at_the_beginning(98)
117    linked_list.insert_at_the_beginning(63)
118    linked_list.insert_at_the_beginning(31)
119    linked_list.print_val()
120
121
122def trail2():
123    linked_list.insert_at_the_beginning(78)
124    linked_list.insert_at_the_ending(67778)
125    linked_list.insert_at_the_ending(899)
126    linked_list.insert_at_the_ending(99)
127    linked_list.print_val()
128    trail1()
129
130
131def trail3():
132    linked_list.insert_at_the_beginning(34)
133    linked_list.insert_at_the_beginning(56)
134    linked_list.insert_at_the_beginning(78)
135    linked_list.insert_at_the_beginning(31)
136    linked_list.insert_at_the_ending(12)
137    linked_list.insert_at_the_ending(14)
138    linked_list.insert_at_the_ending(56)
139    linked_list.insert_with_index(90, 345)
140    linked_list.insert_with_index(5, 23)
141    print(linked_list.peek_index(2))
142    print(linked_list.peek_bottom())
143    print(linked_list.peek_top())
144    linked_list.peek_element(16)
145    linked_list.peek_element(33)
146    linked_list.insert_at_the_beginning(128)
147    linked_list.insert_at_the_beginning(784)
148    linked_list.insert_at_the_beginning(314)
149    linked_list.print_val()
150    print(linked_list.delete_with_index(5))
151    linked_list.print_val()
152    print(linked_list.delete_top_element())
153    linked_list.print_val()
154    print(linked_list.delete_bottom_element())
155    linked_list.print_val()
156    linked_list.delete_with_value(12)
157    linked_list.print_val()
158    # trail2()
159
160    # this is siva
161    # signing off
162
163
164if __name__ == "__main__":
165    trail3()
166
queries leading to this page
singly linked list implementation pythonlinked list operations pythonlinked list in django tutorialspointpython singly linked listimplementation of singly linked list in pythonlink listlinked lists python3singly linked list code pthonhow to implement singly linked list in pythonlinked list in python implementationpython build a linked listsingly linked list in python create and displaysingly linked list implementation code in pythonlinked list in tkinterhow to print a singly linked in like a list in pythonlinked list singly link array python 23 definition for singly linked list pythonsingly linked list implementation in pythonhow to write singly linked list in pythonsingly linked list program in pythonhow to make linked list with one class pythonsingly linked list contains pythonpython singly linked list codesingly linked list code in pythonsingly linked list python implementationsingly linked listsingly linked list 28 29 pythonsingle linked lists pythonsingly linked list in pythonpython 3 linked list functionsruntime singly linked list pythonprogram to implement singly linked list in pythonpython singly linked list implementationhow to return the head of a linked list in pythonpython singly linked list insertcreate new node in linked list pyhtonpython linkeidn list singly linked lists pythonlinked lis in pythongimplementing singly linked list in pythonsingly linked list using python classsingly linked list python class 3singly linked list implementation in data structure code pythonhow to create a new node in linked list in pyhthonpython singlylinkedlistlist node to list pythonlearn linked list pythonlinked lit class in pythoncreate linked list pythonadding elements of a linkedlist pythonsingly linkedlist code in pythonpython singly linked list examplepython complete implementaion linked list gfgsingly linked list in pyhtonpython how to inmplement linked listsingly linked list pythoncreating a singly linked list in pythonsame object in linked list pyhtondynamic singly linked list program in pythonlinked list insertions python algorithmself in linkedlist pythonhow to instantiate a linked list pythonsingly linked list python codestructure definition of a node in the singly linked list pythonsinglylinkedlist pythonlist node in pythonsingly linked list using pythonhow to create a linked list in pythonsingly linked list in data structure in pythonsll using pythonimplement a linked listhow to use singly linked list in pythonimplement singly linked list in pythonpython create a simple linked listsingly linked list algorithm in pythonsingly linked list in python 3singly linked list followssingly linked list in python