|
| 1 | +class Node:#create a Node |
| 2 | + def __int__(self,data): |
| 3 | + self.data=data#given data |
| 4 | + self.next=None#given next to None |
| 5 | +class Linked_List: |
| 6 | + pass |
| 7 | + def insert_tail(Head,data):#insert the data at tail |
| 8 | + tamp=Head#create a tamp as a head |
| 9 | + if(tamp==None):#if linkedlist is empty |
| 10 | + newNod=Node()#create newNode Node type and given data and next |
| 11 | + newNod.data=data |
| 12 | + newNod.next=None |
| 13 | + Head=newNod |
| 14 | + else: |
| 15 | + while tamp.next!=None:#find the last Node |
| 16 | + tamp=tamp.next |
| 17 | + newNod = Node()#create a new node |
| 18 | + newNod.data = data |
| 19 | + newNod.next = None |
| 20 | + tamp.next=newNod#put the newnode into last node |
| 21 | + return Head#return first node of linked list |
| 22 | + def insert_head(Head,data): |
| 23 | + tamp = Head |
| 24 | + if (tamp == None): |
| 25 | + newNod = Node()#create a new Node |
| 26 | + newNod.data = data |
| 27 | + newNod.next = None |
| 28 | + Head = newNod#make new node to Head |
| 29 | + else: |
| 30 | + newNod = Node() |
| 31 | + newNod.data = data |
| 32 | + newNod.next = Head#put the Head at NewNode Next |
| 33 | + Head=newNod#make a NewNode to Head |
| 34 | + return Head |
| 35 | + def Print(Head):#print every node data |
| 36 | + tamp=Node() |
| 37 | + tamp=Head |
| 38 | + while tamp!=None: |
| 39 | + print(tamp.data) |
| 40 | + tamp=tamp.next |
| 41 | + def delete_head(Head):#delete from head |
| 42 | + if Head!=None: |
| 43 | + Head=Head.next |
| 44 | + return Head#return new Head |
| 45 | + def delete_tail(Head):#delete from tail |
| 46 | + if Head!=None: |
| 47 | + tamp = Node() |
| 48 | + tamp = Head |
| 49 | + while (tamp.next).next!= None:#find the 2nd last element |
| 50 | + tamp = tamp.next |
| 51 | + tamp.next=None#delete the last element by give next None to 2nd last Element |
| 52 | + return Head |
| 53 | + def isEmpty(Head): |
| 54 | + if(Head==None):#check Head is None or Not |
| 55 | + return True#return Ture if list is empty |
| 56 | + else: |
| 57 | + return False#check False if it's not empty |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
0 commit comments