链式队列的两种实现方式(C语言)

实现方式一

1、头文件LinkQueue.h

#include <stdio.h>
typedef struct Node
{
    struct Node* next;
}Node;

typedef struct LinkQueue
{
    Node* front;
    Node* rear;
    int length;
}LinkQueue;

void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, Node**);
void EnQueue(LinkQueue*, Node*);
void DeQueue(LinkQueue*, Node**);
void ClearQueue(LinkQueue*);

2、相关操作函数文件LinkQueue.c

#include <stdio.h>
#include "LinkQueue.h"

void InitQueue(LinkQueue* lq)
{
    lq->front = NULL;
    lq->rear = NULL;
    lq->length = 0;
}

int QueueEmpty(LinkQueue* lq)
{
    if(lq->length == 0)
        return 1;

    return 0;
}

int QueueLength(LinkQueue* lq)
{
    return lq->length;
}

void GetFront(LinkQueue* lq, Node** e)
{
    if(lq->length == 0)
    {
        printf("空队列!\n");
        return;
    }

    *e = lq->front;
}

void EnQueue(LinkQueue* lq, Node* e)
{
    if(lq->length == 0)
    {
        lq->front = lq->rear = e;
    }
    else
    {
        lq->rear->next = e;
        lq->rear = lq->rear->next;
    }

    lq->length++;
}

void DeQueue(LinkQueue* lq, Node** e)
{
    if(lq->length == 0)
    {
        printf("空队列!\n");
        return;
    }

    *e = lq->front;
    lq->front = lq->front->next;
    lq->length--;
    if(lq->length == 0)
    {
        lq->rear = NULL;
    }
}

void ClearQueue(LinkQueue* lq)
{
    while(lq->length)
    {
        Node* tmp;
        DeQueue(lq, (Node**)&tmp);
    }
}

3、主函数测试文件main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

typedef struct stu
{
    Node p;
    int id;
    int age;
}student;

int main()
{
    LinkQueue lq;
    InitQueue(&lq);
    student s[10];
    int i = 0;
    for(i = 0; i < 10; i++)
    {
        s[i].id = i;
        s[i].age = i + 20;
        EnQueue(&lq, &s[i].p);
    }

    printf("当前队列长度为:%d\n", QueueLength(&lq));

    while(!QueueEmpty(&lq))
    {
        Node* tmp;
        GetFront(&lq, (Node**)&tmp);
        student* tmp1 = (student*)tmp;
        printf("当前队头元素值为:id = %d, age = %d\n", tmp1->id, tmp1->age);
        DeQueue(&lq, (Node**)&tmp);
        tmp1 = (student*)tmp;
        printf("当前出队元素值为:id = %d, age = %d\n", tmp1->id, tmp1->age);
        printf("\n");

    }
    return 0;
}

实现方式二

1、头文件LinkQueue.h

#include <stdio.h>

typedef struct Node
{
    struct Node* next;
}Node;

typedef struct QueueNode
{
    Node P;
    void* data;
}QueueNode;

typedef struct LinkQueue
{
    int length;
    QueueNode* front;
    QueueNode* rear;
}LinkQueue;

void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, void**);
void EnQueue(LinkQueue*, void*);
void DeQueue(LinkQueue*, void**);
void ClearQueue(LinkQueue*);

2、相关操作函数main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

void InitQueue(LinkQueue* lq)
{
    lq->length = 0;
    lq->front = NULL;
    lq->rear = NULL;
}

int QueueEmpty(LinkQueue* lq)
{
    if(lq->length == 0)
        return 1;
    return 0;
}

int QueueLength(LinkQueue* lq)
{
    return lq->length;
}

void GetFront(LinkQueue* lq, void** e)
{
    if(lq->length == 0)
    {
        printf("空队列!\n");
        return;
    }

    *e = lq->front->data;
}

void EnQueue(LinkQueue* lq, void* e)
{
    QueueNode* pNew = (QueueNode*)malloc(sizeof(QueueNode));
    pNew->data = e;

    if(lq->length == 0)
    {
        lq->front = lq->rear = pNew;
    }
    else
    {
        lq->rear->P.next = &pNew->P;
        lq->rear = pNew;
    }
    lq->length++;
}

void DeQueue(LinkQueue* lq, void** e)
{
    if(lq->length == 0)
    {
        printf("空队列!\n");
        return;
    }

    QueueNode* pDel;
    pDel = lq->front;
    *e = pDel->data;
    lq->front = (QueueNode*)pDel->P.next;
    free(pDel);

    lq->length--;
    if(lq->length == 0)
    {
        lq->rear = NULL;
    }
}
void ClearQueue(LinkQueue* lq)
{
    while(lq->length)
    {
        void* tmp;
        DeQueue(lq, (void**)&tmp);
    }
}

3、主函数main.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"

typedef struct stu
{
    int id;
    int age;
}student;

int main()
{
    LinkQueue lq;
    InitQueue(&lq);
    student s[10];

    int i = 0;
    for(i = 0; i < 10; i++)
    {
        s[i].id = i;
        s[i].age = i + 20;
        EnQueue(&lq, &s[i]);
    }

    printf("当前队列的长度为:%d\n", QueueLength(&lq));

    while(!QueueEmpty(&lq))
    {
        student* tmp;
        GetFront(&lq, (void**)&tmp);
        printf("当前队头元素值为:id = %d, age = %d\n", tmp->id, tmp->age);

        DeQueue(&lq, (void**)&tmp);
        printf("当前出队元素值为:id = %d, age = %d\n", tmp->id, tmp->age);
        printf("\n");
    }
    printf("当前队列的长度为:%d\n", QueueLength(&lq));

    EnQueue(&lq, &s[0]);
    ClearQueue(&lq);
    printf("当前队列的长度为:%d\n", QueueLength(&lq));

    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 8,139评论 0 27
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,679评论 1 32
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,219评论 0 3
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 4,069评论 0 2
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,259评论 0 38

友情链接更多精彩内容