• python-栈队列


    一、栈  (后进先出)

    栈(stack),有些地方称为堆栈,是一种容器,可存入数据元素、访问元素、删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top)进行加入数据(英语:push)和输出数据(英语:pop)的运算。没有了位置概念,保证任何时候可以访问、删除的元素都是此前最后存入的那个元素,确定了一种默认的访问顺序。

    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
    
        def __str__(self):
            return str(self.data)
    
    
    class Stack:
        def __init__(self):
            self._head = None
    
        # 压栈
        def push(self, item):
            node = Node(item)
            node.next = self._head
            self._head = node
            return node.data
    
        # 弹栈
        def pop(self):
            if self._head == None:
                return None
            else:
                data = self._head.data
                self._head = self._head.next
                return data
    
        # peek 取出栈顶元素
        def peek(self):
            if self._head != None:
                return self._head.data
            else:
                raise ValueError('stack is empty')
    
        def print_all(self):
            cur = self._head
            while cur != None:
                print(cur)
                cur = cur.next
    
    
    if __name__ == '__main__':
        s = Stack()
        s.push(6)
        s.push(1)
        s.push(8)
        s.push(10)
    
        s.print_all()
    
        print('--------pop-------------')
        s.pop()
        s.print_all()
    View Code

    二、队列

    队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。

    队列是一种先进先出的(First In First Out)的线性表,简称FIFO。允许插入的一端为队尾,允许删除的一端为队头。队列不允许在中间部位进行操作!

    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
    
        def __str__(self):
            return str(self.data)
    
    
    class Queue:
        def __init__(self, maxsize=-1):
            self.maxsize = maxsize
            self._head = None
            self._tail = None
    
        # 入队
        def enter(self, data):  # put
            size = self.qsize()
            if self.maxsize != -1 and self.maxsize > size or self.maxsize == -1:
                node = Node(data)
                if self._head == None and self._tail == None:
                    self._head = node
                    self._tail = node
                else:
                    self._tail.next = node
                    self._tail = node
            else:
                raise Exception('queue full ')
    
        # 出队
        def exit(self):  # get()
            if self._head == None and self._tail == None:
                # return None
                raise ValueError('queue is empty')
            data = self._head.data
            if self._head == self._tail:
                self._head = None
                self._tail = None
            else:
                self._head = self._head.next
            return data
    
        def print_all(self):
            cur = self._head
            while cur != None:
                print(cur)
                cur = cur.next
    
        def qsize(self):
            count = 0
            cur = self._head
            while cur != None:
                count += 1
                cur = cur.next
    
            return count
    
        def full(self):
            pass
    
        def empty(self):
            pass
    
    
    if __name__ == '__main__':
        q = Queue(4)
    
        q.enter(6)
        q.enter(4)
        q.enter(2)
    
        q.print_all()
    
        print('---------取出-------------')
        q.exit()
        q.print_all()
    
        print('-----------大小-------')
        q.enter(7)
        q.enter(1)
        # q.enter(9)
        print(q.qsize())
    View Code

     双端队列:是一种具有队列和栈的性质的数据结构。

    双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双端队列可以在队列任意一端入队和出队。

    from collections import deque
    
    class Deque:
        def __init__(self):
            self.items = []
    
        def isEmpty(self):
            return self.items == []
    
        def add_front(self, item):
            # 从头部添加
            self.items.insert(0, item)
    
        def add_rear(self, item):
            self.items.append(item)
    
        def remove_front(self):
            return self.items.pop(0)
    
        def remove_rear(self):
            return self.items.pop()
    
        def size(self):
            return len(self.items)
    
        def print_all(self):
            for item in self.items:
                print(item)
    
    
    if __name__ == '__main__':
        dq = Deque()
        dq.add_front(8)
        dq.add_front(9)
        dq.add_front(0)
    
        dq.print_all()
    
        dq.add_rear(6)
        dq.print_all()
    
        print('-----------remove---------')
        dq.remove_front()
    
        dq.print_all()
    View Code

    三、排序算法

    一路独行,遇见最好的自己!!!
  • 相关阅读:
    idea 快捷键
    python 之网页解析器
    python 之正则表达式
    Python Requests快速入门
    Html style="visibility:hidden"与style="display:none"的区别
    html 之table标签结构学习
    python 之format字符串格式化
    Python 时间戳和日期相互转换
    SQL 字符串函数
    xampp默认配置拿shell
  • 原文地址:https://www.cnblogs.com/rungang/p/11383658.html
Copyright © 2020-2023  润新知