91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

c語言列隊怎樣實現先進先出

小樊
91
2024-10-16 22:45:38
欄目: 編程語言

在C語言中,實現隊列的先進先出(FIFO)可以通過結構體、數組和鏈表三種數據結構來實現。這里我將分別介紹這三種方法:

  1. 使用結構體實現隊列:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int *data;
    int front;
    int rear;
    int size;
    int capacity;
} Queue;

Queue* createQueue(int capacity) {
    Queue* queue = (Queue*)malloc(sizeof(Queue));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
    queue->rear = capacity - 1;
    queue->data = (int*)malloc(queue->capacity * sizeof(int));
    return queue;
}

int isFull(Queue* queue) {
    return (queue->size == queue->capacity);
}

int isEmpty(Queue* queue) {
    return (queue->size == 0);
}

void enqueue(Queue* queue, int item) {
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->data[queue->rear] = item;
    queue->size = queue->size + 1;
}

int dequeue(Queue* queue) {
    if (isEmpty(queue))
        return -1;
    int item = queue->data[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}

int main() {
    Queue* queue = createQueue(5);
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
    enqueue(queue, 50);

    while (!isEmpty(queue)) {
        printf("%d ", dequeue(queue));
    }

    free(queue->data);
    free(queue);
    return 0;
}
  1. 使用數組實現隊列:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int *array;
    int front;
    int rear;
    int size;
    int capacity;
} QueueArray;

QueueArray* createQueueArray(int capacity) {
    QueueArray* queue = (QueueArray*)malloc(sizeof(QueueArray));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
    queue->rear = capacity - 1;
    queue->array = (int*)malloc(queue->capacity * sizeof(int));
    return queue;
}

int isFull(QueueArray* queue) {
    return (queue->size == queue->capacity);
}

int isEmpty(QueueArray* queue) {
    return (queue->size == 0);
}

void enqueue(QueueArray* queue, int item) {
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->array[queue->rear] = item;
    queue->size = queue->size + 1;
}

int dequeue(QueueArray* queue) {
    if (isEmpty(queue))
        return -1;
    int item = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}

int main() {
    QueueArray* queue = createQueueArray(5);
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
    enqueue(queue, 50);

    while (!isEmpty(queue)) {
        printf("%d ", dequeue(queue));
    }

    free(queue->array);
    free(queue);
    return 0;
}
  1. 使用鏈表實現隊列:
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

typedef struct Queue {
    Node* front;
    Node* rear;
    int size;
} QueueLinkedList;

QueueLinkedList* createQueue() {
    QueueLinkedList* queue = (QueueLinkedList*)malloc(sizeof(QueueLinkedList));
    queue->front = queue->rear = NULL;
    queue->size = 0;
    return queue;
}

void enqueue(QueueLinkedList* queue, int item) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = item;
    newNode->next = NULL;

    if (queue->rear == NULL) {
        queue->front = queue->rear = newNode;
        return;
    }

    queue->rear->next = newNode;
    queue->rear = newNode;
}

int dequeue(QueueLinkedList* queue) {
    if (queue->front == NULL)
        return -1;

    Node* temp = queue->front;
    int item = temp->data;
    queue->front = queue->front->next;

    if (queue->front == NULL)
        queue->rear = NULL;

    free(temp);
    queue->size = queue->size - 1;
    return item;
}

int main() {
    QueueLinkedList* queue = createQueue();
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
    enqueue(queue, 50);

    while (!isEmpty(queue)) {
        printf("%d ", dequeue(queue));
    }

    return 0;
}

以上三種方法都可以實現隊列的先進先出(FIFO)。

0
慈利县| 高密市| 陇川县| 交城县| 旬邑县| 九台市| 凤阳县| 神农架林区| 镇坪县| 洪泽县| 西乌珠穆沁旗| 武宣县| 进贤县| 黄浦区| 达州市| 四平市| 临城县| 云南省| 邹城市| 安国市| 昌邑市| 特克斯县| 尤溪县| 和平区| 郯城县| 新田县| 页游| 绥芬河市| 青州市| 西华县| 恩施市| 子洲县| 肥乡县| 右玉县| 车致| 苏尼特左旗| 平罗县| 石屏县| 沙河市| 安塞县| 合江县|