zoukankan      html  css  js  c++  java
  • 数据结构C语言实现----入队列操作

    代码如下:

    /*****************************************
     * 入队列操作
     * 首先,创建一个单链表的结点
     * 将这个结点挂在现在队伍的队尾后面
     * 再将队尾指向这个节点
     * ***************************************/
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct QNode
    {
        char date;
        struct QNode *next;
    }QNode , *QueueP;
    typedef struct 
    {
        QueueP front;
        QueueP rear; 
    }LinkQueue;
    ////////////////////////////////////////////
    //创建一个队伍
    void initQueue(LinkQueue *q)
    {
        q->front = q->rear = (QueueP)malloc(sizeof(QNode));
        if (!q->front)
        {
            exit(0);
        }
        q->front->next = NULL;
    }
    ////////////////////////////////////////////
    //入队列操作
    void EnterQueue(LinkQueue *q , char e)
    {
        QueueP New_node;
        New_node = (QueueP)malloc(sizeof(QNode));//创建一个队列元素的结点
        if (!q->front)
        {
            exit(0);   //头结点创建失败
        }
        New_node->date = e;//给新队列元素赋值
        New_node->next = NULL;//新队列元素在队伍最后,所以后面为NULL
        q->rear->next = New_node;//让队列中原来的队尾指向这个插入的新元素
        q->rear = New_node;//插入的新元素变为队尾
    }
    
    int main()
    {
        LinkQueue q;
        initQueue(&q);
        char e;
        e = getchar();
        EnterQueue(&q , e);
        printf("入队列成功,正在打印队尾元素...
    队尾元素为:%c",q.rear->date);
        return 0;
    }
    

      

      

    运行结果:

  • 相关阅读:
    《构建之法》第五章读后感
    《构建之法》第四章读后感
    《构建之法》第三章读后感
    《构建之法》第二章读后感
    《构建之法》第一章读后感
    web mis系统构建
    异常
    多态
    接口与继承
    个人总结_5.8
  • 原文地址:https://www.cnblogs.com/jerryleesir/p/13336737.html
Copyright © 2011-2022 走看看