python full stack engineer - data structure and algorithm: linked list

Course objectives

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

Features of linked list:

  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

class LinkedList:
	def __init__(self):
		self.head=None
		self.length=0
	def __len__(self):
		return self.length
	
	def insert_head(self,node):
		'''
		//Insert node
		'''
		self.head,node.next=node,self.head
		self.length+=1

	def __iter__(self):
		'''
		//Traversal list
		'''
		head=self.head
		while head is not None:
			current,head=head,head.next
			yield current
#Create a list
linkedlist=LinkedList()
for i in range(10):
	node=Node(i)
	linkedlist.insert_head(node)
print(len(linkedlist))
print([node.data for node in linkedlist])
  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')
current=linkedlist.head
while current is not None:
	current=current.next
current.next=new_node
def append_node(self,node):
	'''
	//Insert data node at the end
	'''
	current=self.head
	while current.next is not None:
		current=current.next
	current.next=node
	self.length+=1

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

d=linkedlist.pop_last()
print(d.data)
print([node.data for node in linkedlist])
  1. Insert data from anywhere
new_node=Node('new_index')
if linkedlist.head is None or index<1:
	linkedlist.head,new_node.next=new_node,linkedlist.head
else:
	current=linkedlist.head
	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:
		self.head,new_node.next=new_node,self.head
	else:
		current=self.head
		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')
linkedlist.insert(index,new_node)
print(len(linkedlist))
print([node.data for node in linkedlist])
  1. Delete data from anywhere
if linkedlist.head.next is not None or index<0:
	linkedlist.head=None
else:
	current=linkedlist.head
	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:
		current=self.head
		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
linkedlist.remove(index)
print(len(linkedlist))
print([node.data for node in linkedlist])

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