Data structure--sequential table definition and Python implementation of __ data structure

Source: Internet
Author: User
Tags table definition

The sequential table is the sequential storage structure of the linear table. It stores data in a linear table through a set of contiguous storage units, and adjacent two elements are also adjacent to each other in physical locations. For example, the 1th element is stored in the starting position of the linear Table loc (1), then the first element is stored in the LOC (1) + (i-1) *sizeof (elemtype) position, where sizeof (Elemtype) represents the space occupied by each element.


Here is the python implementation of the sequential table:

#coding: Utf-8 ' Author:xzfreewind ' class Seqlist (object): Def __init__ (self,max=10): Self.max = max     #默认顺序表最多容纳10个元素 #初始化顺序表数组 self.num = 0 Self.date = [None] * Self.max def is_empty (self):

    #判定线性表是否为空 return self.num are 0 def is_full (self): #判定线性表是否全满 return self.num is Self.max #获取线性表种某一位置的元素 def __getitem__ (self, i): If not isinstance (i,int): #如果i不为int型, then the decision was entered incorrectly, that is, type error R Aise TypeError If 0<= i < self.num: #如果位置i满足条件, that is, the number of elements in the range, then return the corresponding element value, otherwise, outside the index, return indexerror RET
        Urn Self.date[i] else:raise indexerror #修改线性表种某一位置的元素 def __setitem__ (self, Key, value): If not isinstance (key,int): #如果key不为int型, then the input error is determined, that is, type errors raise TypeError if 0<= key &LT;SELF.N
            Um: #如果位置key满足条件, that is, in the range of elements, returns the corresponding element value, otherwise, outside the index, returns indexerror Self.date[key] = value else:
 Raise Indexerror   #按值查找元素的位置 def getloc (self,value): n = 0 for J in Range (Self.num): if self.date[j] = = V Alue:return J If J = = Self.num:return-1 #如果遍历顺序表还未找到value值相同的元素, then return-1 indicates that the order table species does not have
        Element #统计线性表中元素的个数 def Count (self) for value values: Return self.num #表末尾插入操作 def appendlast (self,value): If Self.num >= self.max:print ' The ' the ' list is full ' return Else:self.da Te[self.num] = value Self.num + 1 #表任意位置插入操作: def insert (Self,i,value): If not isinstance (i, int): Raise TypeError If I < 0 and i > self.num:raise indexerror for J in R Ange (self.num,i,-1): self.date[j] = self.date[j-1] self.date[i] = value Self.num + 1 #删 Operation def Remove (self,i) with the exception of a location: if not isinstance (i,int): Raise TypeError If I < 0 and I &
          Gt;=self.num:  Raise Indexerror for J in Range (I,self.num): self.date[j] = self.date[j+1] self.num-= 1 #输出操作 def printlist (self): for I in Range (0,self.num): Print Self.date[i] #销毁操作 def des Troy (Self): self.__init__ ()


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.