Python0125

Python0125总结

1.抽象数据类型(ADP):

拥有属性和方法,属性可以存储数据,方法操作属性

2.线性表

3.链表(插入 删除) 列表(查看)

属性:data-存储数据 next-下一个节点的地址
方法:增删改查

'''
class node():
    def __init__(self,data=None,next=None):
        self.__data = data
        self.__next = next
    def get_data(self):
        return self.__data
    def set_data(self,data):
        self.__data = data
    def get_next(self):
        return self.__next
    def set_next(self,next):
        self.__next = next
'''
class lianbiao():
    def __init__(self,first):
        self.__first = first
    def append(self,data):
        n = node(data)
        if self.__first==None:
            self.__first = n
        else:
            currentNode = self.__first
            while currentNode.get_next()!=None:
                currentNode = currentNode.get_next()
            currentNode.set_next(n)
    def insert(self,data1,data2):
        if self.__first == None:
            print("空链表,没办法插入")
        else:
             currentNode = self.__first
             flag = False
             while currentNode.get_next() != None:
                 if currentNode.get_data()==data1:
                     flag = True
                     break
                 else:
                     currentNode = currentNode.get_next()
             if flag:
                 n = node(data2, currentNode.get_next())
                 currentNode.set_next(n)
             else:
                 if currentNode.get_data()==data1:
                     currentNode.set_next(node(data2))

    def showInfo(self):
        if self.__first == None:
            print("该链表为空")
        else:
            currentNode = self.__first
            while currentNode.get_next() != None:
                print(currentNode.get_data())
                currentNode = currentNode.get_next()
            print(currentNode.get_data())
    def update(self,data1,data2):
        if self.__first == None:
            print("该链表为空")
        else:
            currentNode = self.__first
            flag = False
            while currentNode.get_next() != None:
                if currentNode.get_data()==data1:
                    flag = True
                    break
                else:
                    currentNode = currentNode.get_next()
            if flag:
                currentNode.set_data(data2)
            else:
                if currentNode.get_data()==data1:
                    currentNode.set_data(data2)

l = lianbiao(None)
l.append("zzz")
l.insert("z'z'z","kk")
l.update("zzz","uuu")
l.showInfo()
'''

class first_linked_last():
    def __init__(self,first=None,last=None):
        self.__firzzzfirst
        self.__last = last
    def inser_first(self,data):   #函数方法增添数据data
        n = node(data)        # 定义data节点n
        if self.__first == None:   # 判断第一个是否为空
            self.__first = n       #为空则添加data为第一元素
            self.__last = n        #为空则添加data也可以时最后一个元素
        else:
            n.set_next(self.__first)   #数据后一个地址 指向first(此时first指向第一个元素,也就是first为第一个元素怒)
            self.__first = n
    def insert_last(self,data):
        n = node(data)
        if self.__first == None:
            self.__first = n
            self.__last = n
        else:
            self.__last.set_next(n)
            self.__last = n
    def insert(self,data0,data):
        n = node(data)
        if self.__first == None:
            self.__first = n
            self.__last = n
        else:
            currentnode = self.__first      #定义当前结点为首节点
            flag = False
            while currentnode.get_next()!=None:     #当前节点下一个节点不为空,若为空则此节点为末节点
                if currentnode.get_data()==data0:   # 判断节点是否等于data0
                    flag = True
                    break
                else:
                    currentnode = currentnode.get_next()    #若不等于  当前节点就等于下一个节点
            if flag:
                n.set_next(currentnode.get_next())      #
                currentnode.set_next(n)
            else:
                if currentnode.get_data()==data0:
                    self.insert_last(data)

    def delete_first(self):
        if self.__first == None:
            print("空链表,不能删除")
        else:
            self.__first = self.__first.get_next()
    def delete(self,data):
        if self.__first == None:
            print("空链表,不能删除")
        else:
            currentnode = self.__first
            precurrentnode = self.__first
            flag = False
            while currentnode.get_next!=None:
                if currentnode.get_data()==data:
                    flag = True
                    break
                else:
                    precurrentnode = currentnode
                    currentnode = currentnode.get_next()
            if flag:
                if currentnode == precurrentnode:
                    self.delete_first()
                else:
                    precurrentnode.set_next(currentnode.get_next())
            else:
                if currentnode.get_data() == data:
                    precurrentnode.set_next(None)
                    self.__last = precurrentnode

    def update_first(self,data):
        if self.__first == None:
            print("空链表,不能修改")
        else:
            self.__first.set_data(data)
    def update_last(self,data):
        if self.__first == None:
            print("空链表,不能修改")
        else:
            self.__last.set_data(data)

    def update(self,data0,data):
        if self.__first == None:
            print("空链表,不能修改")
        else:
            currentnode = self.__first
            flag = False
            while currentnode.get_next!=None:
                if currentnode.get_data() == data0:
                    flag = True
                    break
                else:
                    currentnode = currentnode.get_next()
            if flag:
                currentnode.set_data(data)
            else:
                self.update_last(data)
    def show(self):
        if self.__first == None:
            print("该链表为空")
        else:
            currentnode = self.__first
            while currentnode!=None:
                print(currentnode.get_data())
                currentnode = currentnode.get_next()

a = first_linked_last()
a.inser_first("张三")
a.insert_last("李四")
a.insert("张三","zzz")
a.inser_first("王五")
a.update_first("王五一")
a.update_last("李四四")
a.update("张三","张一")
a.delete_first()
a.delete("zzz")
a.show()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值