链表

来源:互联网 发布:ubuntu提取声卡codec 编辑:程序博客网 时间:2024/06/03 14:20

参考http://www.cnblogs.com/Freec/p/7230063.html



# coding=utf-8class Node(object):    def __init__(self,item):        self.item=item        self.next=Noneclass Singlelinkist(object):    def __init__(self):        self._head=None    def is_empty(self):        return self._head==None    def length(self):        cur=self._head        count=0        while cur!=None:            count+=1            cur=cur.next        return count    def travel(self):        cur=self._head        while cur!=None:            print(cur.item,end='')            cur=cur.next    def add(self,item):#头部添加元素        node=Node(item)#创建一个保存        node.next=self._head        self._head=node    def append(self,item):#尾部添加元素        node=Node(item)        if self.is_empty():            self._head=node        else:            cur=self._head            while cur.next!=None:                cur=cur.next            cur.next=node    def insert(self,pos,item):        if pos<=0:            self.add(item)        elif pos>(self.length()-1):            self.append(item)        else:            node=Node(item)            count=0            pre=self._head            while count<(pos-1):                count+=1                pre= pre.next            node.next=pre.next            pre.next=node    def remove(self,item):        cur=self._head        pre=None        while cur!=None:            if cur.item==item:                if not pre:                    self._head=cur.next                else:                    self._head=cur.next                    break            else:                pre=cur                cur=cur.next    def search(self,item):        cur=self._head        while cur!=None:            if cur.item==item:                return True            else:                return False            cur = cur.next    def print(self):        if self.is_empty():            print('kong')        else:            node=self._head            print('head',node.item)            while node.next:                node=node.next                print('node',node.item)            print('jieshu')if __name__ == '__main__':     #node1=Node(item='node1')     #node2=Node(item='node2')     single=Singlelinkist()     single.append('node1')     single.append('node2')     single.print()     single.insert(1,'sdf')     single.print()     single.search('sdf')     single.length()     single.remove('node1')     single.travel()
# coding=utf-8class Node():    def __init__(self,x,next):        self.val=x        self.next=None    def Creatlist(n):        if n<=0:            return False        if n==1:            return Node(1)        else:            root=Node(1)            tem=root            for i in range(2,n+1):                tem.next=Node(i)                tem=tem.next        return root    def printlist(head):        p=head        while p!=None:            print(p.value)            p=p.next    def listlen(head):        c=0        p=head        while p!=None:            c=c+1            p=p.next        return c    def insertlist(head,n):#在n前面插入        if n<1 or n>listlen(head):            return        p=head        for i in range(1,n-1):            p=p.next        a=input("inter a:")        t=Node(value=a)        t.next=p.next        p.next=t        return head    def dellist(head,n):#删除n节点        if n<1 or n>listlen(head):            return        elif n is 1:            head=head.next        else:            p=head            for i in range(1,n-1):                p=p.next            q=p.next            p.next=q.next                    return head    

原创粉丝点击