数据结构之单链表的操作

一. 单链表操作

1.将链表中的元素遍历出来

#首先得先有一个单链表,因此来创建一个单链表
class SingleLinklist():
	def __init__(self,node = None):
		self.__head = node
	#链表中定义一个用来遍历元素的方法
	def traver_link_list(self):
		curl = self.__head
		while curl != None:
			print(curl.element)
			curl = curl.next
		
#接下来假设现在已经有了下面这个创建好的节点
class StructNode():
	def __init__(self,element):
		self.element = element
		self.next = None
#实例化
node = StructNode(200)
link_list = SingleLinklist(node)
link_list_second = SingleLinklist()
link_list.traver_link_list()
link_list_second.traver_link_list()

输出结果:

200

2.按照下标取出链表中的元素

#首先得先有一个单链表,因此来创建一个单链表
class SingleLinklist():
	def __init__(self,node = None):
		self.__head = node
	#链表中定义一个用来判断链表是否为空的方法
	def empty_judgement(self):
		return self.__head == None
	#定义一个根据下标取元素的方法
	def subscript_value(self,num):
		curl = self.__head
		count = 0
		if num < 0:
			pass
		else:
			try:
				while count < num:
					count += 1
					curl = curl.next
				print(curl.element)
			except:
				pass
#接下来假设现在已经有了下面这个创建好的节点
class StructNode():
	def __init__(self,element):
		self.element = element
		self.next = None
#实例化
node = StructNode(150)
link_list = SingleLinklist(node)
link_list_second = SingleLinklist()
link_list.subscript_value(0)
link_list.subscript_value(5)
link_list.subscript_value(-3)
link_list_second.subscript_value(0)
link_list.subscript_value(0)
link_list_second.subscript_value(5)
link_list_second.subscript_value(-3)
				

输出结果:

150
150






3.单链表尾部添加元素

#创建节点
class StructNode(object):
	def __init__(self,element):
		self.element = element
		#print(self.element)       #打印链表元素
		self.next = None
	

#创建单链表	
class SingleLinklist(object):
	def __init__(self,node = None):
		self.__head = node
	#判断链表是否为空
	def empty_judgement(self):
		return self.__head == None

	#创建一个方法来实现对单链表尾部添加元素
	def add_end_single(self,item):
		node = StructNode(item)
		curl = self.__head
		if self.empty_judgement():           #判断链表是否为空
			self.__head = node
			print(self.__head.element)      #打印元素
		else:                           #链表非空时执行此处代码块
			while curl.next != None:
				curl = curl.next
			else:
				curl.next = node
				print(curl.next.element)       #打印元素


#创建空链表
link_list = SingleLinklist()

#链表尾部依次插入节点
link_list.add_end_single(200)
link_list.add_end_single(100)
link_list.add_end_single(50)
						

输出结果:

200
100
50

4.单链表头部添加元素

#创建一个节点的类
class StructNode(object):
	def __init__(self,element):
		self.element = element
		self.next = None
#创建一个单链表的类
class SingleLinklist(object):
	def __init__(self,node = None):
		self.__head = node
		
	#判断单链表是否为空
	def is_empty(self):
		return self.__head == None
	
	#求单链表的长度
	def length(self):
		count = 0
		curl = self.__head
		while curl != None:
			count += 1
			curl = curl.next
		return count
	#遍历单链表
	def traver(self):
		curl = self.__head
		while curl != None:
			print(curl.element)
			curl = curl.next
		
	#单链表头部添加元素
	def add(self,item):
		node = StructNode(item)
		node.next = self.__head
		self.__head = node
#创建空链表
s = SingleLinklist()

#链表头部随意添加元素
s.add(10)
s.add('python')
s.add([1,2,3,4,5,'666'])
s.add(3.24)

#遍历链表
s.traver()	

输出结果:

3.24
[1,2,3,4,5,'666']
python
10

5.在单链表指定位置添加元素

#创建一个节点的类
class StructNode(object):
	def __init__(self,element):
		self.element = element
		self.next = None
