数据结构与算法--队列

问题描述

编程实现队列的入队、出队操作

核心步骤

  • 定义队列节点
  • 入队操作
  • 出队操作

编程实现

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

typedef struct student
{
    int data;
    struct student *next;
}node;

typedef struct linkqueue
{
    node *first, *rear;
}queue;

//队列入队
queue *insert(queue *HQ, int x)
{
    node *s;
    s = (node *)malloc(sizeof(node));
    s->data = x;
    s->next = NULL;

    if(HQ->rear==NULL)
    {
        HQ->first = s;
        HQ->rear = s;
    }
    else
    {
        HQ->rear->next = s;
    }
    return HQ;
}

// 队列出队
queue *del(queue *HQ)
{
    int x;
    if (HQ->first==NULL)
    {
        printf("
 溢出");
    }
    else
    {
        x = HQ->first->data;
        if(HQ->first==HQ->rear)
        {
            HQ->first = NULL;
            HQ->rear = NULL;
        }
        else
        {
            HQ->first = HQ->first->next;
        }
        return HQ;
    }
}
原文地址:https://www.cnblogs.com/CocoML/p/12726970.html