程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

数据结构-链表

发布于2019-08-22 16:42     阅读(451)     评论(0)     点赞(18)     收藏(4)


基本数据类型就是:整型、浮点型、字符串型

列表元组等是高级的数据类型;

内存是用来 存储数据,并直接跟CPU连接,即CPU读取内存里面的内容;内存的基本单位是以字节来存储单元的,一共字节是八位;计算机的存储空间是作为一个整体的空间,这个空间以字节来作为最小的存储单元,每个字节是由八位构成,当计算机需要寻找存储空间的某元素的时候,会根据字节的编号寻 找,即每次 能读取八个位也就是一共字节,对于三十二位的计算机,一共基本整型需要占四个字节,例如对于一个整型  int a =1,在计算机内部以二进制存储,占四个字节每个字节八位,其中前三个字节的八个位都存的0,最后一共字节前七个位存的0,最后一个位存的1,这样就完成了整型1在计算机里面的存储;

字符(char),一个字符占一个字节

一个列表是以顺序表的形式存储,即第一个元素存储之后,紧接着第二个元素开始存储,这样找出了第一个元素的位置,后面元素的位置也就相应的找出来了;操作系统会一次性给出列表所需的内存,以整体给出;

因此编号从0 开始,其实就是位置在第一个初始位置的偏移量,第一个位置偏移0,第二个位置偏移1,

当列表中 存储的是不同类型的数据形式的时候,则不能按照上面的顺序表来计算了;

因此需要用地址来存取,即存储元素的位置的编号,每个地址的大小不一样,根据元素大小来确定其大小,但是每一个地址的大小一样,都是四个字节       地址指向需要存的数据      ,这种方法叫做元素外置的方法;

综上所述,顺序表有两种基本的形式,内置和外置,其中内置是直接存取元素,外置是先存数据的位置,在通过位置找到其对应 的元素;

python已经对顺序表进行封装,不需要自行设置;

通常顺序表还会有一个表头信息,表头信息包含容量和元素个数

链表:

链表是每出现一个数据就申请相应的空间存储起来,各元素之间的存储是无序的,然后让每一个存储数据的位置多出一部分,这一部分存储指向下一个数据的位置内容,从而形成链表;这种方式不会先计算整个数据结构所需的存储空间的,而是出现一个存储一个;即需要两个位置,第一个叫数据区,第二个叫链接区;链表和顺序表统称为线性表;最后一个位置的链接区指向空;

python中等号只是说明一个指向,等号左边的内容指向右边的内容

  1. #视频课程内容
  2. #定义一个节点的代码
  3. class Node(object):#顶一个一个节点
  4. def __init__(self,element):
  5. self.element=element
  6. self.next=None#此时节点还没有串起来,因此指向None,
  7. '''
  8. node=node1
  9. node=node2#实现链表中各个节点的的传递
  10. '''
  11. class Singlelinklist(object):#此时的单链表就是一个新的数据类型,这个数据类型含有一些数据,同时支持以下几种访问的方法
  12. def __init__(self,node=None):#不传入node的时候是空的链表
  13. self._head=node#新定义的单链表中包含的元素是节点,里面包括元素内容和next
  14. #且是一个私有属性,即只能内部访问,即单链表中的元素是内部私有属性
  15. def is_empty(self):
  16. return self._head==None
  17. def length(self):
  18. cur=self._head#用来移动遍历节点
  19. count=0#用来记录数量
  20. while cur != None:
  21. count+=1
  22. cur=cur.next
  23. return count
  24. def travel(self):
  25. cur=self._head
  26. while cur!=None:
  27. print(cur.element,end=' ')
  28. cur=cur.next
  29. def append(self,item):#尾部添加元素
  30. node=Node(item)
  31. cur=self._head
  32. if self.is_empty():
  33. self._head=node
  34. else:
  35. while cur.next!=None:
  36. cur=cur.next
  37. cur.next=node
  38. def add(self,item):#在头部增加元素
  39. node=Node(item)
  40. if self.is_empty():
  41. self._head=node
  42. else:
  43. node.next=self._head
  44. self._head=node
  45. def insert(self,pos,item):#在指定位置添加元素,在pos之前加入
  46. node=Node(item)
  47. #通过定义前一个节点pre来达到插入的效果
  48. if pos <=0:
  49. self.add(item)
  50. elif pos >self.length()-1 :
  51. self.append(item)
  52. else:
  53. pre=self._head
  54. count=0
  55. while count<pos-1:
  56. count+=1
  57. pre=pre.next
  58. node.next=pre.next
  59. pre.next=node
  60. def remove(self,item):
  61. if not self.is_empty():
  62. pre=self._head
  63. if pre.element==item:
  64. self._head=pre.next
  65. else:
  66. while pre.next.element!=item:
  67. pre=pre.next
  68. cur=pre.next
  69. pre.next=cur.next
  70. return self.travel()
  71. else:
  72. raise ValueError("item don't exist")
  73. def search(self,item):
  74. cur=self._head
  75. while cur!=None:
  76. if cur.element==item:
  77. return True
  78. else:
  79. cur=cur.next
  80. return False
  81. if __name__=='__main__':
  82. ll=Singlelinklist() #构建了一个空的单链表,里面有_head属性
  83. print(ll.is_empty())
  84. print(ll.length())
  85. ll.append(1)
  86. ll.append(2)
  87. ll.append(3)
  88. ll.length()
  89. ll.add(8)
  90. ll.insert(-1,5)
  91. ll.travel()
  92. print('\n')
  93. ll.remove(2)

