python 线性表的链式存储
来源:互联网 发布:网站代备案 知乎 编辑:程序博客网 时间:2024/06/05 07:58
最后一位插入有点问题
class Node(object):def __init__(self,value,next=None):self.value = valueself.next = nextclass LinkList(object):def __init__(self):self.head=0def CreateList(n):if n<0:return Falseif n==1:return Node(1)else:root = Node(1)tmp = rootfor i in range (2,n+1):tmp.next = Node(i)tmp = tmp.nextreturn rootdef PrintList(head):p=headwhile p!=None:print p.valuep=p.nextdef ListLen(head):p=headsum=0while p!=None:sum +=1p=p.nextdef insertList(head,n):p=headif n<1 or n>ListLen(head):returnfor i in range(1,n-1):p=p.nexta=input("please input a value: ")print at=Node(value=a)t.next=p.nextp.next=treturn headdef deleteList(head,n):if n<1 or n>ListLen(head):returnelif n is 1:head = head.nextelse:p=headfor i in range(1,n-1):p=p.nextq=p.nextp.next = q.nextreturn headdef main(): # print "Create a linklist" # head=CreateList(7) # PrintList(head) # print # print "___________________________" # n1=input("Enter the index to insert ") # n1=int(n1) # print n1 # insertList(head,n1) # PrintList(head) # print # print "___________________________" # n2=input("Enter the index to delete ") # n2=int(n2) # deleteList(head,n2) # PrintList(head) l=LinkList() l.CreateList(7) if __name__=='__main__': main()
#然而依旧没有好
class Node(object):def __init__(self, value,p=0):self.data = valueself.next = pclass LinkedList(object):def __init__(self):self.head = Nonedef getLength(self):p = self.headi = 0while p!= None :p=p.nexti+=1print "length: %d " % i return idef getItem(self,index):p = self.headj = 1while p != None and j < index:p=p.nextj+=1# if p ==None or j > index:# print 'p=None'# return Falseprint "oo"value = p.dataprint "getItem:value= %d" %valuereturn valuedef ListInsert(self,index,value):p=self.headj=1while p != None and j < index:p = p.nextj+=1# if p ==None or j > index:# return Falses=Node(value=value)s.next=p.nextp.next=sreturn pdef ListDelete(self,index):p=self.headj=1while p != None and j < index:p = p.nextj+=1if p ==None or j > index:return Falseq = p.nextp.next = q.nextreturn q.datadef ListInit(self,index):p = self.headL = Node(1,p=0)for i in range(0,index):value = input("please input value: ")p = Node(value,L.next)L.next = pprint 'p %s' % p.datareturn pdef ListClear(self):p = self.headprint 'p=none'while p != None:q = p.nextp.next = q.nextprint " ListDelete"return pif __name__ == '__main__':l=LinkedList()l.ListInit(3)l.getLength()#l.getItem(1)l.ListClear()
0 0
- python 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 《线性表的链式存储》
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- thinkphp中的变量输出
- 优化UITableViewCell高度计算的那些事
- 数据挖掘十大算法之CART详解
- MongoDB数据库设计(二)
- 行优先和列优先的算法
- python 线性表的链式存储
- atoi 字串转换为数字
- 用Python模拟键盘输入
- 微信小程序瀑布流的实现
- 简单的RBAC用户角色权限控制
- 用深度学习方法做topic modeling
- 自定义协议封装包头、包体
- Oracle导入dmp文件,Oracle创建数据库
- geth的使用入门