有这么一句话说“程序=数据结构+算法”,也有人说“如果把编程比作做菜,那么数据结构就好比食材(菜),算法就好比厨艺(做菜的技巧)”。

当然这是笼统的说法,不过也稍微懂得了数据结构和算法的重要性了。

其实,数据结构是数据间的有机关系,而算法是对数据的操作步骤;两者不可分开来谈,不能脱离算法来讨论数据结构,也不能脱离数据结构研究算法。

在网上看到这样一段话:

数据结构并不是来教你怎样编程的,同样编程语言的精练也不在数据结构的管辖范围之内,那是教你学习一门语言的老师的事情,他肯定不想因为数据结构而失业。数据结构是教你如何在现有程序的基础上把它变得更优(运算更快,占用资源更少),它改变的是程序的存储运算结构而不是程序语言本身。

如果把程序看成一辆汽车,那么程序语言就构成了这辆车的车身和轮胎。而算法则是这辆车的核心——发动机。这辆车跑得是快是慢,关键就在于发动机的好坏(当然轮胎太烂了也不行),而数据结构就是用来改造发动机的。

可以这么说,数据结构并不是一门语言,它是一种思想,一种方法,一种思维方式。它并不受语言的限制,你完全可以在gave 中轻而易举地实现一个用C语言给出的算法。

或许你在初入职场的时候并不会涉及到需要使用到数据结构的地方,也因而觉得数据结构貌似没用,但这和“农民工也能盖大楼,干嘛还学建筑呢?” 是一个道理,应该都懂。

前面说了超长的废话,其实我是想介绍一些数据结构的学习资源,主要针对编程新手,希望可以解决新手们“如何学习数据结构”的困惑,若对你有所帮助,那真是极好的。

浅谈单链表与双链表的区别

用代码解释两种结构---

单链表:

class Node:def __init__(self, data):self.data = dataself.next = Nonedef __str__(self):return str(self.data)# 通过单链表构建一个list的结构: 添加  删除  插入   查找 获取长度  判断是否为空...
# list1 = []  list1.append(5)     [5,]             slist =  SingleList()   slist.append(5)
class SingleList:def __init__(self, node=None):self._head = nodedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求长度def len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn count# 遍历def print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef pop(self, index):if index < 0 or index >= self.len():raise IndexError('index Error')if index == 0:self._head = self._head.nextelse:cur = self._head# 找到当前下标的前一个元素while index - 1:cur = cur.nextindex -= 1# 修改的next的指向位置cur.next = cur.next.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node类型')else:node = Node(item)if index == 0:node.next = self._headself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextcur.next = nodedef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node类型')else:node = Node(new_item)if index == 0:node.next = self._head.nextself._head = nodeelse:cur = self._headnode.next = cur.next.nextcur.next = nodedef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node类型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextif __name__ == '__main__':slist = SingleList()print(slist.isEmpty())  # Trueprint(slist.len())slist.append(5)print(slist.isEmpty())  # Falseprint(slist.len())  # 1slist.append(8)slist.append(6)slist.append(3)slist.append(1)print(slist.isEmpty())  # Trueprint(slist.len())print('---------------------')slist.print_all()print('----------pop-------------')slist.pop(2)slist.print_all()print('--------insert-------')slist.insert(1, 19)slist.print_all()print('--------update-------')slist.update(1, 18)slist.print_all()print('--------remove-------')slist.remove(18)slist.print_all()

双链表:

'''
双向链表
'''class Node:def __init__(self, data):self.data = dataself.next = Noneself.prev = Nonedef __str__(self):return str(self.data)class DoubleList:def __init__(self):self._head = Nonedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求长度def add(self, item):node = Node(item)if self.isEmpty():self._head = nodeelse:node.next = self._headself._head.prev = nodeself._head = nodedef len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn countdef print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node类型')else:node = Node(item)if index == 0:node.next = self._headnode.prev = self._head.prevself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextnode.prev = cur.prevcur.next = nodecur.prev = node.prevdef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node类型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextcur.prev = cur.prevdef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node类型')else:node = Node(new_item)if index == 0:node.next = self._head.nextnode.prev = self._head.prevself._head = nodeelse:cur = self._headnode.next = cur.next.nextnode.prev = cur.prevcur.next = nodecur.prev = nodeif __name__ == '__main__':dlist = DoubleList()print(dlist.len())print(dlist.isEmpty())# dlist.append(6)# dlist.append(9)# dlist.append(5)# print(dlist.len())# print(dlist.isEmpty())# dlist.print_all()dlist.add(6)dlist.add(9)dlist.add(5)dlist.print_all()print('--------insert-------')dlist.insert(1, 19)dlist.print_all()print('--------update-------')dlist.update(1, 18)dlist.print_all()print('--------remove-------')dlist.remove(18)dlist.print_all()

