python列表操作实例

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

零基础学Python(一)Python环境安装

任何高级语言都是需要一个自己的编程环境的,这就好比写字一样,需要有纸和笔,在计算机上写东西,也需要有文字处理软件,比如各种名称的OFFICE。笔和纸以及office软件,就是写东西的硬件...

python 按不同维度求和,最值,均值的实例

python 按不同维度求和,最值,均值的实例

当变量维数加大时很难想象是怎样按不同维度求和的,高清楚一个,其他的应该就很清楚了,什么都不说了,上例子,例子一看便明白….. a=range(27) a=np.array(a) a=...

Python线程障碍对象Barrier原理详解

python线程Barrier俗称障碍对象,也称栅栏,也叫屏障。 一.线程障碍对象Barrier简介 # 导入线程模块 import threading # 障碍对象barrier...

Python解决八皇后问题示例

Python解决八皇后问题示例

本文实例讲述了Python解决八皇后问题的方法。分享给大家供大家参考,具体如下: 八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇...

Python基于matplotlib绘制栈式直方图的方法示例

Python基于matplotlib绘制栈式直方图的方法示例

本文实例讲述了Python基于matplotlib绘制栈式直方图的方法。分享给大家供大家参考,具体如下: 平时我们只对一组数据做直方图统计,这样我们只要直接画直方图就可以了。 但有时候我...