python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】


Posted in Python onOctober 24, 2019

本文实例讲述了python单向链表的基本实现与使用方法。分享给大家供大家参考,具体如下:

# -*- coding:utf-8 -*-
#! python3
class Node():
  def __init__(self,item):
    #初始化这个节点,值和下一个指向
    self.item = item
    self.next = None
class SingleLinklist():
  def __init__(self):
    #初始化这个单链表的头指针为空
    self._head = None
  def length(self):
    #获取这个链表的长度
    count = 0
    cur = self._head
    while cur != None:
      count+=1
      cur = cur.next
    return count
  def is_empty(self):
    """判断是否为空"""
    return self._head == None
  def add(self,item):
    """在头部添加元素"""
    node = Node(item)
    node.next = self._head
    self._head = node
  def append(self,item):
    """在尾部添加元素"""
    cur = self._head
    node = Node(item)
    while cur != None:
      cur = cur.next
    cur.next = node
  def insert(self,pos,item):
    """在选定的位置添加元素"""
    cur = self._head
    node = Node(item)
    count = 0
    if pos <= 0:
      self.add(item)
    elif pos > (self.length()-1):
      self.append(item)
    else:
      while count < (pos -1):
        count+=1
        cur = cur.next
      node.next = cur.next
      cur.next = node
  def travel(self):
    """遍历整个链表"""
    cur = self._head
    while cur != None:
      print(cur.item,end=" ")
      cur = cur.next
    print(" ")
  def remove(self,item):
    """删除链表"""
    cur = self._head
    pre =None
    while cur != None:
      if cur.item == item:
        if not pre:
          self._head = cur.next
          break
        else:
          pre.next = cur.next
      else:
        pre = cur #
        cur = cur.next
  def search(self,item):
    """查找某个节点"""
    cur = self._head
    while cur != None:
      if cur.item == item:
        print("找到这个元素了")
        return True
      cur = cur.next
    print("抱歉没有这个元素")
    return False
singlistdemo = SingleLinklist()
singlistdemo.add(1)
singlistdemo.add(2)
singlistdemo.add(65)
singlistdemo.insert(2,77)
singlistdemo.insert(1,66)
singlistdemo.insert(0,66)
print(singlistdemo.length())
singlistdemo.travel()
singlistdemo.remove(1)
singlistdemo.travel()
singlistdemo.search(65)

运行结果:

6
66 65 66 2 77 1 

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
windows10系统中安装python3.x+scrapy教程
Nov 08 Python
用python处理图片实现图像中的像素访问
May 04 Python
Pycharm设置界面全黑的方法
May 23 Python
python版飞机大战代码分享
Nov 20 Python
python实现在函数中修改变量值的方法
Jul 16 Python
python selenium循环登陆网站的实现
Nov 04 Python
python 计算方位角实例(根据两点的坐标计算)
Jan 17 Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
Apr 27 Python
Python3+RIDE+RobotFramework自动化测试框架搭建过程详解
Sep 23 Python
Python安装Bs4的多种方法
Nov 28 Python
如何用 Python 子进程关闭 Excel 自动化中的弹窗
May 07 Python
常用的Python代码调试工具总结
Jun 23 Python
Windows下PyCharm2018.3.2 安装教程(图文详解)
Oct 24 #Python
python实现获取单向链表倒数第k个结点的值示例
Oct 24 #Python
python模块导入的方法
Oct 24 #Python
python读取word 中指定位置的表格及表格数据
Oct 23 #Python
win10下安装Anaconda的教程(python环境+jupyter_notebook)
Oct 23 #Python
pandas按行按列遍历Dataframe的几种方式
Oct 23 #Python
pandas中遍历dataframe的每一个元素的实现
Oct 23 #Python
You might like
PHP中运用jQuery的Ajax跨域调用实现代码
2012/02/21 PHP
浅析php原型模式
2014/11/25 PHP
php使用curl打开https网站的方法
2015/06/17 PHP
php监测数据是否成功插入到Mysql数据库的方法
2016/11/25 PHP
php简单计算年龄的方法(周岁与虚岁)
2016/12/06 PHP
bcastr2.0 通用的图片浏览器
2006/11/22 Javascript
浅谈Javascript面向对象编程
2011/11/15 Javascript
前后台交互过程中json格式如何解析以及如何生成
2012/12/26 Javascript
javascript内置对象arguments详解
2014/03/16 Javascript
javascript监听鼠标滚轮事件浅析
2014/06/05 Javascript
jQuery中unwrap()方法用法实例
2015/01/16 Javascript
js判断文本框剩余可输入字数的方法
2015/02/04 Javascript
nodejs调用cmd命令实现复制目录
2015/05/04 NodeJs
AngularJS控制器详解及示例代码
2016/08/16 Javascript
记一次vue去除#问题处理经过小结
2019/01/24 Javascript
vue表单中遍历表单操作按钮的显示隐藏示例
2019/10/30 Javascript
微信小程序实现页面左右滑动
2020/11/16 Javascript
Python二分查找详解
2015/09/13 Python
python+mongodb数据抓取详细介绍
2017/10/25 Python
Python实现冒泡排序的简单应用示例
2017/12/11 Python
django表单实现下拉框的示例讲解
2018/05/29 Python
python实现连续图文识别
2018/12/18 Python
Python中按键来获取指定的值
2019/03/02 Python
通过python 执行 nohup 不生效的解决
2020/04/16 Python
Python读取excel文件中带公式的值的实现
2020/04/17 Python
pycharm专业版远程登录服务器的详细教程
2020/09/15 Python
Python join()函数原理及使用方法
2020/11/14 Python
python3列表删除大量重复元素remove()方法的问题详解
2021/01/04 Python
html5移动端价格输入键盘的实现
2019/09/16 HTML / CSS
估算杭州有多少软件工程师
2015/08/11 面试题
试解释COMMIT操作和ROLLBACK操作的语义
2014/07/25 面试题
介绍一下Python中webbrowser的用法
2013/05/07 面试题
《美丽的丹顶鹤》教学反思
2014/04/22 职场文书
企业总经理任命书
2014/06/05 职场文书
新教师岗前培训方案
2014/06/05 职场文书
学雷锋活动总结报告
2014/06/26 职场文书