zoukankan      html  css  js  c++  java
  • 数据结构 之 栈与队列

    队列

    栈的实现

    顺序表实现

    class Stack(object):
        """"""
        def __init__(self):
            self.__list = []  # 选用顺序表或链表
    
        def push(self,item):
            """压栈"""
            self.__list.append(item) # 时间复杂度O(1)
            self.__list.insert(0,item) #头部插入,时间复杂度O(n)
    
            # 说明:链表结构的话,头部插入。
    
        def pop(self):
            return self.__list.pop()
            # return self.__list[0]  #时间复杂度 O(n)
    
        def peek(self):
            """返回栈顶元素"""
            if self.__list:  #不为空
                return self.__list[-1]
            else:
                return None
    
        def is_empty(self):
            """是否为空"""
            return not self.__list
    
    
        def size(self):
            return len(self.__list)
    
    
    if __name__ == '__main__':
        s = Stack()
        s.push(1)
        s.push(3)
        s.push(4)
        s.push(5)
        s.push(6)
        print(s.pop())
        print(s.pop())
        print(s.pop())
        print(s.pop())

    换个姿势,链表实现 队列

    import queue
    
    class Node(object):
        def __init__(self,item):
            self.item = item
            self.next = None
    
    class Queue(object):
        """队列"""
        def __init__(self,node=None):
            self._head = node
    
        def enqueue(self,item):
            """入队"""
            node = Node(item)
            node.next = self._head
            self._head = node
    
        def dequeue(self):
            """出队"""
            cur = self._head
            pre = None
            while cur.next!= None:
                pre = cur
                cur = cur.next
            if cur is self._head:  
                self._head = cur.next
                return cur.item
            pre.next = None
            return cur.item
    
        def is_empty(self):
            """判断是否为空"""
            return self._head == None
    
        def size(self):
            cur = self._head
            index = 0
            while cur!= None:
                cur = cur.next
                index+=1
            return index

    双端队列

    操作:

    #encoding:utf-8
    # __author__ = 'donghao'
    # __time__ = 2019/3/16 17:17
    
    class Deque(object):
        def __init__(self):
            self._list = []
    
        def add_front(self,item):
            self._list.insert(0,item)
    
        def add_end(self,item):
            self._list.append(item)
    
        def pop_front(self):
            return self._list.pop(0)
    
        def pop_end(self):
            return self._list.pop()
    
        def size(self):
            return len(self._list)
    
        def is_empty(self):
            return self._list == []
    
    if __name__ == '__main__':
        que = Deque()
        que.add_end(1)
        que.add_end(2)   # [6,5,4,1,2,3]
        que.add_end(3)
        que.add_front(4)
        que.add_front(5)
        que.add_front(6)
        print(que.pop_front())
        print(que.pop_end())
        print(que.pop_end())
        print(que.pop_end())
  • 相关阅读:
    spring@Async注解实现异步方法调用
    mysql锁机制
    springboot启动时执行任务CommandLineRunner
    java-并发编程之fork/join框架
    mysql explain 执行计划详解
    mysql 时间相关sql , 按天、月、季度、年等条件进行查询
    swagger2 常用注解说明
    VirtualBox 安装CentOS虚拟机网卡配置
    RestFul是啥
    文本内文字字数过多,显示省略号
  • 原文地址:https://www.cnblogs.com/donghaoblogs/p/10543641.html
Copyright © 2011-2022 走看看