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