【数据结构】栈,队列,优先级队列,单向链表的python实现

  • 栈(stack),后进先出(LIFO)

    • 其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,另一端称为栈底。
  • 栈常见的操作

    • push(element): 添加一个新元素到栈顶位置.
    • pop():移除栈顶的元素,同时返回被移除的元素。
    • peek():返回栈顶的元素,不会移除栈顶的元素。
    • isEmpty():如果栈里没有任何元素就返回true,否则返回false
    • clear():移除栈里的所有元素。
    • size():返回栈里的元素个数。
class Stack:
    def __init__(self):
        self.stack = []
        
    def push(self,el): # 添加元素 ,入栈   列表最后为栈顶 
        self.stack.append(el)
        
    def pop(self):
        if len(self.stack) == 0:# 如果队列为空,pop(0)报错  没有元素不能通过索引进行操作
            return None
        return self.stack.pop(-1) # 移除并返回移除的元素
    
    def peek(self):
        if len(self.stack) == 0:
            return None
        return self.stack[-1]  # 只是返回,栈顶的元素
    
    def is_Empty(self):
        return len(self.stack) == 0
    
    def clear(self):
        self.stack = []
        
    def size(self):
        return len(self.stack)
    
    def __str__(self):
        return self.stack

# stack = Stack()  
# stack.push(1) 
# stack.push(2)  
# print(stack.peek()) 
# print(stack.pop()) 
# print(stack.is_Empty()) 
# print(stack.size()) 

队列

  • 队列(Queue)先进先出(FIFO First In First Out)
    • 只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作
  • 队列常见的操作
    • enqueue(element):向队列尾部添加一个(或多个)新的项。
    • dequeue():移除队列的第一项,并返回被移除的元素。
    • front():返回当前队列中第一个元素,不移除元素。
    • isEmpty():如果队列中不包含任何元素,返回true,否则返回false
    • size():返回队列包含的元素个数。

1、普通的队列

class Queue:
    def __init__(self):
        self.queue = []
    def enqueue(self,*el): # 遍历添加多个元素
        for el in el:
            self.queue.append(el)
        
    def dequeue(self):
        if len(self.queue) == 0:  # 如果队列为空,pop(0)报错  没有元素不能通过索引进行操作
            return None
        return self.queue.pop(0)
    def front(self):
        if len(self.queue) == 0:
            return None
        return self.queue[0]
    def isEmpty(self):
        return len(self.queue) == 0
    def size(self):
        return len(self.queue)
    
# queue = Queue()
# print(queue.front())    

2、优先级队列

  • 优先级队列的特点:
    • 比较队列的优先级, 可以得出这个元素正确的队列中的位置。在添加元素时和普通队列不同

3、优先级队列的实现

  • 实现优先级队列相对队列主要有两方面需要考虑:
      1. 封装元素和优先级放在一起(可以封装一个新的类)
      1. 添加元素时, 将当前的优先级和队列中已经存在的元素优先级进行比较, 以获得自己正确的位置。
class PriorityQueue(Queue):  # 这里继承了上面的普通队列类,重写了添加元素方法

    def __init__(self):
        self.queue = []
    def enqueue(self,el,pri):
        class priority:# 创建优先级类,储存优先级和元素
            def __init__(self,el,pri):
                self.el = el
                self.pri = pri
        que = priority(el,pri)
        if len(self.queue)==0:
            self.queue.append(que)
            # print('append')
        else:
            for i in range(len(self.queue)):
                if que.pri >= self.queue[i].pri:  # 从右到左 优先级递减  优先级数字越大优先级越高越靠前 
                    self.queue.insert(i+1,que) # 大就排右边
                    # print('insert')
                    break

                else:
                    self.queue.insert(i,que)  # 小就排左边
                    # print('insert')
                    break  # 执行插入操作之后就跳出循环
                    

    
# pri = PriorityQueue()
# pri.enqueue(12,100)
# pri.enqueue(11,99)
# pri.enqueue(10,98)
# print(pri.front())
# print([i.el for i in pri.queue])

链表

  • 链表是链式的存储多个元素.

    • 链表的每个元素由元素本身和指向下一个元素的引用
    • 链表访问任何一个位置的元素时, 都需要从头开始访问
  • 链表常见的操作

    • append(element):向列表尾部添加一个新的项

    • insert(position, element):向列表的特定位置插入一个新的项。

    • remove(element):从列表中移除一项。

    • indexOf(element):返回元素在链表中的索引。如果列表中没有该元素则返回-1

    • removeAt(position):从列表的特定位置移除一项。

    • isEmpty():如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false

    • size():返回链表包含的元素个数。

1、单向链表实现

class code: # 节点对象 链表内存储的元素
    def __init__(self,el):
        self.el = el
        self.next = None # 指向下一个节点

class LinkedList:
    def __init__(self):
        self.linked_list = None # 链表头元素
    
    def append(self,el):
        x = code(el)
        if self.linked_list is None:
            self.linked_list = x
        else:
            current = self.linked_list
            while current.next: # 如果为空,不再往下找
                current = current.next
            current.next = x # 空的节点指向新的节点
    def insert(self,position,el):
        x = code(el)
        pre = None # 上一节点
        Current = self.linked_list
        if position == 0:
            self.linked_list = x
            x.next = Current
        else:
            for i in range(position):
                pre = Current # 往后推
                Current = Current.next
            pre.next = x 
            x.next = Current
    def remove(self,el):
        pre = None # 上一节点
        current = self.linked_list # 当前节点
        nex = None # 下一节点
        x = code(el)
        if current.el == el:
            self.linked_list = current.next
        else:
            for i in range(self.size()-1): # 遍历节点  第一个节点已经判断,所以遍历次数减一
                pre = current
                current = current.next
                nex = current.next
                if current.el == el:
                    pre.next = nex
    def indexOf(self,el):
        current = self.linked_list
        for i in range(self.size()):
            if current.el == el:
                return i
            else:
                current = current.next
        return -1 # 没找到返回-1
    def removeAt(self,position):
        pre = None
        current = self.linked_list
        nex = current.next
        if position == 0:
            self.linked_list = nex
        else:
            for i in range(position):
                pre = current
                current = current.next
                nex = current.next
            pre.next = nex
    def size(self):
        cont = 0
        current = self.linked_list
        while current: # 如果不为空,就加一,并且向后移
            current = current.next
            cont += 1
        return cont
    def isEmpty(self):
        if self.size() == 0:
            return True
        else:
            return False
    
# link = LinkedList()
# link.append(12)
# link.append(23)
# link.append(34)
# link.append(45)
# link.insert(0,1)
# link.insert(1,2)
# link.insert(3,3333)
# link.remove(23)
# link.removeAt(2)
# print(link.isEmpty())
# print(link.linked_list.el)
# print(link.linked_list.next.el)
# print(link.linked_list.next.next.el)
# print(link.linked_list.next.next.next.el)
# print(link.linked_list.next.next.next.next.el)
# print(link.size())
# print(link.indexOf(45))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值