您的位置:首页 > 编程语言 > Python开发

Python 实现有序单向链表

2015-01-22 16:40 357 查看
#!/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: