删除队列中的重复元素的C语言算法

时间: 2024-03-26 22:41:10 浏览: 15
在 C 语言中,可以使用类似 Python 中的哈希表实现来删除队列中的重复元素。以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SIZE 100 struct queue { int data[MAX_SIZE]; int front, rear; }; struct hash_table { int *keys; bool *values; int size; }; struct queue *create_queue() { struct queue *q = malloc(sizeof(struct queue)); q->front = -1; q->rear = -1; return q; } struct hash_table *create_hash_table(int size) { struct hash_table *table = malloc(sizeof(struct hash_table)); table->keys = malloc(size * sizeof(int)); table->values = malloc(size * sizeof(bool)); table->size = size; for (int i = 0; i < size; i++) { table->keys[i] = 0; table->values[i] = false; } return table; } void enqueue(struct queue *q, int value) { if (q->rear == MAX_SIZE - 1) { printf("Queue overflow\n"); return; } if (q->front == -1) { q->front = 0; } q->rear++; q->data[q->rear] = value; } int dequeue(struct queue *q) { if (q->front == -1 || q->front > q->rear) { printf("Queue underflow\n"); return -1; } int value = q->data[q->front]; q->front++; return value; } bool is_empty(struct queue *q) { return q->front == -1 || q->front > q->rear; } bool is_full(struct queue *q) { return q->rear == MAX_SIZE - 1; } void delete_queue(struct queue *q) { free(q); } int hash(int key, int size) { return key % size; } void insert(struct hash_table *table, int key) { int index = hash(key, table->size); while (table->values[index] && table->keys[index] != key) { index = hash(index + 1, table->size); } table->keys[index] = key; table->values[index] = true; } bool search(struct hash_table *table, int key) { int index = hash(key, table->size); while (table->values[index]) { if (table->keys[index] == key) { return true; } index = hash(index + 1, table->size); } return false; } void remove_duplicates(struct queue *q) { struct hash_table *table = create_hash_table(MAX_SIZE); struct queue *new_q = create_queue(); while (!is_empty(q)) { int value = dequeue(q); if (!search(table, value)) { insert(table, value); enqueue(new_q, value); } } while (!is_empty(new_q)) { int value = dequeue(new_q); enqueue(q, value); } delete_queue(new_q); free(table->keys); free(table->values); free(table); } int main() { struct queue *q = create_queue(); enqueue(q, 1); enqueue(q, 2); enqueue(q, 3); enqueue(q, 2); enqueue(q, 4); remove_duplicates(q); while (!is_empty(q)) { printf("%d ", dequeue(q)); } printf("\n"); delete_queue(q); return 0; } ``` 这个程序定义了两个数据结构:队列和哈希表。其中哈希表用于判断队列中的元素是否已经出现过。具体实现细节可以参考代码注释。

相关推荐

最新推荐

recommend-type

C语言使用广度优先搜索算法解决迷宫问题(队列)

主要介绍了C语言使用广度优先搜索算法解决迷宫问题,结合迷宫问题分析了C语言队列广度优先搜索算法的相关使用技巧,需要的朋友可以参考下
recommend-type

C++数据结构与算法之双缓存队列实现方法详解

主要介绍了C++数据结构与算法之双缓存队列实现方法,结合实例形式分析了双缓存队列的原理、实现方法与相关注意事项,需要的朋友可以参考下
recommend-type

利用顺序栈逆置循环队列.docx

设计一个算法,用一个栈s将-一个队列Q逆置: (1)要求采用顺序栈和循环队列来实现。 (2)要求采用链栈和链队列来实现。
recommend-type

MATLAB实验一二 数值计算

MATLAB实验一二 数值计算
recommend-type

Java毕业设计-ssm基于SSM的英语学习网站的设计与实现演示录像(高分期末大作业).rar

Java毕业设计-ssm基于SSM的英语学习网站的设计与实现演示录像(高分期末大作业)
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

确保MATLAB回归分析模型的可靠性:诊断与评估的全面指南

![确保MATLAB回归分析模型的可靠性:诊断与评估的全面指南](https://img-blog.csdnimg.cn/img_convert/4b823f2c5b14c1129df0b0031a02ba9b.png) # 1. 回归分析模型的基础** **1.1 回归分析的基本原理** 回归分析是一种统计建模技术,用于确定一个或多个自变量与一个因变量之间的关系。其基本原理是拟合一条曲线或超平面,以最小化因变量与自变量之间的误差平方和。 **1.2 线性回归和非线性回归** 线性回归是一种回归分析模型,其中因变量与自变量之间的关系是线性的。非线性回归模型则用于拟合因变量与自变量之间非
recommend-type

引发C++软件异常的常见原因

1. 内存错误:内存溢出、野指针、内存泄漏等; 2. 数组越界:程序访问了超出数组边界的元素; 3. 逻辑错误:程序设计错误或算法错误; 4. 文件读写错误:文件不存在或无法打开、读写权限不足等; 5. 系统调用错误:系统调用返回异常或调用参数错误; 6. 硬件故障:例如硬盘损坏、内存损坏等; 7. 网络异常:网络连接中断、网络传输中断、网络超时等; 8. 程序异常终止:例如由于未知原因导致程序崩溃等。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。