咱们如何把现实中大量并且很是复杂的问题以特定的数据类型(个体)和特定的存储结构(个体的关系)保存到相应的主存储器(内存)中,以及在此基础上为实现某个功能而执行的相应操做,这个相应的操做也叫作算法。
数据结构 == 个体 + 个体关系python
算法 == 对存储数据的操做算法
数据结构是软件中最核心的课程。
程序 = 数据的存储 + 数据的操做 + 能够被计算机执行的语言。数组
比较通俗的讲,把全部的节点用一根线串起来的结构就称之为线性结构。线性结构分为两种方式:数组、链表。
数组须要一块连续的内存空间来存储,堆内存的要求比较高。若是咱们申请一个100M大小的数组,当内存中没有连续的、足够大的空间时,即便内存的剩余可用空间大于100M,任然会申请失败。而链表偏偏相反,它并不须要一块连续的内存空间,他经过“指针”将一组零散的内存块串联起来使用,因此申请的是大小是100M的链表,name根本不会有问题。
数组,在python语言中成为列表,是一种基本的数据结构类型。
注:列表的其余问题,请百度python基础。数据结构
链表的及诶单结构以下:指针
data为自定义的数据,next为下一个节点的地址。code
在Python语言中用面向对象组合的方式,代替指针指向,更加的方便,简单,容易理解。对象
# Use The Linked List sort Liangshan Po 108 Heroes # class Hero(): def __init__(self, no=None, name=None, nick_name=None, next=None): self.no = no self.name = name self.nick_name = nick_name self.next = next def add_hero(head, hero): current_position = head while current_position.next and hero.no > current_position.next.no: current_position = current_position.next hero.next = current_position.next current_position.next = hero def get_all(head): current_position = head while current_position.next: print("编号:%s,姓名:%s,外号:%s" % ( current_position.next.no, current_position.next.name, current_position.next.nick_name)) current_position = current_position.next def delete_hero(head, hero): current_position = head if current_position.next: while current_position.next and current_position.next.no < hero.no: current_position = current_position.next current_position.next = current_position.next.next else: print("链表为空") head = Hero() hero = Hero(1, '宋江', '及时雨') # hero1 = Hero(2, '卢俊义', '玉麒麟') # hero2 = Hero(3, '吴用', '智多星') # hero3 = Hero(5, '林冲', '豹子头') # hero4 = Hero(4, '公孙胜', '入云龙') # add_hero(head, hero) # add_hero(head, hero1) # add_hero(head, hero2) # add_hero(head, hero3) # add_hero(head, hero4) # get_all(head) print("---------------------") delete_hero(head, hero) get_all(head)