python算法与数据结构004--单向链表的实现

单向链表:每一个节点保存自己的数据和一个next变量指向下一个节点。链表本身只需要存取表头即可。

class Node(object):
    """链表节点"""
    def __init__(self,data,next=None):
        self.data = data
        self.next = next

class SingleLinkedList(object):
    """单向链表"""
    def __init__(self,node = None):
        if node == None:
            self.__head = None
        else :
            self.__head = node

    def insert(self,index,data):
        """在指定位置插入元素"""
        node = Node(data)
        #如果链表为空,直接在表头插入
        if self.__head == None:
            self.__head = node
        else:
            curLen = self.get_length()
            if index < 0 or index > curLen:
                print('输入的插入位置不合法,请重新输入!当前列表长度为: %d',curLen)
                return
            #遍历链表得到第Index个元素
            if index == 0:
                self.__head ,node.next=node,self.__head
            else:
                curIndex = 0
                cur = self.__head
                while curIndex < index-1:
                    cur = cur.next
                    curIndex+=1
                #第index位置插入元素
                cur.next,node.next = node,cur.next

    def append(self,data):
        """尾部插入元素"""
        # 构造一个Node示例并将它添加到链表末尾
        node = Node(data)

        #遍历链表,获取最后一个元素
        if self.__head == None:
            self.__head = node
        else:
            last = self.__head
            while last.next != None:
                last = last.next
            last.next = node

    def insert_front(self,data):
        """链表头插入元素"""
        #构造一个Node实例
        node = Node(data)
        if self.__head == None:
            self.__head = node
        else:
            self.__head,self.__head.next = node,self.__head

    def get_length(self):
        """获得链表长度"""
        cur = self.__head
        len = 0
        while cur != None:
            len+=1
            cur = cur.next
        return len

    def travle(self):
        """遍历单项链表"""
        cur = self.__head
        print('---开始遍历单向链表----')
        while cur != None:
            print(cur.data, end=",")
            cur = cur.next
        print('\n---结束遍历单向链表----')

    def is_empty(self):
        """判断列表是否为空"""
        return self.__head == None

    def seach(self,data):
        """查找某个元素是否在列表中存在"""
        is_find = False
        cur = self.__head
        while cur != None:
            if cur.data == data:
                is_find = True
                break
            else:
                cur = cur.next
        return is_find

    def remove(self,data):
        """查找列表中是否存在某个元素,如果存在则删除"""
        cur = self.__head
        previous = cur = self.__head
        isHead = True
        while cur != None:
            if cur.data == data:
                if isHead:
                    self.__head = cur.next
                else:
                    previous.next = cur.next
                break
            else:
                previous = cur
                cur = cur.next
                isHead = False

if __name__=='__main__':
    sll = SingleLinkedList()
   # print(sll.is_empty())
   # print(sll.get_length())
    sll.append(1)
    sll.append(2)
    sll.insert_front(0)
    sll.insert_front(-1)
    sll.insert(0,-3)
    sll.insert(1,-2)
    sll.insert(6,4)
    sll.insert(6,3)
   # print(sll.is_empty())
   # print(sll.get_length())
    sll.travle()
    print(sll.seach(0))
    print(sll.seach(5))
    sll.travle()
    sll.remove(0)
    sll.travle()
    sll.remove(-3)
    sll.travle()
    sll.remove(4)
    sll.travle()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值