编写程序,用不同的储存方法,实现队列的基本操作
时间: 2024-05-14 19:11:55 浏览: 13
以下是Python语言实现队列的基本操作的示例代码,包括使用列表、双端队列和队列模块:
使用列表:
```python
queue = []
# 入队
queue.append(1)
queue.append(2)
queue.append(3)
# 出队
print(queue.pop(0)) # 输出:1
print(queue.pop(0)) # 输出:2
print(queue.pop(0)) # 输出:3
```
使用双端队列:
```python
from collections import deque
queue = deque()
# 入队
queue.append(1)
queue.append(2)
queue.append(3)
# 出队
print(queue.popleft()) # 输出:1
print(queue.popleft()) # 输出:2
print(queue.popleft()) # 输出:3
```
使用队列模块:
```python
from queue import Queue
queue = Queue()
# 入队
queue.put(1)
queue.put(2)
queue.put(3)
# 出队
print(queue.get()) # 输出:1
print(queue.get()) # 输出:2
print(queue.get()) # 输出:3
```
相关问题
Linux编写程序实现接收消息队列中类型为6的消息
可以使用Linux系统提供的消息队列相关API来实现接收消息队列中类型为6的消息,具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
#define MSG_TYPE 6
typedef struct {
long type;
char data[1024];
} msg_t;
int main() {
int msgid;
msg_t msg;
// 创建消息队列
msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
// 接收类型为6的消息
msgrcv(msgid, &msg, sizeof(msg_t), MSG_TYPE, 0);
// 输出接收到的消息内容
printf("接收到类型为6的消息:%s\n", msg.data);
// 删除消息队列
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
```
这段代码首先创建了一个消息队列,然后调用msgrcv函数接收类型为6的消息,最后输出接收到的消息内容。你可以将这段代码保存为一个C文件,然后使用gcc命令编译并运行它。当有类型为6的消息发送到该消息队列时,这个程序就会接收到消息并输出其内容。
用图编写程序实现深度优先搜索和广度优先搜索算法
下面是 Python 代码实现深度优先搜索和广度优先搜索算法:
深度优先搜索算法:
```python
def dfs(graph, start):
visited = set() # 用集合保存已经访问过的节点
stack = [start] # 用栈保存待访问的节点
while stack:
node = stack.pop() # 弹出栈顶节点
if node not in visited: # 如果该节点还没有被访问过
visited.add(node) # 将该节点加入已访问节点集合中
stack.extend(graph[node] - visited) # 将该节点的未访问邻居节点入栈
return visited
```
广度优先搜索算法:
```python
from collections import deque
def bfs(graph, start):
visited = set() # 用集合保存已经访问过的节点
queue = deque([start]) # 用队列保存待访问的节点
while queue:
node = queue.popleft() # 弹出队头节点
if node not in visited: # 如果该节点还没有被访问过
visited.add(node) # 将该节点加入已访问节点集合中
queue.extend(graph[node] - visited) # 将该节点的未访问邻居节点入队
return visited
```
其中,`graph` 参数是一个字典,表示图的邻接关系,例如:
```python
graph = {
"A": {"B", "C"},
"B": {"A", "D", "E"},
"C": {"A", "F"},
"D": {"B"},
"E": {"B", "F"},
"F": {"C", "E"}
}
```
上述代码中,我们使用了 Python 标准库中的集合(set)和双向队列(deque)来实现深度优先搜索和广度优先搜索算法。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)