数据结构— —队列(链式存储)

简介: 数据结构— —队列(链式存储)

链式存储

队列的链式存储结构,其实就是线性表的单链表,只不过它只是尾进头出而已,我们把它简称为链队列。为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指向终端节点

f42468d6c1b342898a9adeeabda11dac.png

typedef int DataType; //队列中元素类型
typedef struct _QNode 
{ //结点结构
    DataType data; 
    struct _QNode *next;
}QNode; 
typedef QNode * QueuePtr;
typedef struct Queue
{
    int length; //队列的长度
  QueuePtr front; //队头指针
  QueuePtr rear;  //队尾指针
}LinkQueue;

链式队列操作图示

空队列时,front 和 rear 都指向空。


c81fc90ad1cd44779f4ed7598f1f63d3.png

删除节点


a7dba4947cc64aa58c57b18bd6f18022.png

完整代码实现

#include <stdio.h>
#include <assert.h>
#include <Windows.h>
#include <iostream> 
#include <iomanip> 
using namespace std;
#define MaxSize 5 //队列的最大容量 
typedef int DataType; //队列中元素类型
typedef struct _QNode 
{ //结点结构
    DataType data; 
    struct _QNode *next;
}QNode; 
typedef QNode * QueuePtr;
typedef struct Queue
{
    int length; //队列的长度
  QueuePtr front; //队头指针
  QueuePtr rear;  //队尾指针
}LinkQueue;
//队列初始化,将队列初始化为空队列
void InitQueue(LinkQueue *LQ)
{
    if(!LQ) return ;
    LQ->length = 0;
    LQ->front = LQ->rear = NULL; //把对头和队尾指针同时置0
}
//判断队列为空
int IsEmpty(LinkQueue *LQ)
{ 
    if(!LQ) return 0; 
    if (LQ->front == NULL)
    { 
        return 1;
    } 
    return 0;
}
//判断队列是否为满
int IsFull(LinkQueue *LQ)
{
    if(!LQ) return 0;
    if (LQ->length == MaxSize)
    { 
        return 1;
    } 
        return 0;
}
//入队,将元素data插入到队列LQ中
int EnterQueue( LinkQueue *LQ,DataType data)
{ 
    if(!LQ) return 0;
    if(IsFull(LQ))
    {
        cout<<"无法插入元素 "<<data<<", 队列已满!"<<endl; 
        return 0;
    }
    QNode *qNode = new QNode; 
    qNode->data = data; 
    qNode->next = NULL;
    if(IsEmpty(LQ))
    {//空队列
        LQ->front = LQ->rear = qNode;
    }
    else 
    {
        LQ->rear->next =qNode;//在队尾插入节点qNode
      LQ->rear = qNode; //队尾指向新插入的节点
    }
    LQ->length++;
    return 1;
}
//出队,将队列中队头的元素出队,其后的第一个元素成为新的队首
int DeleteQueue(LinkQueue *LQ, DataType *data)
{
    QNode * tmp = NULL;
    if(!LQ || IsEmpty(LQ))
    { 
        cout<<"队列为空!"<<endl;
        return 0;
    }
    if(!data) return 0; 
    tmp = LQ->front;
    LQ->front = tmp->next;
    if(!LQ->front) 
        LQ->rear=NULL;//如果对头出列后不存在其他元素,则rear节点也要置空
    *data = tmp->data; 
    LQ->length--; 
    delete tmp;
    return 1;
}
//打印队列中的各元素
void PrintQueue(LinkQueue *LQ) 
{ 
    QueuePtr tmp; 
    if(!LQ) return ;
    if(LQ->front==NULL)
    {
        cout<<"队列为空!";
        return ;
    }
    tmp = LQ->front; 
    while(tmp)
    { 
        cout<<setw(4)<<tmp->data;
        tmp = tmp->next;
    }
    cout<<endl;
}
//获取队首元素,不出队
int GetHead(LinkQueue *LQ,DataType *data)
{ 
    if (!LQ || IsEmpty(LQ))
    { 
        cout<<"队列为空!"<<endl;
        return 0; 
    } 
    if(!data) return 0;
    *data = LQ->front->data;
    return 1;
}
//清空队列
void ClearQueue(LinkQueue *LQ)
{ 
    if(!LQ) return ;
    while(LQ->front)
    {
        QueuePtr tmp = LQ->front->next; 
        delete LQ->front; 
        LQ->front = tmp;
    }
    LQ->front = LQ->rear = NULL;
    LQ->length = 0;
}
//获取队列中元素的个数
int getLength(LinkQueue* LQ)
{ 
    if(!LQ) return 0;
    return LQ->length;
}
int main()
{
    LinkQueue *LQ = new LinkQueue;
    DataType data = -1; 
    //初始化队列
    InitQueue(LQ);
    //入队
    for(int i=0; i<7; i++)
    { 
    EnterQueue(LQ, i);
    }
    //打印队列中的元素  
    printf("队列中的元素(总共%d 个):", getLength(LQ));
    PrintQueue(LQ); cout<<endl;
    //出队
    //for(int i=0; i<10; i++)
    { 
        if(DeleteQueue(LQ, &data))
        { 
            cout<<"出队的元素是:"<<data<<endl;
        }
        else 
        { 
            cout<<"出队失败!"<<endl;
        }
  //}
    //打印队列中的元素
    printf("出队一个元素后,队列中剩下的元素[%d]:", getLength(LQ));
    PrintQueue(LQ); cout<<endl;
    ClearQueue(LQ); cout<<"清空队列!\n";
    PrintQueue(LQ);
    //清理资源 
    delete LQ;
    system("pause"); 
    return 0;
}


相关文章
|
8天前
|
存储 监控 NoSQL
Redis处理大量数据主要依赖于其内存存储结构、高效的数据结构和算法,以及一系列的优化策略
【5月更文挑战第15天】Redis处理大量数据依赖内存存储、高效数据结构和优化策略。选择合适的数据结构、利用批量操作减少网络开销、控制批量大小、使用Redis Cluster进行分布式存储、优化内存使用及监控调优是关键。通过这些方法,Redis能有效处理大量数据并保持高性能。
31 0
|
8天前
|
存储 SQL 关系型数据库
关系型数据库数据结构化存储
【5月更文挑战第8天】关系型数据库数据结构化存储
32 6
|
2天前
【数据结构】二叉树的链式结构的实现 -- 详解
【数据结构】二叉树的链式结构的实现 -- 详解
|
2天前
|
存储
【数据结构】队列(Queue)的实现 -- 详解
【数据结构】队列(Queue)的实现 -- 详解
|
2天前
|
机器学习/深度学习 存储 算法
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)(下)
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)
4 0
|
2天前
|
算法 前端开发 C语言
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)(上)
数据结构与算法⑨(第三章_下)队列的概念和实现(力扣:225+232+622)
11 0
|
6天前
|
存储 机器学习/深度学习 人工智能
数据结构(五)----特殊矩阵的压缩存储
数据结构(五)----特殊矩阵的压缩存储
17 3
|
6天前
|
前端开发 JavaScript 算法
JavaScript 中实现常见数据结构:栈、队列与树
JavaScript 中实现常见数据结构:栈、队列与树
|
8天前
|
存储 编译器 C语言
数据结构——顺序队列与链式队列的实现-2
数据结构——顺序队列与链式队列的实现
数据结构——顺序队列与链式队列的实现-2
|
8天前
|
存储 C语言
数据结构——顺序队列与链式队列的实现-1
数据结构——顺序队列与链式队列的实现
数据结构——顺序队列与链式队列的实现-1