|
| 1 | +--- |
| 2 | +path: "/singly_linked_list_insertions" |
| 3 | +date: "October 5, 2020" |
| 4 | +title: "Singly - Insertions" |
| 5 | +author: "Bipin karki" |
| 6 | +language: "Python" |
| 7 | +difficulty: "Easy" |
| 8 | +img: "https://images.pexels.com/photos/3758105/pexels-photo-3758105.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" |
| 9 | +question: Create a class with all types of insertion on singly linked list. |
| 10 | + |
| 11 | +Note: Random. |
| 12 | +descriptionImage: "https://cdn.programiz.com/sites/tutorial2program/files/linked-list-concept_0.png" |
| 13 | +description: " A linked list data structure includes a series of connected nodes. Here, each node store the data and the address of the next node. Time complexity for Search is O(n) and for insert and delete is O(1) while the space complexity is O(n). |
| 14 | +The main advantages of using a linked list are Dynamic memory allocation, |
| 15 | +Implemented in stack and queue, |
| 16 | +In undo functionality of softwares, |
| 17 | +Hash tables, Graphs. |
| 18 | +The structure of singly Linked list is given below: |
| 19 | +" |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | + |
| 24 | +from singly_linked_list import Insert #I have imported my own modules. |
| 25 | +from singly_linked_list import Node |
| 26 | + |
| 27 | +class Insert_All_Places: |
| 28 | + def __init__(self, n): |
| 29 | + self.number_singly = Insert(n) |
| 30 | + def begining(self, data): |
| 31 | + temp = self.number_singly.head |
| 32 | + self.number_singly.head = Node(data) |
| 33 | + self.number_singly.head.next = temp |
| 34 | + def end(self,data): |
| 35 | + temp = self.number_singly.head |
| 36 | + while temp.next!=None: |
| 37 | + temp = temp.next |
| 38 | + temp.next = Node(data) |
| 39 | + def after_node(self, prev, data): |
| 40 | + if prev==None: |
| 41 | + print("Prev is None") |
| 42 | + return |
| 43 | + new_node = Node(data) |
| 44 | + new_node.next = prev.next |
| 45 | + prev.next = new_node |
| 46 | +if __name__ == "__main__": |
| 47 | + singly_list = Insert_All_Places(10) |
| 48 | + singly_list.begining(2100) |
| 49 | + singly_list.number_singly.print_list() |
| 50 | + singly_list.end(900) |
| 51 | + singly_list.number_singly.print_list() |
| 52 | + |
0 commit comments