本文实例讲述了python列表操作的方法。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:class Node:
"""Single node in a data structure"""
def __init__(self, data):
"""Node constructor"""
self._data = data
self._nextNode = None
def __str__(self):
"""Node data representation"""
return str(self._data)
class List:
"""Linked list"""
def __init__(self):
"""List constructor"""
self._firstNode = None
self._lastNode = None
def __str__(self):
"""List string representation"""
if self.isEmpty():
return "empty"
currentNode = self._firstNode
output = []
while currentNode is not None:
output.append(str(currentNode._data))
currentNode = currentNode._nextNode
return " ".join(output)
def insertAtFront(self, value):
"""Insert node at front of list"""
newNode = Node(value)
if self.isEmpty(): # List is empty
self._firstNode = self._lastNode = newNode
else: # List is not empty
newNode._nextNode = self._firstNode
self._firstNode = newNode
def insertAtBack(self, value):
"""Insert node at back of list"""
newNode = Node(value)
if self.isEmpty(): # List is empty
self._firstNode = self._lastNode = newNode
else: # List is not empty
self._lastNode._nextNode = newNode
self._lastNode = newNode
def removeFromFront(self):
"""Delete node from front of list"""
if self.isEmpty(): # raise exception on empty list
raise IndexError, "remove from empty list"
tempNode = self._firstNode
if self._firstNode is self._lastNode: # one node in list
self._firstNode = self._lastNode = None
else:
self._firstNode = self._firstNode._nextNode
return tempNode
def removeFromBack(self):
"""Delete node from back of list"""
if self.isEmpty(): # raise exception on empty list
raise IndexError, "remove from empty list"
tempNode = self._lastNode
if self._firstNode is self._lastNode: # one node in list
self._firstNode = self._lastNode = None
else:
currentNode = self._firstNode
# locate second-to-last node
while currentNode._nextNode is not self._lastNode:
currentNode = currentNode._nextNode
currentNode._nextNode = None
self._lastNode = currentNode
return tempNode
def isEmpty(self):
"""Returns true if List is empty"""
return self._firstNode is None
希望本文所述对大家的Python程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]