# 1, Course objectives

1. Data structure of linked list
2. python implementation of linked list
3. Operation time complexity of linked list

# 2, Interpretation of details

## Linked list structure: like a chain, a link is a data point.

### Single chain structure  ### Double chain structure ### Circular list structure 1. Each data node includes the data itself and the location of the front and back nodes (the single chain table only contains the location of the next node)
2. Linked list does not need continuous memory space
3. Fast insertion, slow query

## python implementation of linked list

1. Single chain table node class
```class Node:
def __init__(self,data,next=None):
self.data=data
self.next=next
```
1. Node creation
```node1=Node(1)
node2=Node(2)
node3=Node(3)
node1.next=node2
node2.next=node3

print(node2.data)
print(node1.next.data)
print(node1.next.next.data)
print(node2.data)
``` 3. Single link list data structure

```class Node:
def __init__(self,data,next=None):
self.data=data
self.next=next

def get_data(self):
return self.data

def get_next(self):
return self.next

def set_data(self,new_data):
self.data=new_data

def __init__(self):
self.length=0
def __len__(self):
return self.length

'''
//Insert node
'''
self.length+=1

def __iter__(self):
'''
//Traversal list
'''
yield current
#Create a list
for i in range(10):
node=Node(i)
```
1. Single list traversal ```current=linkedlist.head
while current is not None:
current=current.next
```
1. Insert data from end ```new_node=Node('new node')
while current is not None:
current=current.next
current.next=new_node
```
```def append_node(self,node):
'''
//Insert data node at the end
'''
while current.next is not None:
current=current.next
current.next=node
self.length+=1

new_node=Node('new Node')
```
1. Remove node from start ```linkedlist.head=linkedlist.head.next
```
```#Pop up and return to the first node
def pop_first(self):
self.length-=1
print(d.data)
```
1. Remove from end ```current=linkedlist.head
while current.next.next is not None:
current=current.next
current.next=None
```
```def pop_last(self):
while current.next.next is not None:
current=current.next
node,current.next=current.next,None
self.length-=1
return node

print(d.data)
```
1. Insert data from anywhere ```new_node=Node('new_index')
else:
while index>1 and current.next is not None:
current=current.next
index-=1
current.next,new_node.next=new_node,current.next
```
```def insert(self,index,new_node):
if self.head is None or index<1:
else:
while index>1 and current.next is not None:
current=current.next
index-=1
current.next,new_node.next=new_node,current.next
self.length+=1

index=3
new_node=Node('new_index')
```
1. Delete data from anywhere ```if linkedlist.head.next is not None or index<0:
else:
while index>1 and current.next is not None:
current=current.next
index-=1
current.next=current.next.next
```
```def remove(self,index):
if self.head is None or index<0:
return None
else:
while index>1 and current.next is not None:
current=current.next
index-=1
current.next=current.next.next
self.length-=1
#Delete node at index
index=4
```

## Time complexity

### Time complexity of various operations

Access from location i O(n)
Replace at position i O(n)
Insert data from end O(n)
Remove from end O(n)
Insert from position i O(n)
Remove from position i O(n)
Insert from start O(1)
Remove from start O(1)

### Advantages of single chain table

1. High memory space utilization (no need for continuous memory space) and low space complexity.
2. It is suitable for the scene of starting to insert and reading at the beginning

Tags: Python

Posted on Sat, 20 Jun 2020 03:20:15 -0400 by rohanm02