国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 線性表的鏈式存儲

2019-11-06 09:03:05
字體:
來源:轉載
供稿:網友
最后一位插入有點問題
class Node(object):	def __init__(self,value,next=None):		self.value = value		self.next = nextclass LinkList(object):	def __init__(self):		self.head=0	def CreateList(n):		if n<0:			return False		if n==1:			return Node(1)		else:			root = Node(1)			tmp = root			for i in range (2,n+1):				tmp.next = Node(i)				tmp = tmp.next		return root	def PRintList(head):		p=head		while p!=None:			print p.value			p=p.next	def ListLen(head):		p=head		sum=0		while p!=None:			sum +=1			p=p.next	def insertList(head,n):		p=head		if n<1 or n>ListLen(head):			return		for i in range(1,n-1):			p=p.next		a=input("please input a value:  ")		print a		t=Node(value=a)		t.next=p.next		p.next=t		return head	def deleteList(head,n):		if n<1 or n>ListLen(head):			return		elif n is 1:			head = head.next		else:			p=head			for  i in range(1,n-1):				p=p.next			q=p.next			p.next = q.next			return 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 = value		self.next = pclass LinkedList(object):	def __init__(self):		self.head = None	def getLength(self):		p = self.head		i = 0		while p!= None :			p=p.next			i+=1		print "length: %d " % i 		return i	def getItem(self,index):		p = self.head		j = 1		while p != None and j < index:			p=p.next			j+=1		# if p ==None or j > index:		# 	print 'p=None'		# 	return False		print "oo"		value = p.data		print "getItem:value= %d" %value		return value	def ListInsert(self,index,value):		p=self.head		j=1		while p != None and j < index:			p = p.next			j+=1		# if p ==None or j > index:		# 	return False		s=Node(value=value)		s.next=p.next		p.next=s		return p	def  ListDelete(self,index):		p=self.head		j=1		while p != None and j < index:			p = p.next			j+=1		if p ==None or j > index:			return False		q = p.next		p.next = q.next		return q.data	def ListInit(self,index):		p = self.head		L = Node(1,p=0)		for i in range(0,index):			value = input("please input value: ")			p = Node(value,L.next)			L.next = p			print 'p %s' % p.data		return p	def ListClear(self):		p = self.head		print 'p=none'		while p != None:			q = p.next			p.next = q.next			print " ListDelete"		return pif __name__ == '__main__':	l=LinkedList()	l.ListInit(3)	l.getLength()	#l.getItem(1)	l.ListClear()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌县| 赣榆县| 琼结县| 福泉市| 会泽县| 大埔县| 洪江市| 河源市| 卢龙县| 长沙市| 兴安盟| 高雄县| 定兴县| 磐安县| 来宾市| 蕉岭县| 宝应县| 济源市| 敦化市| 武平县| 云南省| 章丘市| 全州县| 云浮市| 仙游县| 勐海县| 北流市| 奉节县| 广德县| 读书| 清镇市| 辽中县| 神农架林区| 衡山县| 芦溪县| 辽宁省| 英山县| 安丘市| 防城港市| 皋兰县| 漠河县|