0

0

python实现单链表

巴扎黑

巴扎黑

发布时间:2016-11-26 11:54:18

|

1672人浏览过

|

来源于php中文网

原创

# coding:utf-8 


class node: 
    def __init__(self, value): 
        self.data = value 
        self.next = none 


class linklist: 
    def __init__(self, data=[0]): 
        self.head = none 
        self.init_link_list(data) 

    # 初始化链表 
    # data 为数组 
    def init_link_list(self, data): 
        if len(data) == 0: 
            print("initialization data is null") 
            return 
        self.head = node(data[0]) 
        current = self.head 
        for index in data[1:]: 
            current.next = node(index) 
            current = current.next 

    # 获取当前结点 
    def get_node(self, index): 
        if self.is_empty(): 
            print("link is empty") 
            return 
        if index > self.get_length() or index             print("node is not exist") 
            return 
        current = self.head 
        i = 0 
        while i             if i == index - 1: 
                return current 
            current = current.next 
            i += 1 

    # 获取当前元素的值 
    def get_data(self, index): 
        current = self.get_node(index) 
        if current is none: 
            return "node is not exist" 
        return current.data 

    # 打印链表 
    def print_link(self): 
        if self.is_empty(): 
            return 
        list = [] 
        current = self.head 
        while current.next is not none: 
            list.append(current.data) 
            current = current.next 
        else: 
            list.append(current.data) 
        print(list) 

    # 获取链表长度 
    def get_length(self): 
        if self.is_empty(): 
            return 0 
        current = self.head 
        count = 0 
        while current.next is not none: 
            count += 1 
            current = current.next 
        else: 
            count += 1 
        return count 

    # 判断链表是否为空 
    # 如果为空,返回true 
    # 如果不为空,返回false 
    def is_empty(self): 
        return self.head is none 

    # 当前元素之后插入一个元素 
    # index 元素索引 
    # data 插入的值 
    def add_after(self, index, data): 
        current = self.get_node(index) 
        if current is none: 
            return "node is not exist" 
        current_next = current.next 
        current.next = node(data) 
        current = current.next 
        current.next = current_next 

    # 在当前元素之前插入一个元素 
    def add_before(self, index, data): 
        if index == 1: 
            current = self.get_node(index) 
            self.head = node(data) 
            self.head.next = current 
            return 
        pre = self.get_pre_node(index) 
        current = pre.next 
        pre.next = node(data) 
        pre = pre.next 
        pre.next = current 

    # 获取当前元素的前一个元素 
    def get_pre_node(self, index): 
        if self.is_empty(): 
            print("link is empty") 
            return 
        if index > self.get_length() or index             print("node is not exist") 
            return 
        pre = self.head 
        i = 0 
        while i             if i == index - 2: 
                return pre 
            pre = pre.next 
            i += 1 

    # 删除指定元素,并返回删除元素的值 
    def remove(self, index): 
        if index == 1 and not self.is_empty(): 
            data = self.head.data 
            self.head = self.head.next 
            return data 
        pre_node = self.get_pre_node(index) 
        current = self.get_node(index) 
        if pre_node is none or current is none: 
            print("data is not exist") 
        pre_node.next = current.next 
        return current.data 

    # 修改当前结点的值 
    def update(self, index, data): 
        current = self.get_node(index) 
        if current is none: 
            return "current node is none" 
        current.data = data 

    # 将新链表合并到当前链表 
    def merge(self, data): 
        size = self.get_length() 
        last_node = self.get_node(size) 
        last_node.next = data.head 
        return self 

# test 
y = (1,2,3,4) 
s = ["a", "b", "c", "d"] 
linklist = linklist(y) 
# linklist.init_link_list(["a", "b", "c", "d"]) 
# second = linklist() 
# second.init_link_list(["x", "y", "z"]) 
# linklist.add_after(-1, "x") 
# linklist.add_after(1, "y") 
# linklist.init_link_list([]) 
# linklist.add_before(1,"x") 
# linklist.print_link() 
# print("item:", linklist.get_data(2)) 
# print("length:", linklist.get_length()) 
# print("is empty", linklist.is_empty()) 
# print(linklist.get_pre_node(3).data) 
# print("remove node:",linklist.remove(2)) 
# linklist.merge(second) 


linklist.print_link() 

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.7万人学习

Django 教程
Django 教程

共28课时 | 3.1万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号