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

yipeiwu_com6年前Python基础

本文实例讲述了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数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

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

相关文章

在vscode中配置python环境过程解析

在vscode中配置python环境过程解析

1.安装vscode和python3.7(安装路径在:E:\Python\Python37); 2.打开vscode,在左下角点击设置图标选择setting,搜索python path,...

Python基于matplotlib实现绘制三维图形功能示例

Python基于matplotlib实现绘制三维图形功能示例

本文实例讲述了Python基于matplotlib实现绘制三维图形功能。分享给大家供大家参考,具体如下: 代码一: # coding=utf-8 import numpy as np...

pycharm配置当鼠标悬停时快速提示方法参数

pycharm配置当鼠标悬停时快速提示方法参数

1、配置pycharm 依次点击"File"-->"Settings",进入"Editor"-->"General",勾选"Other"下的“Show quick docum...

python用match()函数爬数据方法详解

python用match()函数爬数据方法详解

match()函数的使用。以及从文本中提取数据的方法。在学习re模块的相关函数前应了解正则表达式的特殊字符 准备一个要爬取的文本文档: 直接从某个网页拷贝一份代码,粘贴在 一个txt文件...

Python django框架输入汉字,数字,字符生成二维码实现详解

这篇文章主要介绍了Python django框架输入汉字,数字,字符转成二维码实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...