栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

队列详解+代码实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

队列详解+代码实现

队列 一、队列的基本概念

二、顺序队列

顺序队列判断队满或者对空有3种方式

  1. (Q.rear + 1) % MaxSize == Q.front,不过会浪费1个存储单元
  2. 加1个字段length表示顺序队列元素个数
  3. tag标志最近是入队还是出队

下面实现是用第1种方式

#include 

#include 

using namespace std;
#define MaxSize 10

typedef struct{
    int data[MaxSize];
    int front, rear;
}Queue;

void init_data(Queue &Q){
    Q.front = Q.rear = 0;
}

bool set_data(Queue &Q, int value){
    if ((Q.rear + 1) % MaxSize == Q.front) {
        return false;
    }
    Q.data[Q.rear] = value;
    Q.rear = (Q.rear + 1) % MaxSize;
    return true;
}

bool erase_data(Queue &Q, int &value){
    if (Q.front == Q.rear) {
        return false;
    }
    value = Q.data[Q.front++];
    return true;
}

void print_data(Queue Q){
    while (Q.front!=Q.rear) {
        cout << "element:" << Q.front << " " << Q.data[Q.front] << endl;
        Q.front = (Q.front + 1) % MaxSize;
    }
}
int main(){
    std::cout << "welcome, to my world!" << std::endl;
    
    Queue Q;
    init_data(Q);
    cout << "size of:" << sizeof(Q) << endl;
    for (int i = 1; i<= MaxSize; i++) {
        set_data(Q, i*10);
    }
   
    print_data(Q);
  
    int value;
    erase_data(Q, value);
    set_data(Q, 1);
    print_data(Q);
    cout << "size of:" << value << endl;
    print_data(Q);
    return 0;
}

三、链式队列

带头结点代码实现

#include 

#include 

using namespace std;

typedef struct linkNode{
    int data;
    struct linkNode *next;
}linkNode;

typedef struct{
    linkNode *front, *rear;
} Queue;

// 带头结点初始化
void init_data(Queue &Q){
    Q.front = Q.rear = (linkNode *)malloc(sizeof(linkNode));
    Q.front->next = NULL;
}

// 入队
bool set_data(Queue &Q, int value){
    linkNode *p = (linkNode *)malloc(sizeof(linkNode));
    // 内存不够,分配失败
    if (p == NULL) {
        return false;
    }
    // 构建好p结点数据
    p->data = value;
    p->next = NULL;
    
    // 入队
    Q.rear->next = p;
    
    // 队尾指针永远指向最后一个元素
    Q.rear = p;
    return true;
}

bool erase_data(Queue &Q, int &value){
    if (Q.front == Q.rear) {
        return false;
    }
    linkNode *p = Q.front->next;
    value = p->data;
    Q.front->next = p->next;
    
    // 如果是最后一个数据元素,修改尾指针
    if (p == Q.rear) {
        Q.rear = Q.front;
    }
    free(p);
    return true;
}

void print_data(Queue Q){
    while (Q.front!=NULL) {
        cout << "element:" << Q.front << " " << Q.front->data << endl;
        Q.front = Q.front->next;
    }
}
int main(){
    std::cout << "welcome, to my world!" << std::endl;
    
    Queue Q;
    init_data(Q);
    cout << "size of:" << sizeof(Q) << endl;
    for (int i = 1; i<= 10; i++) {
        cout << "size of i:" << i << endl;
        set_data(Q, i*100);
    }
   
    print_data(Q);
  
    int value;
    erase_data(Q, value);
    set_data(Q, 1);
    print_data(Q);

    print_data(Q);
  
    return 0;
}

不带头结点实现

#include 

using namespace std;

typedef struct linkNode{
    int data;
    struct linkNode *next;
}linkNode;

typedef struct{
    linkNode *front, *rear;
} Queue;

// 不带头结点初始化
void init_data(Queue &Q){
    Q.front = Q.rear = NULL;
}

// 入队
bool set_data(Queue &Q, int value){
    linkNode *p = (linkNode *)malloc(sizeof(linkNode));
    // 内存不够,分配失败
    if (p == NULL) {
        return false;
    }
    // 构建好p结点数据
    p->data = value;
    p->next = NULL;
    
    // 第一个元素入队
    if (Q.front == NULL) {
        Q.front = p;
    }else{
        Q.rear->next = p;
    }
    
    // 队尾指针永远指向最后一个元素
    Q.rear = p;
    return true;
}

bool erase_data(Queue &Q, int &value){
    if (Q.front == NULL) {
        return false;
    }
    linkNode *p = Q.front;
    value = p->data;
    Q.front = p->next;
    
    // 如果是最后一个数据元素,修改尾指针
    if (p == Q.rear) {
        Q.rear = Q.front = NULL;
    }
    free(p);
    return true;
}

void print_data(Queue Q){
    while (Q.front!=NULL) {
        cout << "element:" << Q.front << " " << Q.front->data << endl;
        Q.front = Q.front->next;
    }
}
int main(){
    std::cout << "welcome, to my world!" << std::endl;
    
    Queue Q;
    init_data(Q);
    cout << "size of:" << sizeof(Q) << endl;
    for (int i = 1; i<= 10; i++) {
        cout << "size of i:" << i << endl;
        set_data(Q, i*100);
    }
   
    print_data(Q);
  
    int value;
    erase_data(Q, value);
    set_data(Q, 1);
    print_data(Q);

    print_data(Q);
  
    return 0;
}

四、双端队列

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/291189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号