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
相关文章推荐
- 浅谈Python单向链表的实现
- python3实现单向链表
- python实现反向打印单向链表的两种方法
- 浅谈Python单向链表的实现
- 【转】Python实现单向链表(图解)
- Python实现基础数据结构--单向链表
- python实现单向链表
- python实现单向链表
- 基于Python单向循环链表实现尾部、任意位置添加,删除
- Python中的单向链表如何实现?
- 单向链表之有序插入节点(C语言实现)
- Python 实现带头尾指针的单向链表
- python实现单向链表连
- 二叉树、list单向链表的实现( python版 Java版)-17-9-18
- python实现反转部分单向链表
- python实现单向链表
- Python实现的单向循环链表功能示例
- 单向链表的Python实现
- python 单向链表实现
- python实现单向链表