#创建一个单链表的类
class SingleLinklist(object):
	def __init__(self,node = None):
		self.__head = node
		
	#判断单链表是否为空
	def is_empty(self):
		return self.__head == None
	
	#求单链表的长度
	def length(self):
		count = 0
		curl = self.__head
		while curl != None:
			count += 1
			curl = curl.next
		return count
	#遍历单链表
	def traver(self):
		curl = self.__head
		while curl != None:
			print(curl.element)
			curl = curl.next
	#单链表头部添加元素
	def add(self,item):
		node = StructNode(item)
		node.next = self.__head
		self.__head = node
	#单链表尾部添加元素
	def append(self,item):
		node = StructNode(item)
		curl = self.__head
		if self.is_empty():           #判断链表是否为空
			self.__head = node
			#print(self.__head.element)      #打印元素
		else:                           #链表非空时执行此处代码块
			while curl.next != None:
				curl = curl.next
			else:
				curl.next = node
				#print(curl.next.element)       #打印元素

		
	#在链表指定位置添加元素
	def insert(self,pos,item):
		if pos <= 0:
			self.add(item)
		elif pos > self.length()-1:
			self.append(item)
		else:
			node = StructNode(item)
			count = 0
			pre = self.__head
			while count < pos-1:
				pre = pre.next
				count += 1
			node.next = pre.next
			pre.next = node
#创建空链表
link = SingleLinklist()

#在链表指定位置插入元素
link.insert(0,'aaa')
link.insert(-1,'ppp')
link.insert(4,'python')
link.insert(5,'hello')
link.insert(2,9)
#遍历链表
link.traver()			

输出结果:

ppp
aaa
9
python
hello

6.删除单链表中指定的元素

#创建一个节点的类
class StructNode(object):
	def __init__(self,element):
		self.element = element
		self.next = None
#创建一个单链表的类
class SingleLinklist(object):
	def __init__(self,node = None):
		self.__head = node
		
	#判断单链表是否为空
	def is_empty(self):
		return self.__head == None
	
	#求单链表的长度
	def length(self):
		count = 0
		curl = self.__head
		while curl != None:
			count += 1
			curl = curl.next
		return count
	#遍历单链表
	def traver(self):
		curl = self.__head
		while curl != None:
			print(curl.element)
			curl = curl.next
	#单链表头部添加元素
	def add(self,item):
		node = StructNode(item)
		node.next = self.__head
		self.__head = node
	#单链表尾部添加元素
	def append(self,item):
		node = StructNode(item)
		curl = self.__head
		if self.is_empty():           #判断链表是否为空
			self.__head = node
			#print(self.__head.element)      #打印元素
		else:                           #链表非空时执行此处代码块
			while curl.next != None:
				curl = curl.next
			else:
				curl.next = node
				#print(curl.next.element)       #打印元素

		
	#在链表指定位置添加元素
	def insert(self,pos,item):
		if pos <= 0:
			self.add(item)
		elif pos > self.length()-1:
			self.append(item)
		else:
			node = StructNode(item)
			count = 0
			pre = self.__head
			while count < pos-1:
				pre = pre.next
				count += 1
			node.next = pre.next
			pre.next = node
	#删除单链表中指定的元素
	def remove(self,item):
		'''创建当前游标'''
		curl = self.__head
		'''创建前驱游标'''
		pre = curl
		while curl != None:
			if self.__head.element == item:
				self.__head = curl.next
				break  
			elif curl.element == item:
				pre.next = curl.next
				break
			else:
				pre = curl
				curl = curl.next 
#创建空链表
link = SingleLinklist()

'''添加元素'''
link.append(1)
link.append(2)
link.append(3)
#遍历
#link.traver()
'''删除指定元素'''
link.remove(1)
link.remove(11)
link.remove(3)
#遍历
link.traver()

输出结果:

2

7.如何实现将传入的字符串压缩,若压缩后的字符串长度比原字符串长度长,则返回原字符串输出

def compress_string(word):
	word = word + ' '
	s = ''
	count = 1
	for i in range(len(word)-1):
		'''判断连续相邻两个字符是否相等'''
		if word[i]== word[i+1]:
			count += 1
		else:
			s += word[i]+str(count)
			count = 1
	if len(s) > len(word)-1:
		return word
	else:
		return s
#调用函数
d = compress_string('aaabbccdec')
d1 = compress_string('aaabbccd')
print(d)
print(d1)

输出结果:

aaabbccdec
a3b2c2d1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值