删除队列中的重复元素的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;
}
```
这个程序定义了两个数据结构:队列和哈希表。其中哈希表用于判断队列中的元素是否已经出现过。具体实现细节可以参考代码注释。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)