码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构--单链表

时间:2014-10-23 20:37:14      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   ar   for   sp   数据   div   on   ad   bs   

#!/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()

数据结构--单链表

标签:style   ar   for   sp   数据   div   on   ad   bs   

原文地址:http://www.cnblogs.com/ariesblogs/p/4046619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!