
数据结构与算法
weixin_40458686
这个作者很懒,什么都没留下…
展开
-
python如何实现链表的逆序
class LNode: def __init__(self,x=None): self.data=x self.next=Nonedef Reverse(head): if head == None or head.next == None: return pre = None #前驱节点 cur = None#...原创 2019-02-16 15:11:35 · 1896 阅读 · 0 评论 -
python实现计算两个比较长的(超出long的表示范围)单链表之和
class LNode: def __init__(self,x=None): self.data=x self.next=Nonedef add(h1,h2): #如果其中有一个链表为空,则输出另一个链表 if h1 is None or h1.next is None: return h2 if h2 is ...原创 2019-02-17 13:35:37 · 545 阅读 · 0 评论 -
给定链表L0->L1->...L(n-1)->Ln,把它重新排序为L0->Ln->L1->L(n-1)->L2->L(n-2)...
给定链表L0->L1->…L(n-1)->Ln,把它重新排序为L0->Ln->L1->L(n-1)->L2->L(n-2)…要求(1)在原来链表基础上进行排序,不能申请新节点(2)只能修改节点next域,不能修改数据域class LNode: def __init__(self,x=None): self.data=x ...原创 2019-02-17 23:41:57 · 1604 阅读 · 0 评论 -
python用单链表实现栈
class LNode: """节点的类""" def __init__(self,x): self.data = x self.next = Noneclass MyStack: """用链表实现栈""" def __init__(self): self.data = None self.原创 2019-02-18 23:17:03 · 974 阅读 · 0 评论 -
python用链表实现队列
class LNode: """节点的类""" def __init__(self,x): self.data = x self.next = Noneclass MyQueue: """用链表实现队列,因为列表尾部出列时间复杂度高,所以一般就是在尾部加个指针self.pEnd实现尾入头出(个人理解)""" def __i原创 2019-02-19 00:48:07 · 1324 阅读 · 0 评论 -
python用递归方法如何翻转一个栈中的所有元素(无需申请额外的空间)
python用递归方法如何翻转一个栈中的元素(无需申请额外的空间,但是时间复杂度为O(n^2))class MyStack: """用列表(顺序表)实现栈""" def __init__(self): self.item = [] #判断栈是否为空 def isEmpty(self): return not self.i原创 2019-02-19 16:03:46 · 598 阅读 · 0 评论 -
python如何根据入栈序列判断出栈队列是否是可能的出栈队列
class MyStack: """先实现栈""" def __init__(self): self.item = [] #判断栈是否为空 def isEmpty(self): return not self.item #返回栈的大小(就是列表长度) def size(self): return le...原创 2019-02-19 21:23:54 · 682 阅读 · 0 评论 -
python用递归和非递归两种方式实现二分查找
def binary_search(alist,item): """二分查找,非递归""" n = len(alist) first = 0 last = n-1 while first <= last: mid = (first + last)//2原创 2019-02-20 13:41:29 · 461 阅读 · 0 评论