#双向链表

  1. class Doublelinklist(Singlelinklist):#继承单链表的长度、是否位空、遍历以及查找这几种方法
  2. def __init__(self,node=None):
  3. self._head=node
  4. self.__next=None
  5. self.__prev=None
  6. #这些标注了的方法可以重新定义,也可以继承
  7. # def is_empty(self):
  8. # return self._head==None
  9. # def length(self):
  10. # cur=self._head#用来移动遍历节点
  11. # count=0#用来记录数量
  12. # while cur != None:
  13. # count+=1
  14. # cur=cur.next
  15. # return count
  16. # def travel(self):
  17. # cur=self._head
  18. # while cur!=None:
  19. # print(cur.element,end=' ')
  20. # cur=cur.next
  21. def add(self,item):#头部插入元素
  22. node=Node(item)
  23. node.next=self._head#建立向后索引的指针
  24. self._head=node#定义新的头节点
  25. node.next.prev=node#向前添加指针
  26. def append(self,item):#在链表的尾部添加元素
  27. node=Node(item)
  28. if self.is_empty():
  29. self._head=node
  30. else:
  31. cur=self._head
  32. while cur.next !=None:
  33. cur=cur.next
  34. cur.next=node
  35. node.prev=cur
  36. def insert(self,pos,item):#在指定位置插入
  37. node=Node(item)
  38. if self.is_empty():
  39. self._head=node
  40. elif pos==self.length()-1:
  41. self.append(item)
  42. else:
  43. cur=self._head
  44. count=0
  45. while count<pos:#这种算法还是需要从头节点开始遍历到指定位置
  46. cur=cur.next
  47. count+=1
  48. cur.prev.next=node
  49. node.prev=cur.prev
  50. node.next=cur
  51. cur.prev=node
  52. def remove(self,item):
  53. cur=self._head
  54. # pre=None
  55. # while cur!=None:
  56. # if cur.element==item:
  57. # if cur==self._head:
  58. # self._head=cur.next
  59. # else:
  60. # pre.next=cur.next
  61. # break
  62. # else:
  63. # pre=cur
  64. # cur=cur.next
  65. if cur.next==None:#序列为空的时候
  66. return None
  67. else:
  68. while cur !=None:
  69. if cur.element==item:
  70. if cur==self._head:
  71. self._head=cur.next
  72. elif cur.next==None:
  73. cur.prev.next=None
  74. else:
  75. cur.prev.next=cur.next
  76. cur.next.prev=cur.prev
  77. break
  78. else:
  79. cur=cur.next
  80. return self.travel()
  81. # def search(self,item):
  82. # cur=self._head
  83. # while cur!= None:
  84. # if cur.element==item:#由于定义node的时候已经调用了Node类,故已经有了ele属性
  85. # return True
  86. # else:
  87. # cur=cur.next
  88. # return False
  89. if __name__=='__main__':
  90. ll=Doublelinklist() #构建了一个空的单链表,里面有_head属性
  91. print(ll.length())
  92. #print(ll.is_empty)
  93. ll.append(1)
  94. ll.append(2)
  95. ll.append(3)
  96. ll.add(8)
  97. ll.insert(1,5)
  98. ll.travel()
  99. print('\n')
  100. ll.remove(2)

