C语言用双向链表实现单调递减(递增)队列

参考代码

c 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int num;
	int index;
	struct Node* prev;
	struct Node* next;
}Node;

typedef struct MonoDeQueue {
	int size;
	Node* begin;
	Node* end;
}MonoDeQueue;

void Enqueue(MonoDeQueue* q, int value, int index)
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->num = value;
	node->index = index;
	node->prev = NULL;
	node->next = NULL;

	if (q->size == 0) {
		q->begin = node;
		q->end = node;
		q->size++;
		return;
	}

	Node* cur = q->end;
	Node* temp = NULL;
	//只需将循环条件改为下面的条件,即可实现单调递增队列
	// while (cur && cur->num >= value) {
	while (cur && cur->num <= value) {
		if (q->size == 1) {
			free(cur);
			q->begin = NULL;
			q->end = NULL;
			q->size = 0;
			cur = NULL;
			break;
		}

		temp = cur->prev;
		free(cur);
		cur = temp;
		q->size--;
	}
	
	if (cur) {
        cur->next = NULL;
        q->end = cur;
    }

	if (q->size == 0) {
		q->begin = node;
		q->end = node;
	}
	else {
		q->end->next = node;
		node->prev = q->end;
		q->end = node;
	}
	q->size++;
}

void Dequeue(MonoDeQueue* q)
{
	if (q->size == 0) {
		return;
	}

	if (q->size == 1) {
		free(q->begin);
		q->begin = NULL;
		q->end = NULL;
		q->size = 0;
		return;
	}

	Node* temp = q->begin->next;
	free(q->begin);
	q->begin = temp;
	q->begin->prev = NULL;
	q->size--;

}
void freequeue(MonoDeQueue* q)
{
	if (q->size == 0) {
		return;
	}

	Node* cur = q->begin;
	Node* temp = NULL;
	while (cur) {
		temp = cur->next;
		free(cur);
		cur = temp;
	}
	q->begin = NULL;
	q->end = NULL;
	q->size = 0;
}

void iterator(MonoDeQueue* q)
{
	if (q->size == 0) {
		printf_s("The MonoDeQueue is now empty!!!\r\n");
		return;
	}

	Node* cur = q->begin;
	while (cur) {
		printf_s("%d ", cur->num);
		cur = cur->next;
	}
	printf_s("\r\n");
}

int main()
{
	int num[] = { 5,4,3,2,1,7 };
	MonoDeQueue q = { 0, NULL, NULL };
	for (int i = 0; i < sizeof(num)/sizeof(num[0]); ++i) {
		Enqueue(&q, num[i]);
	}
	iterator(&q);
	clear(&q);
	iterator(&q);
	return 0;
}

运行结果

利用上述实现的单调递减队列求解Leetcode 239. 滑动窗口最大值

c 复制代码
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
    int size = numsSize - k + 1;
    int index = 0;
    int* result = (int*)malloc(sizeof(int) * size);

    MonoDeQueue q = {0, NULL, NULL,};
    for(int i = 0; i < k; i++){
        Enqueue(&q, nums[i], i);
    }
    result[index++] = q.begin->num;

    for(int i = k; i < numsSize; i++){
        if(q.begin->index == i - k){
            Dequeue(&q);
        }
        Enqueue(&q, nums[i], i);
        result[index++] = q.begin->num;
    }

    freequeue(&q);

    *returnSize = size;
    return result;
}

得到

相关推荐
小小龙学IT16 分钟前
Go 泛型(Generics)深度解析:从类型参数到生产实践
开发语言·数据库·golang
hold?fish:palm24 分钟前
45 二叉树的右视图
开发语言·c++·算法
MI_farmer27 分钟前
Anaconda 安装在 D 盘但虚拟环境跑到 C 盘,以及 Conda PermissionError: WinError 5 的解决方法
c语言·开发语言·conda
“AI国潮设计-小江”34 分钟前
[AIGC实战] 基于Stable Diffusion的潮汕非遗IP自动化生成工作流(附Python批量处理脚本)
开发语言·人工智能·python·prompt·aigc
布莱克6051 小时前
在C++中,构造函数能不能是虚函数?析构函数为什么要虚?
开发语言·c++·构造函数·析构函数·虚函数
落魄实习生1 小时前
Agent Scope Java 2.x 系列【1】核心架构
java·开发语言·架构
一水鉴天1 小时前
三套语言:STL SPL/ SQL 20260928(元宝)
开发语言
优质酸酸乳1 小时前
力扣100题之21合并两个有序链表
算法·leetcode·链表
Sarvartha1 小时前
数组基础知识
java·开发语言
泡海椒2 小时前
金融报表开发:jquick-pdf K 线图 PDF 可视化解决方案
java·开发语言·金融·pdf