zoukankan      html  css  js  c++  java
  • python 单链表操作

    链表是数据结构中最基本常用的,C++语言中单链表是利用指针操作实现的,python作为面向对象编程的,可以使用创建一个Node类来实现链表,利用类的属性引用来代替指针操作。

        下面我们创建了一个节点类,然后编写了几个链表操作,包括创建,插入,删除,输出等:

     1 class Node():     # 初始化 构造函数  
     2     def __init__(self,value,next=None):  
     3         self.value=value  
     4         self.next=next  
     5   
     6 def Creatlist(n):  
     7     if n<=0:  
     8         return False  
     9     if n==1:  
    10         return Node(1)    # 只有一个节点  
    11     else:  
    12         root=Node(1)  
    13         tmp=root  
    14         for i in range(2,n+1):       #  一个一个的增加节点  
    15             tmp.next=Node(i)  
    16             tmp=tmp.next  
    17     return root            # 返回根节点  
    18   
    19 def printlist(head):       # 打印链表  
    20     p=head  
    21     while p!=None:  
    22         print p.value  
    23         p=p.next  
    24   
    25 def listlen(head):       # 链表长度  
    26     c=0  
    27     p=head  
    28     while p!=None:  
    29         c=c+1  
    30         p=p.next  
    31     return c  
    32   
    33 def insert(head,n):         # 在n的前面插入元素  
    34     if n<1 or n>listlen(head):  
    35         return  
    36   
    37     p=head  
    38     for i in range(1,n-1):  # 循环四次到达 5  
    39         p=p.next  
    40     a=raw_input("Enter a value:")  
    41     t=Node(value=a)  
    42     t.next=p.next     # 这里注意  
    43     p.next=t  
    44     return head       # 把6放在t的后面 t放在原先p的后面  
    45   
    46 def dellist(head,n):  # 删除链表  
    47     if n<1 or n>listlen(head):  
    48         return head  
    49     elif n is 1:  
    50         head=head.next   # 删除头  
    51     else:  
    52         p=head  
    53         for i in range(1,n-1):    
    54             p=p.next     # 循环到达 2次   
    55         q=p.next  
    56         p.next=q.next    # 把5放在3的后面  
    57     return head  
    58   
    59   
    60 def main():  
    61     print "Create a linklist"  
    62     head=Creatlist(7)  
    63     printlist(head)  
    64     print  
    65     print "___________________________"  
    66   
    67     n1=raw_input("Enter the index to insert")  
    68     n1=int(n1)  
    69     insert(head,n1)  
    70     printlist(head)  
    71     print  
    72     print "___________________________"  
    73   
    74     n2=raw_input("Enter the index to delete")  
    75     n2=int(n2)  
    76     dellist(head,n2)  
    77     printlist(head)  
    78   
    79   
    80 if __name__=='__main__':  main()   # 主函数调用

    运行结果如下:

    run C:\Anaconda\node.py
    Create a linklist
    1
    2
    3
    4
    5
    6
    7
    
    ___________________________
    
    Enter the index to insert 6
    
    Enter a value:99
    1
    2
    3
    4
    5
    99
    6
    7
    
    ___________________________
    
    Enter the index to delete 4
    1
    2
    3
    5
    99
    6
    7

    参考资料:http://blog.csdn.net/u010786109/article/details/40650609

         https://blog.csdn.net/qq_14959801/article/details/52988861 

  • 相关阅读:
    ViewPager+Fragmrnt最简单结合方法
    Microsoft SQL Server Version List(SQL Server 版本)
    hdu 2795 Billboard(线段树单点更新)
    面向对象程序设计的思想的长处
    iOS 友盟分享
    使用Broadcast实现android组件之间的通信
    jquery ui 分页插件 传入后台的连个參数名
    android adb常见问题的解决方法!
    UVa 11015
    优秀程序猿学习方法
  • 原文地址:https://www.cnblogs.com/kuangsyx/p/9005174.html
Copyright © 2011-2022 走看看