单向循环链表

  1. #单向循环链表
  2. class Singlelinklist(object):#此时的单链表就是一个新的数据类型,这个数据类型含有一些数据,同时支持以下几种访问的方法
  3. def __init__(self,node=None):#不传入node的时候是空的链表
  4. self._head=node
  5. if node:#如果传入了一个节点,需要回到它本身
  6. node.next=node
  7. def is_empty(self):
  8. return self._head==None
  9. def length(self):
  10. cur=self._head#用来移动遍历节点
  11. if self.is_empty():
  12. return 0
  13. count=1#用来记录数量
  14. while cur.next != self._head:#尾节点判别需要修改
  15. count+=1
  16. cur=cur.next
  17. return count
  18. def travel(self):
  19. cur=self._head
  20. while cur.next!=self._head:
  21. print(cur.element,end=' ')
  22. cur=cur.next
  23. print(cur.element)#最后一个节点需要单独打印
  24. def append(self,item):#尾部添加元素
  25. node=Node(item)
  26. cur=self._head
  27. if self.is_empty():
  28. self._head=node
  29. node.next=node
  30. else:
  31. while cur.next!=self._head:
  32. cur=cur.next
  33. cur.next=node
  34. node.next=self._head
  35. def add(self,item):#在头部增加元素
  36. node=Node(item)
  37. if self.is_empty():
  38. self._head=node
  39. node.next=node
  40. else:
  41. cur=self._head
  42. while cur.next != self._head:
  43. cur=cur.next
  44. cur.next=node
  45. node.next=self._head
  46. self._head=node
  47. def insert(self,pos,item):#在指定位置添加元素,在pos之前加入
  48. node=Node(item)
  49. #通过定义前一个节点pre来达到插入的效果
  50. if pos <=0:
  51. self.add(item)
  52. elif pos >self.length()-1 :
  53. self.append(item)
  54. else:
  55. pre=self._head
  56. count=0
  57. while count<pos-1:
  58. count+=1
  59. pre=pre.next
  60. node.next=pre.next
  61. pre.next=node
  62. def remove(self,item):
  63. if not self.is_empty():
  64. pre=self._head
  65. cur=self._head
  66. prex=None
  67. if self._head.element==item:
  68. self._head=None
  69. return None
  70. while cur.next != self._head:
  71. prex=cur
  72. cur=cur.next
  73. if pre.element==item: #头节点
  74. cur.next=pre.next
  75. self._head=pre.next
  76. return self.travel()
  77. elif cur.element==item:#尾节点
  78. prex.next=self._head
  79. else:
  80. while pre.next.element!=item:
  81. pre=pre.next
  82. cur=pre.next
  83. pre.next=cur.next
  84. return self.travel()
  85. else:
  86. raise ValueError("item doesn't exist")
  87. def search(self,item):
  88. if self.is_empty():
  89. return False
  90. cur=self._head
  91. while cur.next != self._head :
  92. if cur.element==item:
  93. return True
  94. else:
  95. cur=cur.next
  96. if cur.element==item:
  97. return True
  98. else:
  99. return False
  100. #方法必须带上括号,属性不需要带上括号
  101. if __name__=='__main__':
  102. ll=Singlelinklist() #构建了一个空的单链表,里面有_head属性
  103. print(ll.length())
  104. print(ll.is_empty())
  105. print(ll.search(5))
  106. ll.append(1)
  107. ll.remove(1)
  108. ll.append(2)
  109. ll.append(3)
  110. ll.add(8)
  111. ll.insert(1,5)
  112. ll.travel()
  113. print('\n')
  114. ll.remove(3)
  115. print(ll.search(5))

 

在面向对象的编程中,如果希望某个类中的属性只能通过类里面定义的方法来访问,而不能直接由类的实例访问属性,则将类定义成私有变量,即名称前加入两个下划线,如果要通过外部访问,那么这个私有变量的名称在内部已经变成了  下划线+类名+双下划线+属性名

 

 



所属网站分类: 技术文章 > 博客

作者:3434erer

链接:https://www.pythonheidong.com/blog/article/52990/be81bc5750c06c6734e2/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

18 0
收藏该文
已收藏

评论内容:(最多支持255个字符)