【python】数据结构和算法 + 浅谈单链表与双链表的区别相关推荐

  1. Python数据结构与算法(2.3)——链表

    Python数据结构与算法(2.3)--链表 0. 学习目标 1. 线性表的链式存储结构 1.1 指针相关概念 1.2 指针结构 1.2 结点 1.3 结点类 2. 单链表的实现 2.1 单链表的初始 ...

  2. python数据结构和算法 时间复杂度分析 乱序单词检测 线性数据结构 栈stack 字符匹配 表达式求值 queue队列 链表 递归 动态规划 排序和搜索 树 图

    python数据结构和算法 参考 本文github 计算机科学是解决问题的研究.计算机科学使用抽象作为表示过程和数据的工具.抽象的数据类型允许程序员通过隐藏数据的细节来管理问题领域的复杂性.Pytho ...

  3. Python数据结构与算法(附录)——块状链表的动态调整

    Python数据结构与算法(附录)--块状链表的动态调整 块状链表的动态调整 违反规则 1 违反规则 2 相关链接 块状链表的动态调整 我们已经知道块状链表的块的最大容量会随着链表长度的变化动态改变, ...

  4. Python数据结构与算法_9_有序链表

    前情提要:Python数据结构与算法_8_链表.无序链表 接下来我们研究有序链表. 什么是有序链表 如果给定一个链表,他的节点数据元素都是的整数,如77, 26, 31, 93, 17, 54.如果这 ...

  5. Python数据结构与算法(2.6)——块状链表

    Python数据结构与算法(2.6)--块状链表 0. 学习目标 1. 块状链表简介 1.1 块状链表介绍 1.2 块状链表中结点类 1.3 块状链表中块类 2. 块状链表的实现 2.1 块状链表的初 ...

  6. Python数据结构与算法(2.5)——循环链表

    Python数据结构与算法(2.4)--循环链表 0. 学习目标 1. 循环链表简介 2. 循环单链表实现 2.1 循环单链表的基本操作 2.2 简单的实现方法 2.3 循环单链表应用示例 2.4 利 ...

  7. 视频教程-Python数据结构与算法面试(上)-Python

    Python数据结构与算法面试(上) 东北大学计算机专业硕士研究生,欧瑞科技创始人&CEO,曾任国内著名软件公司项目经理,畅销书作者,企业IT内训讲师,CSDN学院专家讲师,制作视频课程超过1 ...

  8. python数据结构与算法面试_python面试总结4(算法与内置数据结构)

    算法与内置数据结构 常用算法和数据结构 sorted dict/list/set/tuple 分析时间/空间复杂度 实现常见数据结构和算法 数据结构/算法 语言内置 内置库 线性结构 list(列表) ...

  9. Python数据结构与算法(2.7)——跳表

    Python数据结构与算法(2.7)--跳表 0. 学习目标 1. 跳表的基本概念 1.1 跳表介绍 1.2 跳表的性能 1.3 跳表与普通链表的异同 2. 跳表的实现 2.1 跳表结点类 2.2 跳 ...

最新文章

  1. vba手机号码归属_Android手机号码归属地的查询
  2. python读取excel-Python读取Excel表格
  3. 【大佬漫谈】5G对AI反欺诈行业提出更高要求——谢映莲
  4. 赛程一览 | 2019 上海国际创客大赛
  5. spring项目概念-BeanFactoryApplicationContext
  6. vue 接口节流_vue防抖节流之v-debounce--throttle使用指南
  7. 7-142 最大子列和问题 (20 分)
  8. 一条SQL语句的千回百转
  9. 如何设置mysql字符集支持utf-8 和gbk_mysql建表的时候设置表里面的字段的字符集是utf-8要怎么设置?默认建好后我去mysql里看字符集都是gbk...
  10. java 4.0 下载_javax.servlet-api-4.0.1.jar包下载
  11. 电容屏、电阻屏基础知识
  12. tomcat设置为开机自启动
  13. Filter过滤器的作用
  14. c语言srand函数作用,C语言中srand随机函数怎么用?
  15. iOS测试之接口测试总结
  16. Windows远程桌面实现之十 - 把xdisp_virt项目移植到iOS,macOS,linux平台(一)
  17. 中缀表达式转后缀表达式——c语言栈实现
  18. 企业大数据项目规划落地实施路线图
  19. you have not installed the Java Cryptography Extension (JCE)
  20. PowerPC 32位汇编入门讲解

热门文章

  1. gcc中的内嵌汇编语言(Intel i386平台)
  2. node、npm、vue安装 -- VUE 项目 demo 实例
  3. linux (阿里云 CentOS7) 中安装配置 RocketMQ
  4. JS单引号嵌套的问题,怎么改才能对呢!
  5. Python冒泡排序(4)
  6. 生成jsp验证码的代码详解(servlet版)
  7. pureMVC简单示例及其原理讲解四(Controller层)
  8. 划分用户故事(user-story)的原则
  9. IMDb、烂番茄、MTC、各种电影行业评分名字整理
  10. springBoot(20):使用Spring Session实现集群-redis