【C】【Queue】双向链表的队列实现

双向链表的队列实现

//双向链队列的实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
	int wait_time;//等待时间
}Item;

typedef struct node {
	Item item;
	struct node* next;//指向下一个指针
	struct node* piror;//指向前一个指针
}Node;

typedef struct {
	Node* rear;//队尾指针
	Node* front;//队首指针
	int node_num;//节点数量
}Queue;

bool InitQueue(Queue* pq);//初始化队列
bool QueueIsEmpty(Queue* pq);//判断队列是否为空
bool QueueIsFull(Queue* pq);//判断队列是否已满
bool InQueue(Queue* pq, int n);//入队
bool OutQueue(Queue* pq);//出队
bool FreeQueue(Queue* pq);//释放队列
void ShowQueue(Queue* pq);//展现队列

bool InitQueue(Queue* pq)
{
	pq->rear = (Node*)malloc(sizeof(Node));
	pq->front = (Node*)malloc(sizeof(Node));
	pq->node_num = 0;
	pq->rear = pq->front;

	if (pq->rear == NULL)
		return false;

	if (pq->front == NULL)
		return false;

	return true;
}

bool QueueIsEmpty(Queue* pq)
{
	if (pq->rear == pq->front)
		return true;
	else return false;
}

bool QueueIsFull(Queue* pq)
{
	Node* pnew;
	pnew = (Node*)malloc(sizeof(Node));
	if (pnew == NULL)
	{
		free(pnew);
		return true;
	}
	else
	{
		free(pnew);
		return false;
	}
}

bool InQueue(Queue* pq, int n)
{
	Node* pnew;
	pnew = (Node*)malloc(sizeof(Node));
	if (pnew == NULL)
		return false;

	pnew->item.wait_time = n;
	if (QueueIsFull(pq) == true)
	{
		printf("队列已满,无法入队!\n");
		return false;
	}
	else if (QueueIsEmpty(pq) == true)
	{
		pq->front->piror = NULL;//队首前面不会排节点
		pq->front->next = pnew;//队首之后是新节点
		pnew->piror = pq->front;//把新节点排在队首后面,必须在前两条之后实现
		pnew->next = NULL;//新节点后面没有节点
		pq->rear = pnew;//新节点作为队尾,必须最后实现
	}
	else
	{
		pq->rear->next = pnew;
		pnew->piror = pq->rear;
		pnew->next = NULL;
		pq->rear = pnew;
	}

	return true;
}

bool OutQueue(Queue* pq)
{
	Node* ptemp;
	
	if (QueueIsEmpty(pq) == true)
	{
		printf("队列为空!出队失败!\n");
		return false;
	}
	else
	{
		ptemp = pq->front;
		pq->front = pq->front->next;
		free(ptemp);
		return true;
	}
}

bool FreeQueue(Queue* pq)
{
	Node* psave = pq->front;

	if (QueueIsEmpty(pq) == true)
	{
		printf("队空,不需要释放内存\n");
		return false;
	}

	while (psave != NULL)
	{
		pq->front = pq->front->next;
		free(psave);
	}
	return true;
}

void ShowQueue(Queue* pq)
{
	Node* pscan;

	if (QueueIsEmpty(pq) == true)
		printf("队列为空!\n");
	else
	{
		pscan = pq->front->next;
		while (pscan != NULL)//rear->next = NULL,rear有内容
		{
			printf("%d ", pscan->item.wait_time);
			pscan = pscan->next;                                                                                                  
		}

		putchar('\n');

		pscan = pq->rear;
		while (pscan != pq->front)//front没内容,且不为空指针
		{
			printf("%d ", pscan->item.wait_time);
			pscan = pscan->piror;
		}
	}
}

