python 实现单向链表、单向循环链表、双向链表

python 实现单向链表、单向循环链表、双向链表

单向链表

单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。
在这里插入图片描述

  1. 表元素域data用来存放具体的数据。
  2. 链接域next用来存放下一个节点的位置(python中的标识)
  3. 变量p指向链表的头节点(首节点)的位置,从p出发能找到表中的任意节点。

单向链表操作

  1. is_empty() 链表是否为空
  2. length() 链表长度
  3. travel() 遍历整个链表
  4. add(item) 链表头部添加元素
  5. append(item) 链表尾部添加元素
  6. insert(pos, item) 指定位置添加元素
  7. remove(item) 删除节点
  8. search(item) 查找节点是否存在
class Node(object):
    def __init__(self,data,next=None):
        self.data = data
        self.next = next

class Singlelinklist(object):
    def __init__(self,node):
        self._head = Node(None)

    def is_empty(self):
        return self._head == None

    def length(self):
        lengthnum = 1
        cur = self._head
        while cur != None:
            cur = cur.next
            lengthnum +=1
        return  lengthnum

    def travel(self):
        cur = self._head
        while cur != None:
            print(cur.data)
            cur = cur.next

    def add(self,adddata):
        p = Node(data=adddata)
        p.next = self._head
        self._head = p

    def append(self,appneddata):
        p = Node(data=appneddata)
        if self.is_empty():
            self._head = p
        else:
            cur = self._head
            while  cur.next != None:
                cur = cur.next
            cur.next = p

    def insert(self,insertdata,pos):
        p = Node(data=insertdata)
        if  pos<=0 :
            self.add(insertdata)
        if pos >= self.length():
            self.append(insertdata)
        if 1 < pos <self.length():
            cur = self._head
            nowpos = 0
            while  nowpos < (pos-1):
                cur = cur.next
                nowpos +=1
            p.next = cur.next
            cur.next = p

    def remove(self,removedata):
        cur = self._head
        pre = None
        while cur != None:
            if cur.data == removedata:
                if not pre:
                    self._head = cur.next
                else:
                    pre.next = cur.next
            else:
                pre = cur
                cur = cur.next

    def search(self,searchdata):
        if self.is_empty():
            return '空链表'
        cur = self._head
        while cur != None :
            if cur.data == searchdata:
                return '找到'
            else:
                cur = cur.next

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

单向循环链表

#单向循环链表
class singleCycLinklist(object):
    def __init__(self):
        self._head = Node(None)

    def is_empty(self):
        return self._head == None

    def length(self):
        lengthnum = 1
        if self.is_empty():
            return 0
        else:
            cur = self._head
            while  cur.next != self._head:
                cur = cur.next
                lengthnum+=1
        return  lengthnum

    def travel(self):
        if self.is_empty():
            return None
        else:
            cur =self._head
            while cur.next != self._head:
                cur = cur.next
                print(cur.data)

    def add(self,adddata):
        p = Node(adddata)
        if self.is_empty():
            self._head = p
            p.next = self._head
        else:
            p.next = self._head
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = p
            self._head = p

    def append(self,appenddata):
        p = Node(appenddata)
        if self.is_empty():
            self._head = p
            p.next = self._head
        else:
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = p
            p.next = self._head

    def insert(self, pos, insertdata):
        p = Node(insertdata)
        if self.is_empty():
            self._head = p
            p.next = self._head
        elif pos <= 0 :
            self.add(insertdata)
        elif  pos>= self.length():
            self.append(insertdata)
        elif  0<pos<self.length():
            num = 0
            cur = self._head
            while num < (pos-1):
                cur = cur.next
                num +=1
            p.next = cur.next
            cur.next = p

    def remove(self,removedata):
        """删除一个节点"""
        # 若链表为空,则直接返回
        if self.is_empty():
            return
        # 将cur指向头节点
        cur = self._head
        pre = None
        # 若头节点的元素就是要查找的元素item
        if cur.data == removedata:
            # 如果链表不止一个节点
            if cur.next != self._head:
                # 先找到尾节点,将尾节点的next指向第二个节点
                while cur.next != self._head:
                    cur = cur.next
                # cur指向了尾节点
                cur.next = self._head.next
                self._head = self._head.next
            else:
                # 链表只有一个节点
                self._head = None
        else:
            pre = self._head
            # 第一个节点不是要删除的
            while cur.next != self._head:
                # 找到了要删除的元素
                if cur.data == removedata:
                    # 删除
                    pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            # cur 指向尾节点
            if cur.data == removedata:
                # 尾部删除
                pre.next = cur.next

    def search(self,searchdata):
        if self.is_empty():
            return False
        cur = self._head
        if cur.data == searchdata:
            return True
        while cur.next != self._head:
            cur = cur.next
            if cur.data == searchdata:
                return True
        return False

在这里插入图片描述

双向链表

在这里插入图片描述

class DNode(object):
    def __init__(self, data, next=None, pre=None):
        self.data = data
        self.next = next
        self.pre = pre

class DLinklist(object):
    def __init__(self):
        self._head = DNode(None)

    def is_empty(self):
        return  self._head == None

    def length(self):
        cur = self._head
        lengthnum = 0
        if self.is_empty():
            return 0
        while cur != None:
            cur = cur.next
            lengthnum += 1
        return lengthnum

    def travrel(self):
         cur = self._head
         if self.is_empty():
             return None
         while cur != None:
             cur  = cur.next
             print(cur.data)

    def add(self, adddata):
        p = DNode(adddata)
        if self.is_empty():
            self._head = None
        else:
            p.next = self._head
            self._head.pre = p
            self._head = p

    def append(self, appenddata):
        p = DNode(appenddata)
        if self.is_empty():
            self._head = p
        else:
            cur = self._head
            while cur != None:
                cur = cur.next
            cur.next = p
            p.pre = cur

    def insert(self, insertdata, pos):
        p = DNode(insertdata)

        if  pos <=0:
            self.add(insertdata)
        if pos > (self.length()-1):
            self.append(insertdata)
        if (self.length()-1)>=pos>0:
            num = 0
            cur =self._head
            while num <pos:
                cur = cur.next

            p.next = cur.next
            p.pre = cur

            cur.next.pre = p
            cur.next = p

    def remove(self, removedata):
        p = DNode(removedata)
        if self.is_empty():
            return  None

        else:
            cur = self._head
            #位于句首
            if cur.data == p.data :
                if cur.next == None:
                    self._head = None
                else:
                    cur.next.pre = None
                    self._head = cur.next
            else:
                while cur != None:
                    if cur.data == p.data:
                        cur.pre.next = cur.next
                        cur.next.pre = cur.pre
                        break
                    cur = cur.next

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值