python 链表操作-Python单链表的基本操作

"""

初始化链表

"""

import time

class Node:

def __init__(self,value):

self.value = value

self.next = None

class SingleLinkList:

def __init__(self):

self.head = None

def is_empty(self):

"""判断是否为空"""

return self.head == None

def travel(self):

"""遍历链表"""

cur = self.head

while cur is not None:

print(cur.value,end=" ")

cur = cur.next

def append(self,item):

"""在末尾添加节点"""

node = Node(item)

# 如果是空链表

if self.is_empty():

self.head = node

return

# 当不为空的时候

cur = self.head

while cur.next:

cur = cur.next

cur.next = node

# node.next = None

def insert(self,index,item):

"""插入中间节点"""

if (self.head is None) and (self.head.next is None):

return

cur = self.head

while cur is not None and index > 1:

index = index - 1

cur = cur.next

node = Node(item)

node.next = cur.next

cur.next = node

def add(self,item):

"""在头部添加节点"""

node = Node(item)

node.next = self.head

self.head = node

def length(self):

"""获取链表的长度"""

cur = self.head

count = 0

while cur is not None:

count += 1

cur = cur.next

return count

def search(self,item):

# 查看item在链表中是否存在

cur = self.head

while cur != None:

if cur.value == item:

return True

else:

cur = cur.next

return False

def pop_last(self):

"""弹出链表的最后一个元素"""

if self.head is None:

return

cur = self.head

if cur.next is None:

self.head = None

return

while cur.next.next is not None:

cur = cur.next

cur.next = None

def pop_head(self):

"""把链表的第一个元素弹出"""

if self.head is None:

return

self.head = self.head.next

def cycle(self):

"""循环输出链表"""

cur = self.head

if cur.next == None:

cur.next = self.head

while cur.next != None:

self.travel()

time.sleep(3)

if __name__ == "__main__":

s = SingleLinkList()

s.append(100)

s.append(300)

s.append(600)

s.add(500)

s.add(150)

head = Node(30)

head.next = Node(40)

head.next.next = Node(50)

s.pop_head()

s.pop_last()

s.cycle()

s.travel()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常用的数据结构,它由一系列节点组成,每个节点由一个数据元素和一个指向下一个节点的指针组成。 单链表基本操作包括: 1. 创建一个空链表 2. 在链表头部插入一个节点 3. 在链表尾部插入一个节点 4. 在指定位置插入一个节点 5. 删除链表中的指定节点 6. 查找链表中的指定节点 7. 获取链表的长度 8. 遍历链表并输出元素 下面是单链表基本操作的示例代码: ```python # 定义单链表节点类 class ListNode: def __init__(self, data): self.data = data self.next = None # 定义单链表类 class LinkedList: def __init__(self): self.head = None # 在链表头部插入一个节点 def insert_at_head(self, data): new_node = ListNode(data) if not self.head: self.head = new_node else: new_node.next = self.head self.head = new_node # 在链表尾部插入一个节点 def insert_at_tail(self, data): new_node = ListNode(data) if not self.head: self.head = new_node else: cur = self.head while cur.next: cur = cur.next cur.next = new_node # 在指定位置插入一个节点 def insert_at_position(self, data, position): if position <= 0 or not self.head: self.insert_at_head(data) else: new_node = ListNode(data) cur = self.head for _ in range(position-1): if not cur.next: break cur = cur.next new_node.next = cur.next cur.next = new_node # 删除链表中的指定节点 def delete_node(self, data): if not self.head: return if self.head.data == data: self.head = self.head.next else: cur = self.head while cur.next: if cur.next.data == data: cur.next = cur.next.next break cur = cur.next # 查找链表中的指定节点 def find_node(self, data): cur = self.head while cur: if cur.data == data: return True cur = cur.next return False # 获取链表的长度 def get_length(self): count = 0 cur = self.head while cur: count += 1 cur = cur.next return count # 遍历链表并输出元素 def traverse(self): cur = self.head while cur: print(cur.data, end=' ') cur = cur.next print() # 创建一个空链表 linked_list = LinkedList() # 在链表头部插入节点 linked_list.insert_at_head(1) linked_list.insert_at_head(2) linked_list.insert_at_head(3) # 在链表尾部插入节点 linked_list.insert_at_tail(4) linked_list.insert_at_tail(5) # 在指定位置插入节点 linked_list.insert_at_position(6, 2) # 删除节点 linked_list.delete_node(3) # 查找节点 is_found = linked_list.find_node(4) print(is_found) # 获取链表长度 length = linked_list.get_length() print(length) # 遍历链表并输出元素 linked_list.traverse() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值