python链表操作

链表中的基本要素:

  1. 结点(也可以叫节点或元素),每一个结点有两个域,左边部份叫值域,用于存放用户数据;右边叫指针域,一般是存储着到下一个元素的指针
  2. head结点,head是一个特殊的结节,head结点永远指向第一个结点
  3. tail结点,tail结点也是一个特殊的结点,tail结点永远指向最后一个节点
  4. None,链表中最后一个结点指针域的指针指向None值,因也叫接地点,所以有些资料上用电气上的接地符号代表None

链表的常用方法:

  1. LinkedList() 创建空链表,不需要参数,返回值是空链表
  2. is_empty() 测试链表是否为空,不需要参数,返回值是布尔值
  3. append(data) 在尾部增加一个元素作为列表最后一个。参数是要追加的元素,无返回值
  4. iter() 遍历链表,无参数,无返回值,此方法一般是一个生成器
  5. insert(idx,value) 插入一个元素,参数为插入元素的索引和值
  6. remove(idx)移除1个元素,参数为要移除的元素或索引,并修改链表
  7. size() 返回链表的元素数,不需要参数,返回值是个整数
  8. search(item) 查找链表某元素,参数为要查找的元素或索引,返回是布尔值

 

python用类来实现链表的数据结构,节点(Node)是实现链表的基本模块,每个节点至少包括两个重要部分。首先,包括节点自身的数据,称为“数据域”(也叫值域)。其次,每个节点包括下一个节点的“引用”(也叫指针)

下边的代码用于实现一个Node类:

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

各种操作

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
    
    def is_empty(self):
        return self.head is None
    
    def append(self,data):
        node = Node(data)
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            self.tail.next = node
            self.tail = node
    
    def iter(self):
        if not self.head:
            return
        cur = self.head
        yield cur.data
        while cur.next:
            cur =cur.next
            yield cur.data
            
    def insert(self, idx, value):
        cur = self.head
        cur_idx = 0
        if cur is None:
            raise Exception("The list is an empty")
        while cur_idx < idx - 1:
            cur = cur.next
            if cur is None:
                raise Exception('......')
            cur_idx = cur_idx + 1
        node = Node(value)
        node.next = cur.next
        cur.next = node
        if node.next is None:
            self.tail = node
            
    def remove(self, idx):
        cur = self.head
        cur_idx = 0
        if self.head is None:  # 空链表时
            raise Exception('The list is an empty list')
        while cur_idx < idx-1:
            cur = cur.next
            if cur is None:
                raise Exception('list length less than index')
            cur_idx += 1
        if idx == 0:   # 当删除第一个节点时
            self.head = cur.next
            cur = cur.next
            return
        if self.head is self.tail:   # 当只有一个节点的链表时
            self.head = None
            self.tail = None
            return
        cur.next = cur.next.next
        if cur.next is None:  # 当删除的节点是链表最后一个节点时
            self.tail = cur

            
    def size(self):
        current = self.head
        count = 0
        if current is None:
            return 'The list is an empty list'
        while current is not None:
            count += 1
            current = current.next
        return count
    
    def search(self, item):
        current = self.head
        found = False
        while current is not None and not found:
            if current.data == item:
                found = True
            else:
                current = current.next
        return found

验证操作

if __name__ == '__main__':
    link_list = LinkedList()
    for i in range(150):
        link_list.append(i)
    

print(link_list.is_empty())

link_list.insert(10, 30)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值