国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python列表操作實例

2020-02-23 06:18:40
字體:
來源:轉載
供稿:網友

本文實例講述了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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜君县| 田东县| 太保市| 衢州市| 漠河县| 东丽区| 定日县| 玛曲县| 宝兴县| 丹凤县| 南康市| 绥德县| 靖远县| 酒泉市| 大洼县| 南涧| 克东县| 石棉县| 泾川县| 青冈县| 通许县| 扶风县| 宜川县| 定日县| 土默特左旗| 万荣县| 文登市| 民丰县| 中超| 盐亭县| 板桥市| 永济市| 澄江县| 高密市| 杭州市| 永修县| 阳原县| 民权县| 顺平县| 玛纳斯县| 阿克苏市|