单链表-Python操作

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

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

class Node():     # 初始化 构造函数
    def __init__(self,value,next=None):
        self.value=value
        self.next=next

def Creatlist(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):       # 链表长度
    c=0
    p=head
    while p!=None:
        c=c+1
        p=p.next
    return c

def insert(head,n):         # 在n的前面插入元素
    if n<1 or n>listlen(head):
        return

    p=head
    for i in range(1,n-1):  # 循环四次到达 5
        p=p.next
    a=raw_input("Enter a value:")
    t=Node(value=a)
    t.next=p.next     # 这里注意
    p.next=t
    return head       # 把6放在t的后面 t放在原先p的后面

def dellist(head,n):  # 删除链表
    if n<1 or n>listlen(head):
        return head
    elif n is 1:
        head=head.next   # 删除头
    else:
        p=head
        for i in range(1,n-1):  
            p=p.next     # 循环到达 2次 
        q=p.next
        p.next=q.next    # 把5放在3的后面
    return head


def main():
    print "Create a linklist"
    head=Creatlist(7)
    printlist(head)
    print
    print "___________________________"

    n1=raw_input("Enter the index to insert")
    n1=int(n1)
    insert(head,n1)
    printlist(head)
    print
    print "___________________________"

    n2=raw_input("Enter the index to delete")
    n2=int(n2)
    dellist(head,n2)
    printlist(head)


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


  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小王子单链表是一种基于Python语言实现的数据结构,它是一种线性表的存储结构,由一系列节点组成,每个节点包含两部分:数据域和指针域。数据域用于存储具体的数据,指针域用于指向下一个节点。 在小王子单链表,每个节点只能指向下一个节点,最后一个节点的指针域为空。这样的设计使得在插入、删除等操作时,只需要修改相邻节点的指针域,而不需要移动其他节点,提高了操作效率。 下面是一个简单的小王子单链表Python实现示例: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def is_empty(self): return self.head is None def append(self, data): new_node = Node(data) if self.is_empty(): self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def insert(self, data, position): if position <= 0: new_node = Node(data) new_node.next = self.head self.head = new_node else: current = self.head count = 0 while current and count < position - 1: current = current.next count += 1 if current: new_node = Node(data) new_node.next = current.next current.next = new_node def delete(self, data): if self.is_empty(): return if self.head.data == data: self.head = self.head.next else: current = self.head while current.next and current.next.data != data: current = current.next if current.next: current.next = current.next.next def display(self): current = self.head while current: print(current.data, end=" ") current = current.next print() # 示例代码 linked_list = LinkedList() linked_list.append(1) linked_list.append(2) linked_list.append(3) linked_list.insert(4, 1) linked_list.delete(2) linked_list.display() ``` 以上是一个简单的小王子单链表Python实现示例,包括了常用的插入、删除和显示操作。你可以根据自己的需求进行扩展和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值