zoukankan      html  css  js  c++  java
  • C语言数据结构队列

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


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


    struct Queue {
    Node* head;
    Node* rear;
    };


    Queue* creatQueue() {
    Queue *Q = new Queue;
    Node *node = (Node*)malloc(sizeof(Node)); 
    node->next = NULL;
    Q->head = Q->rear = node;
    return Q;
    }


    void enQueue(Queue *Q, int d) {
    Node *node = (Node*)malloc(sizeof(Node));
    node->data = d;
    node->next = NULL;
    Q->rear->next = node;
    Q->rear = node;
    }


    void output(Queue *Q) {
    int data;
    if (Q->head == Q->rear) {
    printf("队列下溢!");
    }
    Node *node = Q->head->next;
    data = node->data;
    Q->head->next = node->next;
    if (node->next == NULL) {
    Q->rear = Q->head;
    }
    free(node);
    }


    void printQueue(Queue *Q) {
    Node *node = Q->head->next;
    puts("队列成员如下 ");
    while (node != NULL) {
    printf("%d ", node->data);
    node = node->next;
    }
    putchar(' ');
    }


    void destroyQueue(Queue *Q) { 
    Node *node;
    while (Q->head) {
    node = Q->head->next;
    free(Q->head);
    Q->head = node;
    }
    free(Q);
    }


    int main(int argc,char** argv) {
    int select,d;
    Queue *Q = creatQueue();
    while (1) 
    {
    puts("1,入队 2,出队 3,遍历 0,退出 请输入操作号:");
    scanf("%d", &select);
    switch (select) 
    {
    case 1:
    puts("请输入需要入队的数字:");
    scanf("%d", &d);
    enQueue(Q, d);
    break;
    case 2:
    output(Q);
    break;
    case 3:
    printQueue(Q);
    break;
    case 0: 
    printf("程序已退出 ");
    exit(1);
    break;
    default:
    puts("输入错误! ");
    break;
    }

    }


    destroyQueue(Q);

    }



  • 相关阅读:
    docker安装kibana
    docker 安装elasticsearch
    redis常用命令
    判断CheckBox,select是否被选中
    里面的div怎么撑开外面的div,让高度自适应
    超链接,表单jquery提交方式
    SSM框架的sql中参数注入(#和$的区别)
    springmvc接收值的方式
    mysql语法顺序
    js创建对象并赋值其属性
  • 原文地址:https://www.cnblogs.com/viplanyue/p/12700739.html
Copyright © 2011-2022 走看看