c语言数据结构队列的操作


#pragma once

#include <stdio.h>//名称跟着stack走,cplusplus,
#include <stdlib.h>//队列 先进先出,队尾入,对头出
#include <assert.h>
#include <stdbool.h>

typedef int QDataType;

typedef struct QueueNode{
    struct QueueNode* next;
    QDataType data;
}QueueNode;

typedef struct Queue{            //多个值需要指针,传结构体用一级指针
    QueueNode* head;          //改变结构体,就穿结构体的指针过去
    QueueNode* tail;
}Queue;
void QueueInit(Queue* pq)
{
    assert(pq);
    pq->head = NULL;
    pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
    assert(pq);
    QueueNode* cur = pq->head;
    while(cur != NULL){
        //保存下一个,在free当前位置
        QueueNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->head = pq->tail = NULL;
}
void QuueuePush(Queue* pq,QDataType x)
{
    assert(pq);//然后开辟一个新的结点
    QueueNode* newNode =(QueueNode*) malloc(sizeof(QueueNode));
    newNode->data = x;//还要赋值
    newNode->next = NULL;
    if(pq->head == NULL)
    {
        pq->head = pq->tail = newNode;
    }
    else
    {
        pq->tail->next = newNode;
        pq->tail = newNode;
    }
}
void QueuePop(Queue* pq)//队头出数据
{
    assert(pq);
    //pop一定要有数据
    assert(!QueueEmpty(pq));//删到没有的时候就不能删除了
    //if(pq->head == NULL)
    //  return;
    QueueNode* next =pq->head->next;
    free(pq->head);
    pq->head = next; 
    if(pq->head == NULL)
    {
        pq->tail = NULL;
    }
}
QDataType QueueFront(Queue* pq)//取队头数据
{
    
}
QDataType QueueBack(Queue* pq)
{
    
}
int QueueSize(Queue* pq)
{
    
}
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->head == NULL;
}
void QueuePrint(Queue* pq)
{
    
}
void TextQueue()//不要有参数
{
    Queue q;
    QueueInit(&q);
    QuueuePush(&q,1);
    QuueuePush(&q,2);
    QuueuePush(&q,3);
    QuueuePush(&q,4);
    QuueuePush(&q,5);
    QueuePrint(&q);
    QueuePop(&q);
    QueuePop(&q);
    QueuePop(&q);
    //QueueEmpty(&q);
    QueueDestroy(&q);
}
int main()
 {    
    TextQueue();
    return 0;
}

img

  1. 这里的报错我不会改了,我用bool作为返回值不可以嘛?
  2. 在队友出数据的时候先断言,应该也有问题,assert(!QueueEmpty(pq))

把 QueueEmpty 的函数实现 放到 QueuePop 函数的前面,不然无法引用到
然后 还有些没有实现的函数,返回值 没有,要编译不报错,都先加个默认的
完整能编译过的

 

 
#include <stdio.h>//名称跟着stack走,cplusplus,
#include <stdlib.h>//队列 先进先出,队尾入,对头出
#include <assert.h>
#include <stdbool.h>
 
typedef int QDataType;
 
typedef struct QueueNode{
    struct QueueNode* next;
    QDataType data;
}QueueNode;
 
typedef struct Queue{            //多个值需要指针,传结构体用一级指针
    QueueNode* head;          //改变结构体,就穿结构体的指针过去
    QueueNode* tail;
}Queue;
void QueueInit(Queue* pq)
{
    assert(pq);
    pq->head = NULL;
    pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
    assert(pq);
    QueueNode* cur = pq->head;
    while(cur != NULL){
        //保存下一个,在free当前位置
        QueueNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->head = pq->tail = NULL;
}
void QuueuePush(Queue* pq,QDataType x)
{
    assert(pq);//然后开辟一个新的结点
    QueueNode* newNode =(QueueNode*) malloc(sizeof(QueueNode));
    newNode->data = x;//还要赋值
    newNode->next = NULL;
    if(pq->head == NULL)
    {
        pq->head = pq->tail = newNode;
    }
    else
    {
        pq->tail->next = newNode;
        pq->tail = newNode;
    }
}
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->head == NULL;
}
void QueuePop(Queue* pq)//队头出数据
{
    assert(pq);
    //pop一定要有数据
    assert(!QueueEmpty(pq));//删到没有的时候就不能删除了
    //if(pq->head == NULL)
    //  return;
    QueueNode* next =pq->head->next;
    free(pq->head);
    pq->head = next; 
    if(pq->head == NULL)
    {
        pq->tail = NULL;
    }
}
QDataType QueueFront(Queue* pq)//取队头数据
{
    return NULL;
}
QDataType QueueBack(Queue* pq)
{
    return NULL;
}
int QueueSize(Queue* pq)
{
    return 0;
}

void QueuePrint(Queue* pq)
{
    
}
void TextQueue()//不要有参数
{
    Queue q;
    QueueInit(&q);
    QuueuePush(&q,1);
    QuueuePush(&q,2);
    QuueuePush(&q,3);
    QuueuePush(&q,4);
    QuueuePush(&q,5);
    QueuePrint(&q);
    QueuePop(&q);
    QueuePop(&q);
    QueuePop(&q);
    //QueueEmpty(&q);
    QueueDestroy(&q);
}
int main()
 {    
    TextQueue();
    return 0;
}