|
| 1 | +#creating linked list |
| 2 | +from __future__ import print_function |
| 3 | +#creating class node |
| 4 | +class Node(): |
| 5 | + def __init__ (self,data=None,next=None): |
| 6 | + self.data=data |
| 7 | + self.next=next |
| 8 | +#creating class linked list |
| 9 | +class linkedList(): |
| 10 | + def __init__(self,name="Unknown linked list",head=None): |
| 11 | + self.head=head |
| 12 | + self.name=name |
| 13 | + |
| 14 | + #inserting elements |
| 15 | + #inserting at beg: |
| 16 | + def insert_at_beginning(self,new_data): |
| 17 | + if self.head is None: |
| 18 | + new_node=Node(new_data) |
| 19 | + new_node.next=self.head |
| 20 | + self.head=new_node |
| 21 | + return |
| 22 | + first_node=self.head |
| 23 | + new_node=Node(new_data) |
| 24 | + new_node.next=first_node |
| 25 | + self.head=new_node |
| 26 | + |
| 27 | + #insert at end |
| 28 | + def insert_at_end(self,new_data): |
| 29 | + if self.head is None: |
| 30 | + new_node=Node(new_data) |
| 31 | + self.head=new_node |
| 32 | + new_node.next=None |
| 33 | + return |
| 34 | + new_node=Node(new_data) |
| 35 | + first_node=self.head |
| 36 | + while first_node.next: |
| 37 | + first_node=first_node.next |
| 38 | + first_node.next=new_node |
| 39 | + new_node.next=None |
| 40 | + |
| 41 | + #insert at middle |
| 42 | + def insert_at_middle(self,index,new_data): |
| 43 | + if index<0 and index>self.get_length(): |
| 44 | + raise Exception("INVALID INDEX") |
| 45 | + return |
| 46 | + if self.head is None and index==0: |
| 47 | + self.insert_at_beginning(new_data) |
| 48 | + return |
| 49 | + if index==0: |
| 50 | + self.insert_at_beginning(new_data) |
| 51 | + return |
| 52 | + first_node=self.head |
| 53 | + #new_node=Node(new_data) |
| 54 | + count=0 |
| 55 | + while first_node.next: |
| 56 | + if count==index-1: |
| 57 | + new_node=Node(new_data) |
| 58 | + new_node.next=first_node.next |
| 59 | + first_node.next=new_node |
| 60 | + first_node=first_node.next |
| 61 | + count+=1 |
| 62 | + |
| 63 | + #removing_an_element_at_beg |
| 64 | + def removing_an_element_at_beg(self): |
| 65 | + n=self.head |
| 66 | + if self.head is None: |
| 67 | + print("Removing from {} EMPTY LINKED LIST is not Possible".format(n)) |
| 68 | + return |
| 69 | + first_node=self.head |
| 70 | + self.head=self.head.next |
| 71 | + first_node=None |
| 72 | + return |
| 73 | + |
| 74 | + |
| 75 | + #removing_an_element_at_end |
| 76 | + def removing_an_element_at_end(self): |
| 77 | + n=self.head |
| 78 | + length1=self.get_length() |
| 79 | + if self.head is None: |
| 80 | + print("Removing from {} EMPTY LINKED LIST is not Possible".format(n)) |
| 81 | + return |
| 82 | + if length1 == 1: |
| 83 | + self.removing_an_element_at_beg() |
| 84 | + return |
| 85 | + first_node=self.head |
| 86 | + length2=length1-2 |
| 87 | + count=0 |
| 88 | + while first_node: |
| 89 | + if count==length2: |
| 90 | + first_node.next=first_node.next.next |
| 91 | + return |
| 92 | + first_node=first_node.next |
| 93 | + count+=1 |
| 94 | + |
| 95 | + |
| 96 | + #removing an element at middle |
| 97 | + def removing_an_element_at_middle(self,index): |
| 98 | + n=self.name |
| 99 | + if index<0 and index>self.get_length(): |
| 100 | + raise Exception("INVALID INDEX") |
| 101 | + return |
| 102 | + if self.head is None: |
| 103 | + print("Removing from {} EMPTY LINKED LIST is not Possible".format(n)) |
| 104 | + return |
| 105 | + if index==0: |
| 106 | + self.removing_an_element_at_beg() |
| 107 | + return |
| 108 | + first_node=self.head |
| 109 | + count=0 |
| 110 | + while first_node: |
| 111 | + if count==index-1: |
| 112 | + first_node.next=first_node.next.next |
| 113 | + first_node=first_node.next |
| 114 | + count+=1 |
| 115 | + return |
| 116 | + |
| 117 | + #finding length of ll |
| 118 | + def get_length(self): |
| 119 | + n=self.name |
| 120 | + if self.head is None: |
| 121 | + print("LENGTH OF THE {0} IS 0 ".format(n)) |
| 122 | + return |
| 123 | + first_node=self.head |
| 124 | + count=0 |
| 125 | + while first_node: |
| 126 | + first_node=first_node.next |
| 127 | + count+=1 |
| 128 | + #print("LENGTH OF THE {0} IS {1} ".format(n,count)) |
| 129 | + return count |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + #printing linkedlist |
| 135 | + def print_ll(self): |
| 136 | + n=self.name |
| 137 | + if self.head is None: |
| 138 | + print("{0} is EMPTY LINKED LIST".format(n) ) |
| 139 | + return |
| 140 | + first_node=self.head |
| 141 | + string=" " |
| 142 | + while first_node: |
| 143 | + string=str(first_node.data)+" ---> " |
| 144 | + first_node=first_node.next |
| 145 | + print(string,end=" ") |
| 146 | + print() |
| 147 | + |
| 148 | +if __name__=="__main__": |
| 149 | + l1=linkedList("linked list one") |
| 150 | + l1.insert_at_beginning(25) |
| 151 | + l1.insert_at_beginning(45) |
| 152 | + l1.insert_at_beginning(65) |
| 153 | + l1.insert_at_middle(0,75) |
| 154 | + l1.insert_at_beginning(85) |
| 155 | + l1.insert_at_middle(3,55) |
| 156 | + l1.print_ll() |
| 157 | + l1.removing_an_element_at_beg() |
| 158 | + l1.print_ll() |
| 159 | + l1.insert_at_beginning(85) |
| 160 | + l1.print_ll() |
| 161 | + l1.removing_an_element_at_end() |
| 162 | + l1.removing_an_element_at_end() |
| 163 | + l1.removing_an_element_at_end() |
| 164 | + l1.removing_an_element_at_end() |
| 165 | + l1.removing_an_element_at_end() |
| 166 | + l1.removing_an_element_at_end() |
| 167 | + l1.removing_an_element_at_end() |
| 168 | + l1.removing_an_element_at_end() |
| 169 | + l1.print_ll() |
| 170 | + |
| 171 | + l2=linkedList("linked list two") |
| 172 | + l2.insert_at_end(1) |
| 173 | + l2.insert_at_end(2) |
| 174 | + l2.insert_at_end(3) |
| 175 | + l2.insert_at_end(4) |
| 176 | + l2.print_ll() |
| 177 | + l2.removing_an_element_at_end() |
| 178 | + l2.print_ll() |
| 179 | + l2.removing_an_element_at_end() |
| 180 | + l2.print_ll() |
| 181 | + l2.removing_an_element_at_end() |
| 182 | + l2.print_ll() |
| 183 | + l2.removing_an_element_at_end() |
| 184 | + l2.print_ll() |
| 185 | + |
0 commit comments