ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2 Python链表

2021-09-23 22:33:31  阅读:132  来源: 互联网

标签:head .__ Python self None next 链表 def


单链表类的定义

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

头插法添加元素

  • 先生成新结点

在这里插入图片描述

遍历单链表

在这里插入图片描述

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

class List(object):
    '''创建一个单链表类'''
    def __init__(self):
        '''初始化单链表'''
        self.__head = None # 表头,head为空

    def is_empty(self):
        if self.__head:
            return 1
        else:
            return 0
       def add(self, item):
        '''头插法'''
        s = Node(item) # 生成新节点
        s.next = self.__head
        self.__head = s

    def travel(self):
        '''遍历单恋表'''
        p = self.__head  # 定义指针p,p从表头开始
        while p != None:
            print(p.data, end='')
            p = p.next  # 指针移动到下一元素

尾插法

在这里插入图片描述

  • 先把指针移动到最后一个节点
  • 创建一个节点
  • 把新结点的地址放在最后一个节点的指针域里

指定位置插入元素insert(self, pos, x)

在这里插入图片描述

按值删除元素

在这里插入图片描述

单链表增删查改完整代码python

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

class List(object):
    '''创建一个单链表类'''
    def __init__(self):
        '''初始化单链表'''
        self.__head = None # 表头,head为空

    def is_empty(self):
        if self.__head:
            return 1
        else:
            return 0

    def add(self, item):
        '''头插法'''
        s = Node(item) # 生成新节点
        s.next = self.__head
        self.__head = s

	    def add(self, item):
        '''头插法'''
        s = Node(item) # 生成新节点
        s.next = self.__head
        self.__head = s

    def add_end(self, x):
        '''尾插法'''
        s = Node(x)
        p = self.__head
        if self.__head == None:
            self.__head = s
        else:
            while p.next != None:
                p = p.next
            p.next = s

    def insert(self, pos, x):
        '''指定位置插入元素'''
        p = self.__head
        count = 0
        if pos >= self.length():
            return -1
        while count < pos:
            p = p.next
            count += 1
        s = Node(x)
        s.next = p.next
        p.next = s
        return 1

    def remove(self, item):
        p = self.__head
        if p.data == item:
            self.__head = p.next
        else:
            while p!=None and p.data!=item:
                pre = p
                p = p.next
            pre.next = p.next


    def travel(self):
        '''遍历单恋表'''
        p = self.__head  # 定义指针p,p从表头开始
        while p != None:
            print(p.data, end=' ')
            p = p.next  # 指针移动到下一元素
        print('\n')
                                                                                                 def length(self):
        p = self.__head
        count = 0
        while p != None:
            count +=1
            p = p.next
        return count

    def search_pos(self, pos):
        '''按序号查找,pos是待查找的位置'''
        p = self.__head
        count = 1  # 计数器
        if pos < 1:  # 判断位置是否合法
            return None
        else:
            while p != None and count != pos:
                p = p.next
                count += 1
            return p   # 成功返回节点p

    def search_value(self, value):
        '''search a item by value'''
        p = self.__head
        while p!=None and p.data!=value:
            p = p.next

        if p.data == value:
            return p

        elif p == None:
            return None


标签:head,.__,Python,self,None,next,链表,def
来源: https://blog.csdn.net/qq_42899028/article/details/120423393

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有