Python implements the basic functions of an ordered table:
[Cpp] "Algri. py"
Class SeqList (object ):
Def _ init _ (self, length ):
Self. MaxLength = length
Self. _ data = range (length)
Self. Last =-1;
"Indexer get"
Def _ getitem _ (self, key ):
If key> self. Last or key <0:
Print "The given key is Error! "
Return
Else:
Return self. _ data [key]
"Index 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
"Sequence table length"
Def GetLength (self ):
Return self. Last + 1
"Clear sequence table"
Def Clear (self ):
Last =-1
"Judge whether the sequence table is empty"
Def IsEmpty (self ):
If self. Last =-1:
Return True
Else:
Return False
"Determine whether the sequence table is full"
Def IsFull (self ):
If self. Last = self. MaxLength-1:
Return True
Else:
Return False
"Add new elements at the end of the sequence table"
Def Append (self, item ):
If (self. IsFull ()):
Print "List is full"
Else:
Self. Last + = 1
Self. _ data [self. Last] = item
"Insert a data element at position 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
"Deleting the data element at location 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
"Obtain the I data element of the sequence table"
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]
"Find the data element as value in the sequence table"
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;
"Indexer get"
Def _ getitem _ (self, key ):
If key> self. Last or key <0:
Print "The given key is Error! "
Return
Else:
Return self. _ data [key]
"Index 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
"Sequence table length"
Def GetLength (self ):
Return self. Last + 1
"Clear sequence table"
Def Clear (self ):
Last =-1
"Judge whether the sequence table is empty"
Def IsEmpty (self ):
If self. Last =-1:
Return True
Else:
Return False
"Determine whether the sequence table is full"
Def IsFull (self ):
If self. Last = self. MaxLength-1:
Return True
Else:
Return False
"Add new elements at the end of the sequence table"
Def Append (self, item ):
If (self. IsFull ()):
Print "List is full"
Else:
Self. Last + = 1
Self. _ data [self. Last] = item
"Insert a data element at position 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
"Deleting the data element at location 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
"Obtain the I data element of the sequence table"
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]
"Find the data element as value in the sequence table"
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]
From xufei96's column