• python-链队列的实现


    7 class Node(object):   
      8     def __init__(self,data):
      9         self.data = data
     10         self.next = None
     11                       
     12 class linkqueue(object):
     13     def __init__(self):
     14         self.front = None
     15         self.rear = None
     16         self.count = 0
     17     def isEmpty(self):
     18         return self.count == 0
     19     def clear(self):                                               
     20         self.front = self.rear = None
     21         self.count = 0
     22     def insert(self,item):
     23         node = Node(item)
     24         if self.isEmpty():
     25             self.front = self.rear = node
     26             self.count += 1
     27         elif self.count == 1:
     28             self.front.next = node
     29             self.rear = node
     30             self.count += 1
     31         else:         
     32             self.rear.next = node
     33             self.rear = node
     34             self.count += 1
     35     def length(self): 
     36         return self.count
     37     def delitem(self):
     38         if self.isEmpty():
     39             raise Exception("队列为空")
     40         elif self.count == 1:
     41             self.rear.next = None
     42             self.front = self.rear
     43             self.rear = None
    44             self.count -= 1
     45         else:         
     46             tempnode = self.front
     47             self.front = tempnode.next
     48             self.count -= 1
     49     def display(self):
     50         if self.front == None:
     51             print("None")
     52         tempnode = self.front
     53         while tempnode is not None:
     54             print(tempnode.data,end = " ")
     55             tempnode = tempnode.next
     56         print()       
     57                       
     58 if __name__ == "__main__":
     59     linkqueue1 = linkqueue()
     60     print(linkqueue1.length())
     61     linkqueue1.insert(1)
     62     linkqueue1.insert(2)
     63     linkqueue1.insert(3)
     64     linkqueue1.insert(4)
     65     linkqueue1.insert(5)
     66     linkqueue1.insert(6)
     67     print(linkqueue1.length())
     68     linkqueue1.display()
     69     linkqueue1.delitem()
     70     linkqueue1.display()
     71     linkqueue1.clear()
     72     linkqueue1.display()
     73  
     74  

    运行结果

    0
    6
    1 2 3 4 5 6  
    2 3 4 5 6  
    None
    该代码中没有关于队列的查找和元素值修改的方法,大家可以参照代码进行添加,代码逻辑也相对来说较为简单


    笨鸟先飞
  • 相关阅读:
    Netcat for Windows
    绕过图片格式限制上传木马获取WebShell
    Firefox Security Toolkit 安装
    centos安装异常解决方法
    docker --help
    centos更新163源并升级内核
    CentOS系统内核升级
    CentOS7安装Docker时的异常报错与解决方法
    EPEL库安装
    CentOS7 64位 自动分配IP地址设置
  • 原文地址:https://www.cnblogs.com/zoutingrong/p/13916083.html
Copyright © 2020-2023  润新知