栈的实现--python

1. 使用顺序表实现 栈

class Stack(object):
    def __init__(self):
        self.__list=[]

    def is_empty(self):
        return self.__list==[]

    def push(self,item):
        self.__list.append(item)

    def pop(self):
        return self.__list.pop()

    def peek(self):
        #返回栈顶元素
        if self.__list:
            return self.__list[-1]
        else:
            return None
    def size(self):
        return len(self.__list)


if __name__ == '__main__':
    s=Stack()
    s.push(1)
    s.push(2)
    s.push(3)
    s.push(4)
    print(s.pop())
    print(s.pop())
    print(s.pop())
    print(s.pop())

2. 使用链表实现 栈

class Node(object):   #节点
    def __init__(self,elem):
        self.elem=elem
        self.next=None

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

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

    def push(self,item):
        node=Node(item)
        cur=self.__head
        if self.is_empty():
            self.__head=node
        else:
            while cur.next!=None:
                cur=cur.next
            cur.next=node

    def pop(self):
        if self.is_empty():
            return
        else:
            cur=self.__head
            pre=None
            while cur.next!=None:
                pre=cur
                cur=cur.next
            pre.next=cur.next



    def size(self):
        cur=self.__head
        cound=0
        while cur.next!=None:
            cound+=1
            cur=cur.next
        return cound


if __name__ == '__main__':
    s=Stack()
    s.push(1)
    s.push(2)
    s.push(3)
    s.pop()

    print(s.size())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
求解算法? 可以使用深度优先搜索算法(DFS)来求解迷宫。具体实现方法是使用来保存走过的路径,当搜索到终点时,中的路径就是一条正确的路径。以下是 Python 实现迷宫求解的示例代码: ```python # 定义迷宫 maze = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0, 1], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]] # 定义节点 class StackNode: def __init__(self, x, y, next=None): self.x = x self.y = y self.next = next # 定义 class Stack: def __init__(self): self.head = None def push(self, x, y): node = StackNode(x, y, self.head) self.head = node def pop(self): if self.head is None: return None node = self.head self.head = node.next return node def is_empty(self): return self.head is None # 定义迷宫求解函数 def solve_maze(maze): start_x, start_y = 1, 1 # 起点 end_x, end_y = 5, 6 # 终点 stack = Stack() stack.push(start_x, start_y) directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上 while not stack.is_empty(): node = stack.pop() x, y = node.x, node.y for dx, dy in directions: nx, ny = x + dx, y + dy if maze[nx][ny] == 0: # 未走过的路径 stack.push(nx, ny) maze[nx][ny] = maze[x][y] + 1 # 记录步数 if nx == end_x and ny == end_y: # 到达终点 return maze[nx][ny] return None # 没有找到正确路径 # 输出迷宫求解结果 steps = solve_maze(maze) if steps is None: print("没有找到正确路径") else: print(steps) ``` 这段代码中,我们首先定义了一个迷宫,使用 0 表示通路,使用 1 表示墙,然后定义了一个节点和一个数据结构,节点保存迷宫中的一个位置,用于保存路径。在迷宫求解函数中,我们首先将起点入,然后使用深度优先搜索算法不断向前寻找可走的路径,直到找到终点或者为空时停止。当搜索到一个节点时,我们将其未走过的路径入,并记录到达该节点时的步数。如果最终能够到达终点,则返回到达终点的步数。如果没有找到正确的路径,则返回 None。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值