Python 实现有序单向链表
来源:互联网 发布:html javascript 编辑:程序博客网 时间:2024/06/07 02:27
#!/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 listclass 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 nodesclass _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 smithCSCI-112ECON-101HIST-340MATH-121deleted smithCSCI-112ECON-101MATH-121lenght of smith3check whether not inFalsecheck whether inTrue
0 0
- Python 实现有序单向链表
- Java实现有序单向链表
- Python 实现单向链表
- python实现单向链表
- Python 实现单向链表
- Python 实现简单的单向链表
- Python中的单向链表如何实现?
- 单向链表的Python实现
- 合并两个有序单向链表
- 有序单向链表的相关操作
- 两个有序单向链表的合并
- 将两个单向有序链表合并成一个单向有序链表
- 将两个有序的单向链表整合成一个有序单向链表
- 单向链表 python
- 单向链表实现
- 单向链表实现
- Python 实现带头尾指针的单向链表
- 【转】Python实现单向链表(图解)
- 做正确的事(效果)比正确的做事(效率)更重要
- linux shell for循环syntax error: bad for loop variable
- java删除文件夹或者文件
- java map list实现一对多
- 关键路径算法(PAT How Long Does It Take (25))
- Python 实现有序单向链表
- 使用PLSQL Developer连接数据库三种方式
- CAShapeLayer FontAwesome
- [mark]ubuntu ifconfig 能够获取ip但是不能联网浏览网页 ubuntu突然不能联网
- c语言使用freetds连接ms sql server
- POJ 1743 Musical Theme(后缀数组)
- testng测试 并生成覆盖率报告
- Linux Shell 文本处理工具集锦
- linux批量替换文件内容3种方法(perl,sed,shell)