Python 实现有序单向链表

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2015-1-22
@author: beyondzhou
@name: test_sortedsinglelinkedlist.py
'''

def test_sortedsinglelinkedlist():
    
    # import linkedlist
    from sortedsinglelinkedlist import sortedsingleLinkedList
    
    # init a linkedlist named smith
    smith = sortedsingleLinkedList()
    smith.add('CSCI-112')
    smith.add('MATH-121')
    smith.add('HIST-340')
    smith.add('ECON-101')
    
    # print smith
    print 'primary smith'
    for item in smith:
        print item
        
    # remove one item
    smith.remove('HIST-340')
    
    # print smith
    print '\ndeleted smith'
    for item in smith:
        print item
        
    # pring length
    print '\nlenght of smith'
    print len(smith)
    
    # check whether not in
    print '\ncheck whether not in'
    print 'abc' in smith
 
    # check whether in
    print '\ncheck whether in'
    print 'ECON-101' in smith       
        
if __name__ == "__main__":
    test_sortedsinglelinkedlist()

# Implements the linked list
class sortedsingleLinkedList:
    # constructs an empty linked list
    def __init__(self):
        self._head = None
        self._size = 0

    # Returns the number of items in the linked list
    def __len__(self):
        return self._size

    # Determines if an item is contained in the linked list
    def __contains__(self, target):
        curNode = self._head
        while curNode is not None and curNode.item != target:
            curNode = curNode.next
        return curNode is not None

    # Adds a new item to the linked list
    def add(self, item):
        predNode = None
        curNode = self._head
        while curNode is not None and item > curNode.item:
            predNode = curNode
            curNode = curNode.next

        # Create the new node for the new value
        newNode = _LinkedListNode(item)
        newNode.next = curNode
        # Link the new node into the list
        if curNode is self._head:
            self._head = newNode
        else:
            predNode.next = newNode
        self._size += 1

    # Removes an instance of the item from the linked list
    def remove(self, item):
        predNode = None
        curNode = self._head
        while curNode is not None and item > curNode.item:
            predNode = curNode
            curNode = curNode.next
        
        # The item has to be in the linked list to remove it
        assert curNode is not None, "The item must be in the linked list."
        
        # Unlink the node and return the item
        self._size -= 1
        if curNode is self._head:
            self._head = curNode.next
        else:
            predNode.next = curNode.next
        return curNode.item

    # Returns an iterator for traversing the list of items
    def __iter__(self):
        return _LinkedListIterator(self._head)

# Defines a private storage clss for creating list nodes
class _LinkedListNode(object):
    def __init__(self, item):
        self.item = item
        self.next = None

# Defines a linked list iterator 
class _LinkedListIterator:
    def __init__(self, listHead):
        self._curNode = listHead

    def __iter__(self):
        return self

    def next(self):
        if self._curNode is None:
            raise StopIteration
        else:
            item = self._curNode.item
            self._curNode = self._curNode.next
            return item

primary smith
CSCI-112
ECON-101
HIST-340
MATH-121

deleted smith
CSCI-112
ECON-101
MATH-121

lenght of smith
3

check whether not in
False

check whether in
True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值