python 資料結構之順序列表的實現

yupeng發表於2013-11-03

 

演算法簡要:

  追加直接往列表後面新增元素,插入是將插入位置後的元素全部往後面移動一個位置,然後再將這個元素放到指定的位置,將長度加1刪除是將該位置後面的元素往前移動,

覆蓋該元素,然後再將長度減1  

 

#!/usr/bin/python
# -*- coding: utf-8 -*-


class SeqList(object):
    def __init__(self,maxsize):
        self.maxsize = maxsize
        self.data = range(maxsize)
        self.last = len(self.data) -1

    def __getitem__(self, key):
        if self.is_empty():
            print 'seqlist is empty'
            return
        elif key<0 or key>self.last:
            print 'the given key is Error'
            return
        else:
            return self.data[key]

    def __setitem__(self, key, value):
        if self.is_empty():
            print 'seqlist is empty'
            return
        elif key<0 or key>self.last:
            print 'the given key is Error'
            return
        else:
            self.data[key] = value

    def __len__(self):
        length = self.last + 1
        return length

    def getlength(self):
        return self.last+1

    def clear(self):
        self.data = []

    def is_empty(self):

        if self.last == -1:
            return True
        else:
            return False

    def is_full(self):
        if self.last == self.maxsize-1:
            return True
        else:
            return False

    def getelem(self,index):

        if self.is_empty():
            print 'seqlist is empty'
            return
        elif index<0 or index>self.last:
            print 'position is error'
        else:
            return self.data[index]

    def getindex(self,elem):

        if self.is_empty():
            print 'seqlst is empty'
            return
        else:
            for i in range(self.last):
                if self.data[i]==elem:
                    return i

    def append(self,elem):
        if self.is_empty():
            print 'seqlist is empty'
            return
        else:
            self.last +=1
            self.data  = self.data + [elem]

    def insert(self,index,elem):

        if self.is_empty():
            print 'seqlist is empty'
            return
        elif  index<0 or index> self.last+1:
            print 'postion is error'
            return
        elif index == self.last+1:
            self.last+=1
            self.data  = self.data + [elem]
        else:
            self.data += [elem]
            if index ==0:
                for i in self.data[self.last::-1]:
                    self.data[i+1] = self.data[i]
            else:
                for i in self.data[self.last:index-1:-1]:
                    self.data[i+1] = self.data[i]
            self.data[index] =  elem
            self.last+=1

            #print self.data


    def delete(self,index):

        if self.is_empty():
            print 'seqlist is empty'
            return
        elif  index<0 or index> self.last+1:
            print 'postion is error'
            return
        elif index == self.last+1:
            self.last -= 1
            self.data =self.data[:-1]
        else:

            for i in self.data[:-1]:
                if i >= index:
                    self.data[i] = self.data[i+1]
                else:
                    pass
            self.data = self.data[:-1]
            self.last -= 1

sl = SeqList(5)

print sl.data

sl.append(5)

print sl.data

sl.insert(6,10)

print sl.data

sl.delete(5)

print sl.data
  

說明:其實python中得list 本身是支援該種資料結構的,可以直接使用。

相關文章