int main()
{
	Queue Q;
	Item item;
	
	if (InitQueue(&Q) == true)
		printf("程序运行……\n");
	else
		exit(1);

	while (scanf("%d", &item.wait_time) == 1)
	{
		while (getchar() != '\n')
			continue;
		InQueue(&Q, item.wait_time);
	}

	OutQueue(&Q);

	ShowQueue(&Q);

	FreeQueue(&Q);

	return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 C 语言中,可以使用双向链表实现 FIFO 算法。具体步骤如下: 1. 定义一个结构体表示队列中的节点,包含一个指向前一个节点和后一个节点的指针,以及存储数据的变量。例如: ```c struct node { struct node* prev; struct node* next; int data; }; ``` 2. 定义一个队列结构体,包含队列的头指针和尾指针。例如: ```c struct queue { struct node* head; struct node* tail; }; ``` 3. 初始化队列,将头指针和尾指针都置为 NULL。例如: ```c void init_queue(struct queue* q) { q->head = NULL; q->tail = NULL; } ``` 4. 实现入队操作,即在队列尾部插入一个新节点。首先创建一个新节点,将其前指针指向当前队列的尾节点,将其后指针置为 NULL,将数据存储到节点中。然后将队列的尾指针指向新节点。如果队列为空,还需要将头指针也指向新节点。例如: ```c void enqueue(struct queue* q, int data) { struct node* new_node = malloc(sizeof(struct node)); new_node->prev = q->tail; new_node->next = NULL; new_node->data = data; if (q->tail != NULL) { q->tail->next = new_node; } q->tail = new_node; if (q->head == NULL) { q->head = new_node; } } ``` 5. 实现出队操作,即删除队列头部的节点。首先判断队列是否为空,如果是则直接返回。否则将头节点的后指针指向新的头节点,并将新的头节点的前指针置为 NULL。然后释放原来的头节点的内存。如果队列中只有一个节点,还需要将尾指针也置为 NULL。例如: ```c void dequeue(struct queue* q) { if (q->head == NULL) { return; } struct node* old_head = q->head; q->head = old_head->next; if (q->head != NULL) { q->head->prev = NULL; } free(old_head); if (q->head == NULL) { q->tail = NULL; } } ``` 这样就可以使用双向链表实现 FIFO 算法了。 ### 回答2: FIFO(First In First Out)算法是一种常用的调度算法,它按照任务或数据的到达顺序进行处理。在C语言中,FIFO算法可以通过双向链表实现。下面是实现FIFO算法的C语言代码示例。 1. 首先,我们需要定义一个双向链表的节点结构体,包含数据和指向前后节点的指针。 ```c typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; ``` 2. 接下来,我们定义一个队列结构体,用于管理FIFO算法中的操作。队列结构体包含双向链表的头尾节点指针和队列中元素的数量。 ```c typedef struct Queue { Node* front; Node* rear; int size; } Queue; ``` 3. 实现一个初始化队列的函数,将队列的头尾节点指针设为空,并将队列的元素数量初始化为0。 ```c void initQueue(Queue* queue) { queue->front = NULL; queue->rear = NULL; queue->size = 0; } ``` 4. 实现一个入队的函数,将新节点添加到双向链表的尾部,并更新队列的元素数量和尾节点指针。 ```c void enqueue(Queue* queue, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = queue->rear; newNode->next = NULL; if (queue->front == NULL) { queue->front = newNode; } else { queue->rear->next = newNode; } queue->rear = newNode; queue->size++; } ``` 5. 实现一个出队的函数,从双向链表的头部移除节点,并更新队列的元素数量和头节点指针。 ```c int dequeue(Queue* queue) { if (queue->front == NULL) { printf("Queue is empty!\n"); return -1; } int data = queue->front->data; Node* temp = queue->front; if (queue->front == queue->rear) { queue->front = NULL; queue->rear = NULL; } else { queue->front = queue->front->next; } free(temp); queue->size--; return data; } ``` 通过以上代码,我们就可以利用C语言双向链表实现FIFO算法了。双向链表的特性可以方便地进行头尾节点的添加和删除操作,从而实现FIFO的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值