C 语言 实现 Queue

queue.h

cpp 复制代码
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include <stdbool.h>

typedef int Item;

#define MAXQUEUE 10

typedef struct node
{
    Item item;
    struct node *next;
}Node;

typedef struct queue
{
    Node *front;
    Node *rear;
    int items;

}Queue;

void InitializeQueue(Queue *pq);
bool QueueIsFull(const Queue *pq);
bool QueueIsEmpty(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Item item, Queue *pq);
bool DeQueue(Item *pitem, Queue *pq);
void EmptyTheQueue(Queue *pq);

#endif

queue.c

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

static void CopyToNode(Item item, Node *pn);
static void CopyToItem(Node *pn, Item *pi);


void InitializeQueue(Queue *pq)
{
    pq->front = pq->rear = NULL;
    pq->items = 0;
}

bool QueueIsFull(const Queue *pq)
{
    return pq->items == MAXQUEUE;
}

bool QueueIsEmpty(const Queue *pq)
{
    return pq->items == 0;
}

int QueueItemCount(const Queue *pq)
{
    return pq->items;
}

bool EnQueue(Item item, Queue *pq)
{
    Node *pnew;

    if(QueueIsFull(pq)) return false;
    pnew = (Node *) malloc(sizeof(Node));
    if(pnew == NULL)
    {
        fprintf(stderr, "Unable to allocate memory!\n");
        exit(1);
    }
    CopyToNode(item, pnew);
    pnew->next = NULL;
    if(QueueIsEmpty(pq))
    {
        pq->front = pnew;
    }
    else
    {
        pq->rear->next = pnew;
    }
    pq->rear = pnew;
    pq->items++;

    return true;
}

bool DeQueue(Item *pitem, Queue *pq)
{
    Node *pt;
    if(QueueIsEmpty(pq)) return false;
    CopyToItem(pq->front, pitem);
    pt= pq->front;
    pq->front = pq->front->next;
    free(pt);
    pq->items--;
    if(pq->items == 0)
    {
        pq->rear = NULL;
    }

    return true;
}

void EmptyTheQueue(Queue *pq)
{
    Item dummy;
    while(!QueueIsEmpty(pq))
    {
        DeQueue(&dummy, pq);
    }
}

static void CopyToNode(Item item, Node *pn)
{
    pn->item = item;
}

static void CopyToItem(Node *pn, Item *pi)
{
    *pi = pn->item;
}

main.c

cpp 复制代码
#include <stdio.h>
#include "queue.h"

int main(void)
{
    Queue line;
    Item temp;
    char ch;

    InitializeQueue(&line);
    puts("Testing the Queue interface. Type a to add a value, ");
    puts("type d to delete a value, and type q to quit");
    while((ch = getchar()) != 'q')
    {
        if(ch != 'a'  && ch != 'd')
        {
            continue;
        }

        if(ch == 'a')
        {
            printf("Integer to add: ");
            scanf("%d", &temp);
            if(!QueueIsFull(&line))
            {
                printf("Putting %d into queue\n", temp);
                EnQueue(temp, &line);
            }
            else
            {
                puts("Queue is full");
            }
        }
        else
        {
            if(QueueIsEmpty(&line))
            {
                puts("Nothing to delete!");
            }
            else
            {
                DeQueue(&temp, &line);
                printf("Removing %d from queue\n", temp);
            }
        }

        printf("%d items in queue\n", QueueItemCount(&line));
        puts("Type a to add , d to delte, q to quit: ");
    }

    EmptyTheQueue(&line);
    puts("Bye!");

    return 0;
}
相关推荐
Tomorrow'sThinker1 小时前
Python零基础学习第三天:函数与数据结构
开发语言·windows·python
元媛媛1 小时前
Python - 轻量级后端框架 Flask
开发语言·python·flask
钢板兽1 小时前
Java后端高频面经——Spring、SpringBoot、MyBatis
java·开发语言·spring boot·spring·面试·mybatis
爱吃柠檬呀1 小时前
《C陷阱与缺陷》读书笔记(一)
c语言·开发语言·算法·《c陷阱与缺陷》·编写程序
行码棋2 小时前
【Python】omegaconf 用法详解
开发语言·python
awonw2 小时前
[java][基础] 悲观锁 vs 乐观锁
java·开发语言
Trouvaille ~2 小时前
【Java篇】数据类型与变量:窥见程序的天地万象
java·开发语言·青少年编程·面向对象·数据类型·基础知识·入门必看
新停浊酒杯2 小时前
Java基础——java8+新特性——方法引用(::)
java·开发语言
flashier2 小时前
C语言 进阶指针学习笔记
c语言·笔记·学习
wxr的理想之路2 小时前
list链表的使用
c语言·数据结构·链表·list