精品一区二区三区在线成人,欧美精产国品一二三区,Ji大巴进入女人66h,亚洲春色在线视频

Python數據結構之線性順序表

開發 后端
線性表(linear list)是數據結構的一種,一個線性表是n個具有相同特性的數據元素的有限序列。本文結合了互聯網上的一些代碼,以及結合百度百科關于線性順序表的定義,實現了全部代碼。

[[410561]]

本文轉載自微信公眾號「python與大數據分析」,作者一只小小鳥鳥 。轉載本文請聯系python與大數據分析公眾號。

線性表(linear list)是數據結構的一種,一個線性表是n個具有相同特性的數據元素的有限序列。本文結合了互聯網上的一些代碼,以及結合百度百科關于線性順序表的定義,實現了全部代碼。

在稍復雜的線性表中,一個數據元素可由多個數據項(item)組成,此種情況下常把數據元素稱為記錄(record),含有大量記錄的線性表又稱文件(file)。

線性表中的個數n定義為線性表的長度,n=0時稱為空表。在非空表中每個數據元素都有一個確定的位置,如用ai表示數據元素,則i稱為數據元素ai在線性表中的位序。

線性表的相鄰元素之間存在著序偶關系。如用(a1,…,ai-1,ai,ai+1,…,an)表示一個順序表,則表中ai-1領先于ai,ai領先于ai+1,稱ai-1是ai的直接前驅元素,ai+1是ai的直接后繼元素。當i=1,2,…,n-1時,ai有且僅有一個直接后繼,當i=2,3,…,n時,ai有且僅有一個直接前驅 [1] 。

需要轉換思想的是,線性表中的參數也好,最大數量也好,要在列表序號基礎上加1

