python2.7实现链表

1.基本链表实现:

class Chain:
    def __init__(self,data,pnext=None):
        self.data=data
        self.pnext=pnext

if __name__=='__main__':
    chain=Chain('A',Chain('B',Chain('C',Chain('D'))))
    while True:
        print chain.data
        if chain.pnext!=None:
            chain=chain.pnext
        else:
            break

2.对链表的操作:

class Node:
    def __init__(self,data,pnext=None):
        self.data=data
        self.pnext=pnext

class Chain:
    def __init__(self):
        self.head=None
        self.length=0
    #判断链表是否为空
    def isEmpty(self):
        return self.head==None
    #在链表尾部插入元素
    def append(self,item):
        if isinstance(item,Node):
            node=item
        else:
            node=Node(item)

        if self.head==None:
            self.head=node
            self.length+=1
        else:
            node_now=self.head
            while node_now.pnext:
                node_now=node_now.pnext
            node_now.pnext=node
            self.length=self.length+1
    #在第index个元素后插入元素
    def insert(self,index,item):
        if isinstance(item,Node):
            node=item
        else:
            node=Node(item)
        if index<=0 or index>self.length:
            print 'error:out of index'
            return

        node_now=self.head
        #count=0
        for i in range(index-1):
            node_now=node_now.pnext
        node.pnext=node_now.pnext
        node_now.pnext=node
        self.length+=1
    #删除第index元素
    def delete(self,index):
        if self.isEmpty():
            print 'chain is empty'
            return
        if index<=0 or index>self.length:
            print 'out of index'
            return
        node_now=self.head
        count=1
        if index==1:
            self.head=node_now.pnext
            self.length-=1
        else:
            while True:
                count += 1
                if count==index:
                    node_now.pnext=node_now.pnext.pnext
                    self.length-=1
                    return
                node_now=node_now.pnext
    #得到第index的数
    def getitem(self,index):
        if index<1 or index>self.length:
            print 'out of index'
            return
        elif index==1:
            return self.head.data
        else:
            count=1
            node_now=self.head
            while True:
                if count==index:
                    return node_now.data
                node_now=node_now.pnext
                count+=1
    #清楚链表
    def clearchain(self):
        self.head=None
        self.length=0



if __name__=='__main__':
    chain=Chain()
    chain.append('A')
    chain.append('B')
    chain.append('C')
    chain.insert(3,'a')
    chain.insert(4,'b')
    chain.insert(5,'c')
    chain.delete(5)
    #chain.clearchain()

    #print chain.head.data
    '''while True:
        print chain.data
        if chain.pnext!=None:
            chain=chain.pnext
        else:
            break'''
    mynode=chain.head
    print chain.length
    while mynode:#for i in range(chain.length):
        print mynode.data
        mynode=mynode.pnext

    #print chain.getitem(5)




你可能感兴趣的:(数据结构)