设为首页 加入收藏

TOP

C语言 队列(链式队列)(四)
2019-08-13 05:39:23 】 浏览:133
Tags:语言 队列 链式
ode(q);        /* 按先进先出对队列进行打印 */
        printf("Do you want to append a new node(Y/N)?");
        scanf_s(" %c", &c);
    }
    printf("Do you want to delete node(Y/N)?");
    scanf_s(" %c", &c);
    while (c == 'Y' || c == 'y')
    {
        q = DeletNode(q);    /* 出队 */
        DisplyNode(q);        /* 按先进先出对队列进行打印 */
        printf("Do you want to delete node(Y/N)?");
        scanf_s(" %c", &c);
    }


    return 0;
}
int IsemptyQueue(Queue p)
{
    if (p.front == p.rear)    /* 队首指针和队尾指针重合队列为空 */
    {
        return Empty;
    }
    else
    {
        return NoEmpty;
    }
}
Queue DeletNode (Queue p)
{
    Node *del;


    if (IsemptyQueue(p) == Empty)        /* 判断队列是否为空*/
    {
        printf("队列为空,无法出队 ");
        return p;
    }
    else        /* 队列不为空 */
    {
        if (p.front->next == p.rear)    /* 如果出队的节点为最后一个节点 */
        {
            printf("出队节点的数据为%d----", p.rear->data);
            free(p.rear);    /* 释放最后一一个节点*/
            p.rear = p.front;        /* 队首指针和队尾指针都指向头节点 */
            p.front->next = NULL;
            p.length--;
        }
        else
        {
            del = p.front->next;
            printf("出队节点的数据为%d----", del->data);
            p.front->next = p.front->next->next; /* 使头节点的指针域指向出队节点的下一个节点 */
            free(del);        /* 释放出队的节点 */
            p.length--;
        }


        return p;
    }
}
//函数功能:初始化队列(其实就是搞个头结点放在队列里面)
//单独弄个子函数来初始化队列是为了方便入队的时候判断队列是否为空
Queue init (Queue p)   
{                       
    p.front = p.rear = (Node *)malloc(sizeof(Node));
    if (p.front == NULL && p.rear == NULL)   
    {
        printf("initialization failed");
        exit(0);
    }
    p.front->next = NULL;


    return p;
}
//函数功能:新建节点并添加到队列中,记录队列长度
Queue AppendNode (Queue p)
{
    int data;
    Node *q;


    q = (Node *)malloc(sizeof(Node));
    if (q == NULL)    /* 判断分配内存是否失败 */
    {
        printf("No enough memory to allocate");
        exit(0);
    }
    p.rear->next = q;        /* 最后一个节点的指针指向新建节点*/
    p.re

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言文件操作(FILE)与常用文件操.. 下一篇二叉树、前序遍历、中序遍历、后..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目