python数据结构--链表

class Node:                     #定义一个node节点,节点中含有两个参数,一个为节点存的数据,另一个为
    def __init__(self, item):   #该节点的next属性
        self.item = item
        self.next = None


class linkList:				#定义一个链表类
    def __init__(self):     #初始化链表表头为None
        self.head = None



    def isEmpty(self):		#判断链表是否为空有多种方法,第一种是判断链表长度是否为0,第二种是
        return self.head == None     #判断链表的head属性是否为None,第三种我还不知道,欢迎大佬补充




    def add_in_head(self,item):  #头插法  在链表头部加入一个元素
        node=Node(item)          #因为传进来的参数是一个字符串或者数字,所以需要在函数内创建一个
        node.next=self.head      #node对象,对该对象进行操作。后面两个语句意思是把新建的node
        self.head=node			#对象的next属性指向self.head,也就是没插入之前的头节点,
								#然后让新创建的node取代之前的头节点,成为新的头节点
   

	 def add_in_tail(self,item): #尾插法,同样在函数内创建一个node节点,然后判断该链表是否为空
        node = Node(item)		 #如果链表为空,那么插入元素之后,链表内只有一个元素,可以直接
        if self.isEmpty():       #把该节点设置成头节点
            self.head=node
        else:
            currentNode=self.head      			#如果该节点不是头节点的话,循环判断,看当前节点
            while currentNode.next !=None:      #是不是空节点,如果当前节点不是空节点,则查找当前节点
                currentNode=currentNode.next    #的下一个节点是否是空节点,直到当前节点的下一个节点
            currentNode.next=node               #变成空节点,这时可以把创建的节点插入到尾部


    def length(self):
        currentNode=self.head                  #这个应该,应该不用做注释了吧,手累了┓( ´∀` )┏
        count=0
        while currentNode !=None:
            count +=1
            currentNode=currentNode.next
        return  count


    def travel(self):
        currentNode=self.head
        while currentNode !=None:
            print(currentNode.item,end=',')
            currentNode=currentNode.next

    def insert(self,pos,item):
        if pos<=0:
            self.add_in_head(item)
        elif pos>(self.length()-1):
            self.add_in_tail(item)
        else:
            pre=self.head
            node=Node(item)
            count=0
            while count<(pos-1):
                count +=1
                pre=pre.next
            node.next=pre.next
            pre.next=node

    def search(self,item):
        position=[]
        currentNode=self.head
        count=0
        count=int(count)
        num=0
        while currentNode!=None:
            count += 1
            if currentNode.item==item:
                num +=1
                position.append(count)
            currentNode=currentNode.next
        for item in position:
            item +=1
        print('查找到%d个该元素'%num)
        print('它们的位置在:',position)

    def remove(self, item):
        currentNode = self.head
        pre = None
        while currentNode != None:
            if currentNode.item == item:
                if currentNode == self.head:
                    self.head = currentNode.next
                else:
                    pre.next = currentNode.next
                break
            else:
                pre = currentNode
                currentNode = currentNode.next


def show_list():
    print("--------------------单链表综合应用-----------------------------\n")
    print("--------------------1.初始化链表-------------------------------")
    print("--------------------2.查看链表是否为空-------------------------")
    print("--------------------3.头部添加元素-----------------------------")
    print("--------------------4.尾部添加元素-----------------------------")
    print("--------------------5.查看链表长度-----------------------------")
    print("--------------------6.显示链表元素-----------------------------")
    print("--------------------7.在中间插入元素---------------------------")
    print("--------------------8.查找元素---------------------------------")
    print("--------------------9.删除元素---------------------------------")
    print("--------------------0.退出-------------------------------------")


def main():
    while True:
        show_list()
        choice=input('请选择你的操作:')
        choice=int(choice)
        if choice==0:
            print('欢迎再次使用')
            break
        if choice==1:
            li=linkList()
            print('初始化成功')
        if choice==2:
            if li.isEmpty():
                print('链表为空')
            else:
                print('链表不为空')
        if choice==3:
            item=input('需要插入的元素为:')
            li.add_in_head(item)
        if choice==4:
            item = input('需要插入的元素为:')
            li.add_in_tail(item)
        if choice==5:
            print('该链表长度为',li.length())
        if choice==6:
            li.travel()
        if choice==7:
            pos=input('请输入插入的位置')
            pos=int(pos)
            item=input('请输入插入的元素:')
            li.insert(pos,item)
        if choice==8:
            search_item=input('请输入需要查找的内容:')
            li.search(search_item)
        if choice==9:
            remove_item=input('请输入需要删除的内容:')
            li.remove(remove_item)


if __name__ == "__main__":
    main()
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值