博客
关于我
队列练习
阅读量: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/

你可能感兴趣的文章
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>