• python-单链表的实现


      1  #!/usr/bin/python
      2 
      3 class Node(object):
      4     def __init__(self,value,next=None):
      5         self.value,self.next=value,next
      6 
      7 class Linklist(object):
      8     def __init__(self):
      9         self.head=None
     10     def initlist(self,data):
     11         self.head=Node(0)
     12         p=self.head
     13 
     14         for i in data:
     15             node=Node(i)
     16             p.next=node
     17             p=p.next
     18 
     19     def show(self):
     20         p=self.head.next
     21         while p!=None:
     22             print p.value
     23             p=p.next
     24     def getlength(self):
     25         p=self.head
     26         length=0
     27 
     28         while p.next !=None:
     29             length+=1
     30             p=p.next
     31         return length
     32 
     33     def insert(self,index,value):
     34         if index<0 or index>self.getlength():
     35             print "Index is Error!"
     36             return
     37         p=self.head   
     38         j=0
     39         while p.next !=None and j < index:
     40             p=p.next
     41             j+=1
     42         node=Node(value,p.next)
     43         p.next=node
     44     
     45 
     46     def remove(self,index):
     47         if index<0 or index > self.getlength():
     48             print "Index is Error!"
     49             return 
     50 
     51         p=self.head
     52         j=0
     53         while p.next !=None and j<index:
     54             p=p.next
     55             j+=1
     56         next=p.next.next
     57     
     58     def checkin(self,value):
     59         p=self.head
     60         flag=-1
     61         while p.next != None:
     62             if p.next.value==value:
     63                 flag=1
     64                 return flag 
     65             p=p.next
     66         return flag  
     67 
     68     def replace(self,index,value):
     69         p=self.head.next
     70         i=0
     71         while i<index:
     72             p=p.next
     73             i+=1
     74         p.value=value
     75     
     76     def getitem(self,index):
     77         p=self.head.next
     78         j=0
     79         while p!=None and j<index:
     80             j+=1
     81             p=p.next
     82         return p.value
     83 
     84     def is_empty(self):
     85         if self.getlength()==0:
     86             return True
     87         else:
     88             return False
     89 
     90     def __getitem__(self,key):
     91         if self.is_empty() or key>=self.getlength():
     92             print "Index is Error!"
     93             return
     94         else:
     95             return self.getitem(key)
     96     def __setitem__(self,key,value):
     97         if self.is_empty() or key>=self.getlength():
     98             print "Index is Error!"
     99             return
    100         else:
    101             return self.replace(key,value)
  • 相关阅读:
    web print
    datediff
    Typical AJAX
    sql中构建sql语句,再exec这条SQL语句
    ASP.NET中将数据输出到WORD、EXCEL、TXT、HTM
    sql中添加单引号
    sql语句批处理
    日期分组
    异步线程
    电脑内存大有什么好处?
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7195872.html
Copyright © 2020-2023  润新知