15 一个完整的链式队列代码

文件结构:

关于头节点:

  Q.head:头节点本身;

  Q.head->next: 头节点的下一个节点

main.cpp:

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include "function_for_LinkQueue.h"

using namespace std;

int main()
{
    TestLinkQueue();

    return 0;
}
View Code

function_for_LinkQueue.h:

#ifndef FUNCTION_FOR_LINKQUEUE_H_INCLUDED
#define FUNCTION_FOR_LINKQUEUE_H_INCLUDED

#define MAXSIZE 100

typedef char ElemType;

//节点
typedef struct Qnode{
    ElemType data;      //数据域
    struct Qnode *next;     //指针域
}Qnode, *QueuePtr;

typedef struct{
    QueuePtr head;      //队头指针--指向位于队头的Qnode节点
    QueuePtr rear;      //队尾指针--指向位于队尾的Qnode节点
}LinkQueue;

//初始化
void InitQueue(LinkQueue &Q);
//销毁
//从队头节点开始,依次释放所有节点
void DestroyQueue(LinkQueue &Q);

//(队尾)插入元素
void InsertQueue(LinkQueue &Q, ElemType e);

//(队头)出队--返回值
ElemType DelQueue_with_elem(LinkQueue &Q);

//(队头)出队--不返回值
void DelQueue(LinkQueue &Q);

//获取队头元素
ElemType GetHead(LinkQueue &Q);

//打印队列元素--从头到尾
void PrintQueue(LinkQueue &Q);

//测试
void TestLinkQueue();

#endif // FUNCTION_FOR_LINKQUEUE_H_INCLUDED
View Code

function_for_LinkQueue.cpp:

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

//初始化
void InitQueue(LinkQueue &Q){
    Q.head = (QueuePtr)malloc(sizeof(Qnode));       //分配空间,将队头、队尾指针都指向队头
    Q.rear = Q.head;
    Q.head->next = NULL;
}

//销毁
//从队头节点开始,依次释放所有节点
void DestroyQueue(LinkQueue &Q){
    while(Q.head != NULL){
        QueuePtr p = Q.head->next;
        free(Q.head);
        Q.head = p;
    }
    printf("销毁成功
");
}

//(队尾)插入元素
void InsertQueue(LinkQueue &Q, ElemType e){
    QueuePtr p = (QueuePtr)malloc(sizeof(Qnode));       //新分配一个节点空间
    if(!p)  exit(0);        //判断分配是否成功
    p->data = e;        //给数据域赋值
    p->next = NULL;
    Q.rear->next = p;       //关键算法 -- 可以看出,此队列存在头节点,头节点在第一个节点上,首元节点在第二个节点上
    Q.rear = p;
}

//(队头)出队--返回值
ElemType DelQueue_with_elem(LinkQueue &Q){
    if(Q.head == Q.rear)        exit(0);        //队列先判断是否为空
    ElemType e = Q.head->next->data;
    QueuePtr p = (QueuePtr)malloc(sizeof(Qnode));
    p = Q.head->next;
    Q.head->next = p->next;
    //考虑特殊情况,若头指针的下一个节点就是尾指针,即队列中只有一个元素
    if(Q.rear == p)     Q.head = Q.rear;    //则头指针指向尾指针位置,即队列清空
    free(p);
    return e;
}

//(队头)出队--不返回值
void DelQueue(LinkQueue &Q){
    if(Q.head == Q.rear)        exit(0);        //队列先判断是否为空
    QueuePtr p = (QueuePtr)malloc(sizeof(Qnode));
    p = Q.head->next;
    Q.head->next = p->next;
    //考虑特殊情况,若头指针的下一个节点就是尾指针,即队列中只有一个元素
    if(Q.rear == p)     Q.head = Q.rear;    //则头指针指向尾指针位置,即队列清空
    free(p);
}

//获取队头元素
ElemType GetHead(LinkQueue &Q){
    if(Q.head == Q.rear)        exit(0);        //判空
    return Q.head->next->data;      //队列头节点不存放元素,首元节点在第二个节点
}

//打印队列元素--从头到尾
void PrintQueue(LinkQueue &Q){
    if(Q.head == Q.rear)        exit(0);        //先判空
    QueuePtr p = Q.head->next;        //建立游标指向首元节点
    do{
        printf("%c ", p->data);     //依次输出每个元素的数据域
        p = p->next;        //游标后移
    }while(p);
}

//测试
void TestLinkQueue(){
    LinkQueue Q;
    printf("
初始化:
");
    InitQueue(Q);

    printf("
插入元素a,b,c,d,e(队尾):
");
    InsertQueue(Q, 'a');
    InsertQueue(Q, 'b');
    InsertQueue(Q, 'c');
    InsertQueue(Q, 'd');
    InsertQueue(Q, 'e');

    printf("
获取队头元素:%c
", GetHead(Q));
    printf("打印队列:
");
    PrintQueue(Q);

//    printf("获取队头元素:%c
", GetHead(Q));
//    printf("打印队列:
");
//    PrintQueue(Q);

    printf("
再次从队尾插入元素:
");
    InsertQueue(Q, 'f');
//
    printf("获取队头元素:%c
", GetHead(Q));
    printf("打印队列:
");
    PrintQueue(Q);

    printf("
此时头节点的next指向的节点的数据域是:%c
", Q.head->next->data);

    printf("
出队:
");
    DelQueue(Q);

    printf("获取队头元素:%c
", GetHead(Q));
    printf("打印队列:
");
    PrintQueue(Q);

    printf("
出队并返回队头元素:%c
", DelQueue_with_elem(Q));

    printf("获取队头元素:%c
", GetHead(Q));
    printf("打印队列:
");
    PrintQueue(Q);

    printf("
销毁节点:
");
    DestroyQueue(Q);

    printf("获取队头元素:%c
", GetHead(Q));
    printf("打印队列:
");
    PrintQueue(Q);
}
View Code

运行结果:

原文地址:https://www.cnblogs.com/CPU-Easy/p/11724548.html