程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 數據結構 C#語言版 第2章 線性表(2)

數據結構 C#語言版 第2章 線性表(2)

編輯:C#入門知識

Python實現一個順序表的基本功能:

[cpp] "Algri.py" 
class SeqList(object): 
    def __init__(self, length): 
        self.MaxLength=length 
        self.__data=range(length) 
        self.Last=-1; 
     
    "索引器get"     
    def __getitem__(self, key): 
        if key>self.Last or key<0: 
            print "The given key is Error!" 
            return 
        else: 
            return self.__data[key] 
     
    "索引器set" 
    def __setitem__(self, key, value): 
        if key>self.Last or key<0: 
            print "The given key is Error!" 
            return 
        else: 
            self.__data[key]=value 
             
    "順序表長度"     
    def GetLength(self): 
        return self.Last+1 
     
    "清空順序表" 
    def Clear(self): 
        last=-1 
     
    "判斷順序表是否為空" 
    def IsEmpty(self): 
        if self.Last==-1: 
            return True 
        else: 
            return False 
     
    "判斷順序表是否為滿" 
    def IsFull(self): 
        if self.Last==self.MaxLength-1: 
            return True 
        else: 
            return False 
     
    "在順序表的末尾添加新元素" 
    def Append(self,item): 
        if(self.IsFull()): 
            print "List is full" 
        else: 
            self.Last+=1 
            self.__data[self.Last]=item 
     
    "在第i個位置插入一個數據元素" 
    def Insert(self,item,i): 
        if(self.IsFull()): 
            print "List is full" 
        elif i<0 or i>self.Last+1: 
            print "Position is error" 
        elif i==self.Last+1: 
            self.Last+=1 
            self.__data[self.Last]=item 
        else: 
            for j in self.__data[self.Last:i-1:-1]: 
                self.__data[j+1]=self.__data[j] 
            self.__data[i]=item 
            self.Last+=1 
     
    "刪除第i個位置的數據元素" 
    def Delete(self,i): 
        if self.IsEmpty(): 
            print "List is empty"      
        elif i<0 or i>self.Last: 
            print "Position is error!" 
        elif i==self.Last: 
            self.Last-=1 
        else: 
            for j in self.__data[i:Last-1]: 
                self.__data[j]=self.__data[j+1] 
            self.Last-=1 
     
    "獲得順序表第i個數據元素" 
    def GetItem(self,i): 
        if(self.IsEmpty() or i<0 or i> self.Last): 
            print "List is empty or position is error!" 
        else: 
            return self.__data[i] 
     
    "在順序表中查找為value的數據元素" 
    def Locate(self,value): 
        if(self.IsEmpty()): 
            print "List is empty!" 
         
        for i in range(0,self.Last): 
            if self.__data[i]==value: 
                return i 
"Algri.py"
class SeqList(object):
    def __init__(self, length):
        self.MaxLength=length
        self.__data=range(length)
        self.Last=-1;
   
    "索引器get"   
    def __getitem__(self, key):
        if key>self.Last or key<0:
            print "The given key is Error!"
            return
        else:
            return self.__data[key]
   
    "索引器set"
    def __setitem__(self, key, value):
        if key>self.Last or key<0:
            print "The given key is Error!"
            return
        else:
            self.__data[key]=value
           
    "順序表長度"   
    def GetLength(self):
        return self.Last+1
   
    "清空順序表"
    def Clear(self):
        last=-1
   
    "判斷順序表是否為空"
    def IsEmpty(self):
        if self.Last==-1:
            return True
        else:
            return False
   
    "判斷順序表是否為滿"
    def IsFull(self):
        if self.Last==self.MaxLength-1:
            return True
        else:
            return False
   
    "在順序表的末尾添加新元素"
    def Append(self,item):
        if(self.IsFull()):
            print "List is full"
        else:
            self.Last+=1
            self.__data[self.Last]=item
   
    "在第i個位置插入一個數據元素"
    def Insert(self,item,i):
        if(self.IsFull()):
            print "List is full"
        elif i<0 or i>self.Last+1:
            print "Position is error"
        elif i==self.Last+1:
            self.Last+=1
            self.__data[self.Last]=item
        else:
            for j in self.__data[self.Last:i-1:-1]:
                self.__data[j+1]=self.__data[j]
            self.__data[i]=item
            self.Last+=1
   
    "刪除第i個位置的數據元素"
    def Delete(self,i):
        if self.IsEmpty():
            print "List is empty"    
        elif i<0 or i>self.Last:
            print "Position is error!"
        elif i==self.Last:
            self.Last-=1
        else:
            for j in self.__data[i:Last-1]:
                self.__data[j]=self.__data[j+1]
            self.Last-=1
   
    "獲得順序表第i個數據元素"
    def GetItem(self,i):
        if(self.IsEmpty() or i<0 or i> self.Last):
            print "List is empty or position is error!"
        else:
            return self.__data[i]
   
    "在順序表中查找為value的數據元素"
    def Locate(self,value):
        if(self.IsEmpty()):
            print "List is empty!"
       
        for i in range(0,self.Last):
            if self.__data[i]==value:
                return i[cpp] "Program.py" 
from Algri import SeqList  
 
seq=SeqList(10) 
for i in range(8): 
    seq.Append(i) 
 
for i in range(8): 
    print seq[i] 
     
seq.Insert(100,5) 
 
for i in range(10): 
    print seq[i] 
 
seq.Append(1000) 
 
for i in range(10): 
    print seq[i] 
     
seq.Delete(9) 
 
for i in range(10): 
    print seq[i] 
     
print seq.Locate(100) 
 
print seq.GetItem(6) 
 
print seq[7] 
 
print seq[9] 

 摘自 xufei96的專欄
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved