食材运输c语言地代码
时间: 2024-06-30 17:00:36 浏览: 2
在C语言中,编写一个简单的食材运输管理系统的代码涉及到数据结构(如数组、链表或结构体)以及基本的输入输出操作。这里我们假设创建一个简单的命令行程序,用于模拟运输食材到指定位置的过程。我们将使用结构体来表示食材和运输队列。
```c
#include <stdio.h>
#include <stdlib.h>
// 食材结构体
typedef struct Ingredient {
char name[50];
int quantity;
} Ingredient;
// 队列结构体,使用双向链表实现
typedef struct QueueNode {
Ingredient ingredient;
struct QueueNode* next;
struct QueueNode* prev;
} QueueNode;
typedef struct Queue {
QueueNode* front;
QueueNode* rear;
int size;
} Queue;
// 队列操作
void enqueue(Queue* q, Ingredient i);
void dequeue(Queue* q);
void display(Queue* q);
int main() {
Queue ingredientsQueue;
QueueNode* newNode;
// 初始化队列
enqueue(&ingredientsQueue, {"Tomatoes", 10});
enqueue(&ingredientsQueue, {"Carrots", 5});
enqueue(&ingredientsQueue, {"Potatoes", 8});
printf("Ingredients in the queue:\n");
display(&ingredientsQueue);
// 模拟运输过程
printf("\nTransporting ingredients...\n");
dequeue(&ingredientsQueue);
printf("%s transported.\n", ingredientsQueue.front->ingredient.name);
return 0;
}
// 队列操作实现
void enqueue(Queue* q, Ingredient i) {
newNode = (QueueNode*)malloc(sizeof(QueueNode));
newNode->ingredient = i;
newNode->next = NULL;
newNode->prev = q->rear;
if (q->rear)
q->rear->next = newNode;
else
q->front = newNode;
q->rear = newNode;
q->size++;
}
void dequeue(Queue* q) {
if (q->front == NULL)
return;
QueueNode* temp = q->front;
q->front = q->front->next;
if (q->front)
q->front->prev = NULL;
else
q->rear = NULL;
free(temp);
q->size--;
}
void display(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty.\n");
return;
}
QueueNode* temp = q->front;
while (temp != NULL) {
printf("%s (quantity: %d)\n", temp->ingredient.name, temp->ingredient.quantity);
temp = temp->next;
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)