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

这篇具有很好参考价值的文章主要介绍了【“栈、队列”的应用】408数据结构代码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

王道数据结构强化课——【“栈、队列”的应用】代码,持续更新
【“栈、队列”的应用】408数据结构代码,学业课程,数据结构,考研文章来源地址https://www.toymoban.com/news/detail-728857.html

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

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
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;
}

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

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
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;
}

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

#include <stdio.h>
#include <stdlib.h>

#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;
}

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

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点
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;
}

到了这里,关于【“栈、队列”的应用】408数据结构代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【头歌】数据结构-队列的应用

      第1关:循环队列 任务描述 本关任务:编写一个循环队列,实现入队、出队操作,判断队空、队满等特殊情况。 相关知识 为了完成本关任务,你需要掌握:1.循环队列定义,2.入队、出队的定义,3.队空、队满的情况。 循环队列定义 循环队列将数组存储区看成是一个首尾相

    2024年02月08日
    浏览(33)
  • 【数据结构】栈和队列的应用

    🌈积薪高于山,焉用先后别 🌈   🌟 正式开始学习数据结构啦~此专栏作为学习过程中的记录 🌟 对于编译器来说,我们在大多数 I D E IDE I D E 内进行编码时,都会提示括号的匹配标志,可能用不同颜色或者距离差加以区分,那么,编译器中是如何实现这些操作的呢? 其实

    2024年02月10日
    浏览(34)
  • 数据结构与算法课程设计---最小生成树的应用

    1.问题 假定有这么一个问题,有11个城市,城市之间有一些天然气管道,铺设天然气管道需要花费不同的金额,现在要你选择其中一些天然气管道,使得所有城市可以互相联通且花费最小。 2.分析 我们把这个问题抽象为一张图,每个城市是一个顶点,城市与城市之间的管道是

    2024年02月08日
    浏览(37)
  • 数据库原理课程设计 — 学业课程预警系统

    一、选题背景 21世纪的社会可谓日新月异,科学技术突飞猛进,经济知识和信息产业初见端倪,特别是信息技术和网络技术的讯速发展和广泛应用,对社会的政治、经济、军事、文化等领域产生越来越深刻的影响。学校也不例外地快速发展着,而且要求也在不断变化。学生的

    2024年02月13日
    浏览(36)
  • 深入理解数据结构:队列的实现及其应用场景

    队列(Queue)是一种具有先进先出(FIFO)特性的数据结构。在队列中,数据的插入和删除操作分别在队列的两端进行。插入操作在队列的尾部进行,而删除操作则在队列的头部进行。这种特性使得队列在很多实际应用中非常有用,比如任务调度、缓冲区管理等。 线性表是一种

    2024年04月28日
    浏览(37)
  • 数据结构 - 堆(优先队列)+ 堆的应用 + 堆练习

    1、本文章适合新学和复习用,都是用c语言实现的,包含了堆的讲解、堆的应用、堆的练习。 2、有图解和代码都注释,放心食用哦 那么开始: 一、什么是堆 堆(Heap)是计算机科学中一类特殊的数据结构,是最高效的优先级队列。堆通常是一个可以被看作一棵完全二叉树的数组

    2024年03月11日
    浏览(31)
  • 数据结构例题代码及其讲解-栈与队列

    栈Stack 后进先出 ​ 栈的结构体定义及基本操作。 初始化 ​ 这里初始化时是将栈顶指针指向-1,有些则是指向0,因此后续入栈出栈的代码略微有点区别 判断栈是否为空 压栈操作 由于初始时栈顶指针指向-1,因此需要先变化栈顶指针,然后入栈操作; 且当MaxSize为50时候,数

    2024年02月10日
    浏览(34)
  • 数据结构之队列(源代码➕图解➕习题)

            在学过栈之后,会了解到栈的底层是根据顺序表或者链表来构建的,那么我们今天要学习的队列是否也是基于顺序表和链表呢?那我们直接进入正题吧!         还是跟上节一样,依旧用图解的方式让大家更好的理解概念。         队列: 队列指的是图中黑色边框

    2024年02月06日
    浏览(27)
  • 数据结构:队列的链表结构(含完整代码,可复制)

    1.输出队列 2.入队一个元素 3.出队一个元素 5.建立链表队列 6.完整代码

    2024年01月16日
    浏览(36)
  • 【数据结构】15 队列应用实例:多项式加法运算

    我们准备采用不带头节点的单向链表结构表示一元多项式,并按照指数递减的顺序排列各项。 对列表存放的两个多项式进行加法运算时,可以使用两个指针p1和p2。初始时的p1和p2分别指向这两个多项式第1个节点(指数的最高项)。通过循环不断比较p1和p2所指的节点,比较结

    2024年02月21日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包