python implementation of linked list

Linked list

Linked list is a data structure of computer, which is a kind of physical Storage unit Discontinuous, non sequential storage structure data elements The logical order of is through the in the linked list Pointer Link order. The linked list consists of a series of nodes (each element in the linked list is called a node), which can be generated dynamically at run time. Each node consists of two parts: one is storage data elements The other is to store the address of the next node Pointer Domain.

As shown in the figure above, a simple one-way linked list. Visible nodes consist of data and pointers.

 

There is no pointer in python. We need to replace the pointer with a reference. In the following, we use a pointer, but this is not a pointer, but a reference.

1 class node():
2     def __init__(self,data=0,next=None):
3         self.data=data
4         self.next=next

As shown in the figure above, the node is created

 

 

Then we need to connect the nodes to form a linked list

1 class linklist():
2     def __init__(self):
3         self.head=None
4         self.len=0

In the linked list, the head node is very important. Generally, the linked list will be traversed from the head node

 

 

After referring to some other blogs, we can conclude that we mainly operate the linked list as follows:

1. Add a node at the end of the linked list

2. Insert nodes in the linked list

3. Delete nodes in the linked list

4. Search for nodes in the linked list

5. Modify nodes in the linked list

6. Traverse the output linked list data

 

1. Add a node at the end of the linked list (the same function as the append() function in the array)

Idea: find the tail node and make the pointer of the tail node next a new node

       If the linked list is empty, add a new node directly

 1     def append_node(self,data):
 2         newnode=node(data,None)
 3         temp=self.head
 4         if self.head==None:
 5             self.head=newnode
 6         else :
 7             while temp.next!=None:
 8                 temp=temp.next
 9             temp.next=newnode
10             self.len+=1

2. Insert nodes in the linked list

Idea: the new node points to the node at the insertion position, and the previous node at the insertion position points to the new node. If you insert it in the first place, execute only the first half of the sentence

 1     def insert(self,number,newdata):
 2         temp = self.head
 3         j = 0
 4         newnode=node(newdata)
 5         if self.len < number:
 6             print("error")
 7         else:
 8             while j < number-1:
 9                 temp = temp.next
10                 j += 1
11             newnode.next=temp.next
12             temp.next=newnode
13             self.len+=1

3. Delete nodes in the linked list

Idea: let the node before deleting the node point to the node pointed by the deleted node, and clear the memory occupied by the deleted node. (there is no memory management in python, so the second sentence will not be executed)

 1     def del_node(self,number):#The first item is 0
 2         temp=self.head
 3         j=0
 4         if self.len<number:
 5             print("error")
 6         else:
 7             while j<number-1:
 8                 temp = temp.next
 9                 j+=1
10             temp.next=temp.next.next
11             self.len-=1

4. Search for nodes in the linked list

Idea: traverse the linked list from the node.

1     def search_node(self,data):
2         temp = self.head
3         j = 0
4         while temp.next!=None:
5             temp = temp.next
6             j += 1
7             if temp.data==data:
8                 break
9         return j

5. Modify nodes in the linked list

Idea: traverse the linked list from the first node, find the target node and modify its data.

 1     def change_data(self,number,newdata):
 2         temp = self.head
 3         j = 0
 4         if self.len < number:
 5             print("error")
 6         else:
 7             while j < number:
 8                 temp = temp.next
 9                 j += 1
10             temp.data=newdata

6. Traverse the output linked list data

Idea: traverse the linked list from the node and output data.

1     def print_list(self):
2         temp=self.head
3         while temp.next!=None:
4             print(temp.data,"-> ",end="")
5             temp=temp.next
6         print(temp.data)

Complete code, please take it if necessary:

 1 class node():
 2     def __init__(self,data=0,next=None):
 3         self.data=data
 4         self.next=next
 5 
 6 class linklist():
 7     def __init__(self):
 8         self.head=None
 9         self.len=0
10 
11     def append_node(self,data):
12         newnode=node(data,None)
13         temp=self.head
14         if self.head==None:
15             self.head=newnode
16         else :
17             while temp.next!=None:
18                 temp=temp.next
19             temp.next=newnode
20             self.len+=1
21 
22     def print_list(self):
23         temp=self.head
24         while temp.next!=None:
25             print(temp.data,"-> ",end="")
26             temp=temp.next
27         print(temp.data)
28 
29     def del_node(self,number):#The first item is 0
30         temp=self.head
31         j=0
32         if self.len<number:
33             print("error")
34         else:
35             while j<number-1:
36                 temp = temp.next
37                 j+=1
38             temp.next=temp.next.next
39             self.len-=1
40 
41     def search_node(self,data):
42         temp = self.head
43         j = 0
44         while temp.next!=None:
45             temp = temp.next
46             j += 1
47             if temp.data==data:
48                 break
49         return j
50 
51     def change_data(self,number,newdata):
52         temp = self.head
53         j = 0
54         if self.len < number:
55             print("error")
56         else:
57             while j < number:
58                 temp = temp.next
59                 j += 1
60             temp.data=newdata
61 
62     def insert(self,number,newdata):
63         temp = self.head
64         j = 0
65         newnode=node(newdata)
66         if self.len < number:
67             print("error")
68         else:
69             while j < number-1:
70                 temp = temp.next
71                 j += 1
72             newnode.next=temp.next
73             temp.next=newnode
74             self.len+=1
75 
76 a=linklist()
77 for i in range (0,10):
78     a.append_node(i)
79 print("append_node:  ",end="")
80 a.print_list()
81 print("del_node(2):  ",end="")
82 a.del_node(2)
83 a.print_list()
84 print("search_node(5):  ",end="")
85 print(a.search_node(5))
86 print("change_data(4,999):  ",end="")
87 a.change_data(4,999)
88 a.print_list()
89 print("a.insert(4,19909)):  ",end="")
90 a.insert(4,19909)
91 a.print_list()

 

Posted on Mon, 08 Nov 2021 18:10:14 -0500 by sam_rich