博客
关于我
队列练习
阅读量:501 次
发布时间:2019-03-07

本文共 1593 字,大约阅读时间需要 5 分钟。

#pragma once 

typedef int QDataType;

//此时需要注意的是结构体的建立
//第二个结构体的建立需要用QNode来,Queue会出错误
//用QNode可以将两个结构体链接起来

//若链接不成功,一定是结构体出粗了

typedef struct QNode{
    struct QNode* next;
    QDataType val;
}QNode;

typedef struct Queue{

    struct QNode* head;
    struct QNode* rear;
    QDataType _size;
}Queue;

void QueueInit(Queue* q);

void QueuePush(Queue* q, int val);

void QueuePop(Queue* q);

QDataType QueueFront(Queue* q);

QDataType QueueBack(Queue* q);

int QueueSize(Queue* q);

int QueueEmpty(Queue* q);

 

 

#include "Queue.h"

#include <stddef.h>
#include <malloc.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>

//建立节点,判断是否建立成功,节点是QNode*
//最后赋值,然后返回
QNode* BuyNode(int val)
{
    QNode* NewNode = (QNode*)malloc(sizeof(QNode));
    if (NULL == NewNode)
    {
        assert(0);
        return NULL;
    }

    NewNode->next = NULL;

    NewNode->val = val;

    return NewNode;//记得返回

}

int QueueEmpty(Queue* q)
{
    assert(q);
    return 0 == q->_size;
}

void QueueInit(Queue* q)
{
    assert(q);
    q->head = BuyNode(0);
    q->rear = q->head;
    q->_size = 0;
}

//入队列先建立节点,然后将节点连接上去
//然后挪动q->rear
//最后有效元素个数++
void QueuePush(Queue* q, int val)
{
    assert(q);
    QNode* NewNode = BuyNode(val);
    q->rear->next = NewNode;
    q->rear = NewNode;
    q->_size++;
}

//创建一个删除节点,然后标记删除的位置
//将q->head往后挪一下,在删除节点delNode
void QueuePop(Queue* q)
{
    QNode* delNode = NULL;
    assert(q);
    if (QueueEmpty(q))
        return;

    delNode = q->head->next;

    q->head->next = delNode->next;
    free(delNode);
    q->_size--;
}

QDataType QueueFront(Queue* q)

{
    assert(!QueueEmpty(q));
    return q->head->next->val;
}

QDataType QueueBack(Queue* q)

{
    assert(q);
    return q->rear->val;
}

int QueueSize(Queue* q)

{
    assert(q);
    return q->_size;
}

转载地址:http://afacz.baihongyu.com/

你可能感兴趣的文章
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>