代碼如下:

  1. # 線性表(linear list)是數據結構的一種,一個線性表是n個具有相同特性的數據元素的有限序列。 
  2. # 在稍復雜的線性表中,一個數據元素可由多個數據項(item)組成,此種情況下常把數據元素稱為記錄(record),含有大量記錄的線性表又稱文件(file)。 
  3. # 線性表中的個數n定義為線性表的長度,n=0時稱為空表。在非空表中每個數據元素都有一個確定的位置,如用ai表示數據元素,則i稱為數據元素ai在線性表中的位序。 
  4. # 線性表的相鄰元素之間存在著序偶關系。如用(a1,…,ai-1,ai,ai+1,…,an)表示一個順序表,則表中ai-1領先于ai,ai領先于ai+1,稱ai-1是ai的直接前驅元素,ai+1是ai的直接后繼元素。當i=1,2,…,n-1時,ai有且僅有一個直接后繼,當i=2,3,…,n時,ai有且僅有一個直接前驅 [1]  。 
  5. # 1)MakeEmpty(L) 這是一個將L變為空表的方法 
  6. # 2)Length(L) 返回表L的長度,即表中元素個數 
  7. # 3)Get(L,i) 這是一個函數,函數值為L中位置i處的元素(1≤i≤n) 
  8. # 4)Prior(L,i) 取i的前驅元素 
  9. # 5)Next(L,i) 取i的后繼元素 
  10. # 6)Locate(L,x) 這是一個函數,函數值為元素x在L中的位置 
  11. # 7)Insert(L,i,x)在表L的位置i處插入元素x,將原占據位置i的元素及后面的元素都向后推一個位置 
  12. # 8)Delete(L,p) 從表L中刪除位置p處的元素 
  13. # 9)IsEmpty(L) 如果表L為空表(長度為0)則返回true,否則返回false 
  14. # 10)Clear(L)清除所有元素 
  15. # 11)Init(L)同第一個,初始化線性表為空 
  16. # 12)Traverse(L)遍歷輸出所有元素 
  17. # 13)Find(L,x)查找并返回元素 
  18. # 14)Update(L,x)修改元素 
  19. # 15)Sort(L)對所有元素重新按給定的條件排序 
  20. # 16) strstr(string1,string2)用于字符數組的求string1中出現string2的首地址 
  21. class Sequencelist(object): 
  22.     def __init__(self, datatype=int, maxlength=10): 
  23.         self.maxlength = maxlength 
  24.         self.currentnum = 0 
  25.         self.data = [None] * self.maxlength 
  26.         self.datatype = datatype 
  27.  
  28.     def __setitem__(self, key, value): 
  29.         if not isinstance(keyint): 
  30.             raise TypeError 
  31.         if not isinstance(value, self.datatype): 
  32.             raise TypeError("數據類型不符合{0}".format(self.datatype)) 
  33.         if 0 <= key <= self.currentnum: 
  34.             self.data[key-1] = value 
  35.         else
  36.             raise IndexError 
  37.  
  38.     def __getitem__(self, key): 
  39.         if not isinstance(keyint): 
  40.             raise TypeError 
  41.         if 1 <= key <= self.currentnum: 
  42.             return self.data[key-1] 
  43.         else
  44.             raise IndexError 
  45.  
  46.     def __len__(self): 
  47.         return self.currentnum 
  48.  
  49.     def __repr__(self): 
  50.         return '__repr__={}'.format(str(self.data)) 
  51.  
  52.     def __str__(self): 
  53.         return '__str__={}'.format(str(self.data[:self.currentnum])) 
  54.  
  55.     def isempty(self): 
  56.         return self.currentnum == 0 
  57.  
  58.     def isfull(self): 
  59.         return self.currentnum == self.maxlength 
  60.  
  61.     def maxlength(self): 
  62.         return self.maxlength 
  63.  
  64.     def makeempty(self): 
  65.         self.clear() 
  66.  
  67.     def length(self): 
  68.         return self.__len__() 
  69.  
  70.     def count(self): 
  71.         return self.__len__() 
  72.  
  73.     def get(self, key): 
  74.         return self.__getitem__(key
  75.  
  76.     def set(self, key,value): 
  77.         self.__setitem__(key,value) 
  78.  
  79.     def prior(self, key): 
  80.         assert key>1 and key <self.currentnum+1 ,'數組越界' 
  81.         return self.data[key-2] 
  82.  
  83.     def next(self, key): 
  84.         assert key>=1 and key <self.currentnum, '數組越界' 
  85.         return self.data[key
  86.  
  87.     def locate(self, value,start=0): 
  88.         for i in range(start,self.currentnum): 
  89.             if self.data[i]==value: 
  90.                 return i+1 
  91.         raise ValueError("{} is not find in list".format(value)) 
  92.  
  93.     def index(self,value,start=0): 
  94.         return self.locate(value,start) 
  95.  
  96.     def append(self,value): 
  97.         if self.isfull(): 
  98.             print('list is full'
  99.             return 
  100.         else
  101.             self.data[self.currentnum]=value 
  102.             self.currentnum+=1 
  103.  
  104.     def insert(self, key, value): 
  105.         if not isinstance(key,self.datatype): 
  106.             raise TypeError 
  107.         if key<1: 
  108.             raise IndexError 
  109.         if key>=self.currentnum: 
  110.             self.append(value) 
  111.         else
  112.             for i in range(self.currentnum,key-1,-1): 
  113.                 self.data[i]=self.data[i-1] 
  114.             self.data[key-1]=value 
  115.             self.currentnum+=1 
  116.  
  117.     def delete(self, key): 
  118.         if not isinstance(key, self.datatype): 
  119.             raise TypeError 
  120.         if key < 1 and key>self.currentnum: 
  121.             raise IndexError 
  122.         else
  123.             for i in range(key-1,self.currentnum): 
  124.                 self.data[i]=self.data[i+1] 
  125.             self.currentnum-=1 
  126.  
  127.     def pop(self): 
  128.         return self.delete(self.currentnum) 
  129.  
  130.     def clear(self): 
  131.         self.__init__() 
  132.  
  133.     def init(self): 
  134.         self.__init__() 
  135.  
  136.     def reverse(self): 
  137.         i,j=0,self.currentnum-1 
  138.         while i<j: 
  139.             self.data[i],self.data[j]=self.data[j],self.data[i] 
  140.             i,j=i+1,j-1 
  141.             #print(self.data) 
  142.  
  143.     def find(self, value,start=0): 
  144.         return self.locate(self,value,start) 
  145.  
  146.     def update(self, key,value): 
  147.         self.__setitem__(key,value) 
  148.  
  149.     def sort(self): 
  150.         for i in range(0,self.currentnum-1): 
  151.             for j in range(i+1,self.currentnum): 
  152.                 if self.data[i]>self.data[j]: 
  153.                     self.data[i],self.data[j]=self.data[j],self.data[i] 
  154.  
  155.     def strstr(string1, string2): 
  156.         pass 
  157.  
  158.  
  159. if __name__ == '__main__'
  160.     a=Sequencelist() 
  161.     a.append(1) 
  162.     a.append(2) 
  163.     a.append(3) 
  164.     print(a) 
  165.     print(repr(a)) 
  166.     b=a.locate(2) 
  167.     print(b) 
  168.     print(a.isempty()) 
  169.     print(a.isfull()) 
  170.     print(len(a)) 
  171.     print(a.length()) 
  172.     #print(a.prior(1)) 
  173.     # AssertionError: 數組越界 
  174.     print(a.prior(2)) 
  175.     print(a.prior(3)) 
  176.     print(a.next(1)) 
  177.     print(a.next(2)) 
  178.     print(a) 
  179.     print(a.get(2)) 
  180.     a.insert(2,4) 
  181.     print(a) 
  182.     a.delete(2) 
  183.     print(a) 
  184.     print(a.length()) 
  185.     a.pop() 
  186.     print(a) 
  187.     print(a.length()) 
  188.     a.update(2,4) 
  189.     print(a) 
  190.     print(a.index(4)) 
  191.     # print(a.index(5)) 
  192.     # ValueError: 5 is not find in list 
  193.     print(a) 
  194.     a.reverse() 
  195.     print(a) 
  196.     a.append(3) 
  197.     a.append(5) 
  198.     # a.append(2) 
  199.     print(a) 
  200.     a.sort() 
  201.     print(a) 

結果如下:

  1. C:\python\pyproject\pythonalgorithms\venv\Scripts\python.exe C:/python/pyproject/pythonalgorithms/sequencelist.py 
  2. __str__=[1, 2, 3] 
  3. __repr__=[1, 2, 3, None, None, None, None, None, None, None] 
  4. False 
  5. False 
  6. __str__=[1, 2, 3] 
  7. __str__=[1, 4, 2, 3] 
  8. __str__=[1, 2, 3] 
  9. __str__=[1, 2] 
  10. __str__=[1, 4] 
  11. __str__=[1, 4] 
  12. __str__=[4, 1] 
  13. __str__=[4, 1, 3, 5] 
  14. __str__=[1, 3, 4, 5] 
  15.  
  16. Process finished with exit code 0 

 

責任編輯:武曉燕 來源: python與大數據分析
相關推薦

2018-06-06 08:54:23

數據結構存儲

2009-08-12 18:35:17

C#數據結構

2023-11-06 06:43:23

單鏈表查詢數據結構

2009-08-11 14:30:32

C#數據結構與算法

2009-08-11 14:14:42

C#數據結構與算法

2012-04-28 14:21:47

Java數據結構線性結構

2021-04-20 09:18:41

順序存儲結構

2021-05-12 14:09:35

鏈表數據結構線性結構

2021-07-16 07:57:34

Python數據結構

2009-08-11 14:36:17

C#數據結構與算法線性表

2021-07-15 06:43:12

Python數據結構

2021-07-13 07:52:03

Python數據結構

2017-03-01 13:58:46

Python數據結構鏈表

2020-06-09 08:13:15

PHP數據結構

2023-03-13 10:08:31

數據結構算法

2023-03-28 07:44:23

數據結構數組

2017-08-31 09:45:43

JavaArrayList數據

2023-02-08 07:52:36

跳躍表數據結構

2020-05-13 09:14:16

哈希表數據結構

2012-02-02 10:21:05

單鏈表nexthead
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 磐安县| 焦作市| 桂平市| 科技| 南平市| 黑水县| 锡林浩特市| 苏州市| 诏安县| 定远县| 辉南县| 黔西| 天峨县| 清水县| 来宾市| 青阳县| 商南县| 科尔| 舟山市| 惠安县| 明光市| 沭阳县| 东安县| 六安市| 临猗县| 神木县| 灵武市| 崇阳县| 南皮县| 班玛县| 米脂县| 西乌珠穆沁旗| 惠东县| 瓦房店市| 伊宁市| 灵台县| 哈尔滨市| 兴海县| 万全县| 安化县| 安吉县|