博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构--单链表
阅读量:5312 次
发布时间:2019-06-14

本文共 3406 字,大约阅读时间需要 11 分钟。

#!/use/bin/env python

#encoding=gbk
 
import sys
class Node():
    def __init__(self, data, next = None):
        self.data = data
        self.next = next
 
"""
单链表:
1. 初始化单链表
2. 单链表是否为空
3. 单链表的长度
4. 获得单链表指定位置的元素
5. 重置指定位置的单链表的元素
6. 在单链表指定位置插入一个元素
7. 在单链表的尾部追加一个元素
8. 删除单链表指定位置的元素
9. 清空单链表
10. 打印单链表的内容 
 
"""
class LinkedList():
    def __init__(self):
        self.head = None
 
    def initList(self, data):
        self.head = Node(data[0])
        pointer = self.head
 
        for in range(1,len(data)):
            node = Node(data[i])
            pointer.next = node
            pointer = pointer.next
 
    def isEmpty(self):
        if self.head == None:
            return True
        return False
 
    def length(self):
        pointer = self.head
        length = 0
        while pointer != None:
            length += 1
            pointer = pointer.next
        return length
 
    def get(self, index):
        if self.head != None and index > -1:
            pointer = self.head
            = 0
            while pointer != None and j < index:
                += 1
                pointer = pointer.next
            if pointer != None:
                return pointer
        return None
 
    def set(self, index, element):
        if self.head != None and index > -1:
            pointer = self.head
            = 0
            while pointer != None:
                if == index:
                    pointer.data = element
                += 1
                pointer = pointer.next
            return 0
        return -1
    def setx(self, index, element):
        node = self.get(index)
        if node != None:
            node.data = element
        return -1
 
    def add(self, index, element):
        if self.head != None:
            if index <= 0:
                print "index:[%d],insert head!"%index
                self.head = Node(element, self.head)
                return True
            else:
                pointer = self.head
                = 0
                while pointer.next != None and j < index - 1:
                    += 1
                    pointer = pointer.next
                pointer.next = Node(element, pointer.next)
                return True
            return False
        else:
            print "the linked list is empty!"
            return False
 
    def append(self, element):
        return self.add(sys.maxint, element)
 
    def remove(self, index):
        if self.head != Node:
            if index == 0:
                print "index:[%d],remove head!"%index
                self.head = self.head.next
                return True
            else:
                pointer = self.head
                = 0
                while pointer.next != Node and j < index - 1:
                    += 1
                    pointer = pointer.next
 
                if pointer.next != None:
                    pointer.next = pointer.next.next
                return  True
        else:
            print "the linked list is empty!"
            return False
    def clear(self):
        self.head = None
        return 0
    def show(self):
        if self.head != None:
            showInfo = ""
            pointer = self.head
            while pointer != None:
                showInfo += "%s "%(pointer.data)
                pointer = pointer.next
            return showInfo.strip()
        return None
 
 
if __name__ == '__main__':
    linkedList = LinkedList()
    print "linked list is empty:[%s]"%linkedList.isEmpty()
    linkedList.initList(["tom","jim","jack","json"])
    print "linked list is empty:[%s]"%linkedList.isEmpty()
    print "linked list's length:[%d]"%linkedList.length()
    node = linkedList.get(3)
    print "linked list's third element is:[%s]"%(node.data)
    print "linked list's info:[%s]"%linkedList.show()
    linkedList.set(3,"winston")
    print "linked list's info:[%s]"%linkedList.show()
    linkedList.setx(3,"infox")
    print "linked list's info:[%s]"%linkedList.show()
     
    linkedList.add(0,"head")
    print "linked list's info:[%s]"%linkedList.show()
    linkedList.add(linkedList.length(),"foot")
    print "linked list's info:[%s]"%linkedList.show()
    linkedList.add(linkedList.length()/2,"middle")
    print "linked list's info:[%s]"%linkedList.show()
     
    linkedList.append("append")
    print "linked list's info:[%s]"%linkedList.show()
     
    linkedList.remove(linkedList.length())
    print "linked list's info:[%s]"%linkedList.show()
     
    linkedList.clear()
    print "linked list's info:[%s]"%linkedList.show()
    print "linked list's length:[%d]"%linkedList.length()

转载于:https://www.cnblogs.com/ariesblogs/p/4046619.html

你可能感兴趣的文章
自己动手写ORM(01):解析表达式树生成Sql碎片
查看>>
如何使用USBWebserver在本机快速建立网站测试环境
查看>>
变量提升
查看>>
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>
JavaScript动画打开半透明提示层
查看>>
jquery-jqzoom 插件 用例
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
查看oracle数据库的连接数以及用户
查看>>
【数据结构】栈结构操作示例
查看>>
三.野指针和free
查看>>
activemq5.14+zookeeper3.4.9实现高可用
查看>>
TCP/IP详解学习笔记(3)IP协议ARP协议和RARP协议
查看>>
简单【用户输入验证】
查看>>
python tkinter GUI绘制,以及点击更新显示图片
查看>>
20130330java基础学习笔记-语句_for循环嵌套练习2
查看>>
Spring面试题
查看>>
C语言栈的实现
查看>>
代码为什么需要重构
查看>>
TC SRM 593 DIV1 250
查看>>