Python实现链表增删查改

这篇博客详细介绍了如何使用Python实现链表数据结构,包括创建节点类Node、定义链表类LinkedList以及实现链表的增删查改操作,如添加元素(头插法、尾插法、中间插入)、查找元素、删除元素以及插入元素等。示例代码展示了如何创建和操作单链表。
摘要由CSDN通过智能技术生成

Python实现链表增删查改

#先定一个node的类
class Node():                  #value + next
    def __init__ (self, value = None, next = None):
        self._value = value
        self._next = next
 
    def getValue(self):
        return self._value
 
    def getNext(self):
        return self._next
 
    def setValue(self,new_value):
        self._value = new_value
 
    def setNext(self,new_next):
        self._next = new_next
 
#实现Linked List及其各类操作方法
class LinkedList():
    def __init__(self):      #初始化链表为空表
        self._head = Node() 
        self._tail = None
        self._length = 0
 
    #检测是否为空
    def isEmpty(self):
        return self._head == None  
 
    #add在链表前端添加元素:O(1)
    def add(self,value):
        newnode = Node(value,None)    #create一个node(为了插进一个链表)
        newnode.setNext(self._head)   
        self._head = newnode
 
    #append在链表尾部添加元素:O(n)
    def append(self,value):
        newnode = Node(value)
        if self.isEmpty():
            self._head = newnode   #若为空表,将添加的元素设为第一个元素
        else:
            current = self._head
            while current.getNext() != None:
                current = current.getNext()   #遍历链表
            current.setNext(newnode)   #此时current为链表最后的元素
 
    #search检索元素是否在链表中    
    def search(self,value):
        current=self._head
        foundvalue = False
        while current != None and not foundvalue:
            if current.getValue() == value:
                foundvalue = True
            else:
                current=current.getNext()
        return foundvalue
 
    #index索引元素在链表中的位置
    def index(self,value):
        current = self._head
        count = 0
        found = None
        while current != None and not found:
            count += 1
            if current.getValue()==value:
                found = True
            else:
                current=current.getNext()
        if found:
            return count
        else:
            raise ValueError ('%s is not in linkedlist'%value)
 
    #remove删除链表中的某项元素
    def remove(self,value):
        current = self._head
        pre = None
        while current!=None:
            if current.getValue() == value:
                if not pre:
                    self._head = current.getNext()
                else:
                    pre.setNext(current.getNext())
                break
            else:
                pre = current
                current = current.getNext()
 
    #insert链表中插入元素
    def insert(self,pos,value):
        if pos <= 1:
            self.add(value)
        elif pos > self.size():
            self.append(value)
        else:
            temp = Node(value)
            count = 1
            pre = None
            current = self._head
            while count < pos:
                count += 1
                pre = current
                current = current.getNext()
            pre.setNext(temp)
            temp.setNext(current)

增加元素

class Node:
    def __init__(self,val):
        self.val = val
        self.next = None

class singlelinkedlist:
    def __init__(self,node=None):
        self.__head = node

    def headadd(self,val):
        node = Node(val)
        node.next = self.__head
        self.__head = node

    def tailadd(self,val):
        node = Node(val)
        cur = self.__head
        if cur is None:
            self.headadd(val)
        else:
            while cur.next is not None:
                cur = cur.next
            cur.next = node

    def middleadd(self,val,pos):
        node = Node(val)
        if pos <= 0 :
            self.headadd(val)
        else:
            cur = self.__head
            for i in range(pos-1):
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def walkthrough(self):
        cur = self.__head
        while cur is not None:
            print(cur.val,end=" ")
            cur = cur.next

alink = singlelinkedlist()
alink.headadd(3)
alink.headadd(2)
alink.headadd(1)
alink.tailadd(4)
alink.tailadd(5)
alink.tailadd(6)
alink.middleadd(10,2)
alink.walkthrough()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值