• 【“栈、队列”的应用】408数据结构代码


    王道数据结构强化课——【“栈、队列”的应用】代码,持续更新
    在这里插入图片描述

    链式存储栈(单链表实现),并基于上述定义,栈顶在链头,实现“出栈、入栈、判空、判满”四个基本操作

    #include 
    #include 
    
    // 定义链表节点
    struct Node {
        int data;
        struct Node* next;
    };
    
    // 定义栈结构
    struct Stack {
        struct Node* top; // 栈顶指针
    };
    
    // 初始化栈
    void initStack(struct Stack* stack) {
        stack->top = NULL;
    }
    
    // 入栈操作
    void push(struct Stack* stack, int value) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        if (newNode == NULL) {
            printf("内存分配失败,无法执行入栈操作\n");
            return;
        }
        newNode->data = value;
        newNode->next = stack->top;
        stack->top = newNode;
    }
    
    // 出栈操作
    int pop(struct Stack* stack) {
        if (stack->top == NULL) {
            printf("栈为空,无法执行出栈操作\n");
            return -1; // 返回一个错误值
        }
        struct Node* temp = stack->top;
        int poppedValue = temp->data;
        stack->top = temp->next;
        free(temp);
        return poppedValue;
    }
    
    // 判空操作
    int isEmpty(struct Stack* stack) {
        return (stack->top == NULL);
    }
    
    // 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
    int isFull(struct Stack* stack) {
        return 0;
    }
    
    // 释放栈内存
    void freeStack(struct Stack* stack) {
        while (stack->top != NULL) {
            struct Node* temp = stack->top;
            stack->top = temp->next;
            free(temp);
        }
    }
    
    int main() {
        struct Stack stack;
        initStack(&stack);
    
        // 入栈操作
        push(&stack, 1);
        push(&stack, 2);
        push(&stack, 3);
    
        // 出栈操作
        printf("出栈操作: %d\n", pop(&stack));
    
        // 判空操作
        printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");
    
        // 判满操作
        printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");
    
        // 释放栈内存
        freeStack(&stack);
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    链式存储栈(双向链表实现)

    #include 
    #include 
    
    // 定义链表节点
    struct Node {
        int data;
        struct Node* next;
        struct Node* prev;
    };
    
    // 定义栈结构
    struct Stack {
        struct Node* top; // 栈顶指针,链尾
    };
    
    // 初始化栈
    void initStack(struct Stack* stack) {
        stack->top = NULL;
    }
    
    // 入栈操作
    void push(struct Stack* stack, int value) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        if (newNode == NULL) {
            printf("内存分配失败,无法执行入栈操作\n");
            return;
        }
        newNode->data = value;
        newNode->next = NULL;
    
        if (stack->top == NULL) {
            newNode->prev = NULL;
            stack->top = newNode;
        } else {
            newNode->prev = stack->top;
            stack->top->next = newNode;
            stack->top = newNode;
        }
    }
    
    // 出栈操作
    int pop(struct Stack* stack) {
        if (stack->top == NULL) {
            printf("栈为空,无法执行出栈操作\n");
            return -1; // 返回一个错误值
        }
        struct Node* temp = stack->top;
        int poppedValue = temp->data;
    
        if (stack->top->prev != NULL) {
            stack->top = stack->top->prev;
            stack->top->next = NULL;
        } else {
            stack->top = NULL;
        }
    
        free(temp);
        return poppedValue;
    }
    
    // 判空操作
    int isEmpty(struct Stack* stack) {
        return (stack->top == NULL);
    }
    
    // 判满操作(对于链式存储的栈,通常不会满,所以返回0表示不满)
    int isFull(struct Stack* stack) {
        return 0;
    }
    
    // 释放栈内存
    void freeStack(struct Stack* stack) {
        while (stack->top != NULL) {
            struct Node* temp = stack->top;
            stack->top = temp->prev;
            free(temp);
        }
    }
    
    int main() {
        struct Stack stack;
        initStack(&stack);
    
        // 入栈操作
        push(&stack, 1);
        push(&stack, 2);
        push(&stack, 3);
    
        // 出栈操作
        printf("出栈操作: %d\n", pop(&stack));
    
        // 判空操作
        printf("栈是否为空: %s\n", isEmpty(&stack) ? "是" : "否");
    
        // 判满操作
        printf("栈是否满: %s\n", isFull(&stack) ? "是" : "否");
    
        // 释放栈内存
        freeStack(&stack);
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    顺序存储的队列(数组实现)

    #include 
    #include 
    
    #define MAX_QUEUE_SIZE 10 // 队列的最大容量
    
    // 定义队列结构
    struct Queue {
        int front, rear; // 前后指针
        int data[MAX_QUEUE_SIZE];
    };
    
    // 初始化队列
    void initQueue(struct Queue* queue) {
        queue->front = -1;
        queue->rear = -1;
    }
    
    // 判空操作
    int isEmpty(struct Queue* queue) {
        return (queue->front == -1);
    }
    
    // 判满操作
    int isFull(struct Queue* queue) {
        return ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front);
    }
    
    // 入队操作
    void enqueue(struct Queue* queue, int value) {
        if (isFull(queue)) {
            printf("队列已满,无法执行入队操作\n");
            return;
        }
        
        if (isEmpty(queue)) {
            queue->front = 0;
        }
    
        queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
        queue->data[queue->rear] = value;
    }
    
    // 出队操作
    int dequeue(struct Queue* queue) {
        if (isEmpty(queue)) {
            printf("队列为空,无法执行出队操作\n");
            return -1; // 返回一个错误值
        }
    
        int dequeuedValue = queue->data[queue->front];
        
        if (queue->front == queue->rear) {
            // 队列中只有一个元素,出队后队列为空
            queue->front = -1;
            queue->rear = -1;
        } else {
            queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
        }
        
        return dequeuedValue;
    }
    
    int main() {
        struct Queue queue;
        initQueue(&queue);
    
        // 入队操作
        enqueue(&queue, 1);
        enqueue(&queue, 2);
        enqueue(&queue, 3);
    
        // 出队操作
        printf("出队操作: %d\n", dequeue(&queue));
    
        // 判空操作
        printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");
    
        // 判满操作
        printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    链式存储队列(单链表实现)

    #include 
    #include 
    
    // 定义链表节点
    struct Node {
        int data;
        struct Node* next;
    };
    
    // 定义队列结构
    struct Queue {
        struct Node* front; // 队列前端
        struct Node* rear;  // 队列后端
    };
    
    // 初始化队列
    void initQueue(struct Queue* queue) {
        queue->front = NULL;
        queue->rear = NULL;
    }
    
    // 判空操作
    int isEmpty(struct Queue* queue) {
        return (queue->front == NULL);
    }
    
    // 判满操作(对于链式存储的队列,通常不会满,所以返回0表示不满)
    int isFull(struct Queue* queue) {
        return 0;
    }
    
    // 入队操作
    void enqueue(struct Queue* queue, int value) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        if (newNode == NULL) {
            printf("内存分配失败,无法执行入队操作\n");
            return;
        }
        newNode->data = value;
        newNode->next = NULL;
        
        if (isEmpty(queue)) {
            queue->front = newNode;
        } else {
            queue->rear->next = newNode;
        }
        
        queue->rear = newNode;
    }
    
    // 出队操作
    int dequeue(struct Queue* queue) {
        if (isEmpty(queue)) {
            printf("队列为空,无法执行出队操作\n");
            return -1; // 返回一个错误值
        }
        
        struct Node* temp = queue->front;
        int dequeuedValue = temp->data;
        
        queue->front = temp->next;
        free(temp);
        
        if (queue->front == NULL) {
            // 如果出队后队列为空,需要更新rear指针
            queue->rear = NULL;
        }
        
        return dequeuedValue;
    }
    
    // 释放队列内存
    void freeQueue(struct Queue* queue) {
        while (queue->front != NULL) {
            struct Node* temp = queue->front;
            queue->front = temp->next;
            free(temp);
        }
    }
    
    int main() {
        struct Queue queue;
        initQueue(&queue);
    
        // 入队操作
        enqueue(&queue, 1);
        enqueue(&queue, 2);
        enqueue(&queue, 3);
    
        // 出队操作
        printf("出队操作: %d\n", dequeue(&queue));
    
        // 判空操作
        printf("队列是否为空: %s\n", isEmpty(&queue) ? "是" : "否");
    
        // 判满操作
        printf("队列是否满: %s\n", isFull(&queue) ? "是" : "否");
    
        // 释放队列内存
        freeQueue(&queue);
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
  • 相关阅读:
    【杂记-浅谈端口号和协议号】
    DOE认证是什么
    网络安全的规划
    java计算机毕业设计疫情防控管理系统MyBatis+系统+LW文档+源码+调试部署
    高效掌握JDBC技术(二)| 掌握ORM思想 | 定义连接数据库的工具类
    ATT&CK初步了解
    第五章 目标检测中K-means聚类生成Anchor box(工具)
    idea使用gradle教程 (idea gradle springboot)2024
    milvus和相似度检索
    虾皮物流价格是多少?如何计算?
  • 原文地址:https://blog.csdn.net/weixin_51506598/article/details/133578530