数据结构(三)队列

队列

队列是一种受限制的线性表,只允许表的一端插入,在表的另一端删除

队列(顺序存储)

// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define maxsize 50
#define elemtype int
typedef struct
{
    elemtype data[maxsize];
    int front, rear;  //队头指针和队尾指针
}SqQueue;


void InitQueue(SqQueue &Q)
{
    Q.rear = Q.front = 0;   //初始化队首、队尾指针
}

bool QueueEmpty(SqQueue Q)
{
    if (Q.front == Q.rear)
    {
        return true;

    }
    return false;
}

bool EnQueue(SqQueue& Q, elemtype x)
{
    if (Q.rear == maxsize)
    {
        return false;
    }

    Q.data[Q.rear++] = x;//添加队列
    return true;
}

elemtype DeQueue(SqQueue& Q)
{

    bool ret=QueueEmpty(Q);
    if (ret)
    {
        printf("队列为空!\n");
        exit(1);
    }
    return Q.data[Q.front++];//添加队列
}

bool GetHead(SqQueue Q, elemtype& x)
{
    if (QueueEmpty(Q))
    {
        printf("队列为空!\n");
        exit(1);
    }

    x = Q.data[Q.front];
    return true;
}





int main()
{
    SqQueue Q;
    InitQueue(Q);
    EnQueue(Q, 5);
    EnQueue(Q, 8);
    EnQueue(Q, 10);
    DeQueue(Q);

    for (int i = Q.front; i < Q.rear; i++)
    {
        printf("%d\n",Q.data[i]);
    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

循环队列(顺序存储)

// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define maxsize 50
#define elemtype int
typedef struct
{
    elemtype data[maxsize];
    int front, rear;  //队头指针和队尾指针
}SqQueue;


void InitQueue(SqQueue &Q)
{
    Q.rear = Q.front = 0;   //初始化队首、队尾指针
}

bool QueueEmpty(SqQueue Q)
{
    if (Q.front == Q.rear)
    {
        return true;

    }
    return false;
}

bool EnQueue(SqQueue& Q, elemtype x)
{
    if (Q.rear == maxsize)
    {
        return false;
    }

    Q.data[Q.rear++] = x;//添加队列
    return true;
}

elemtype DeQueue(SqQueue& Q)
{

    bool ret=QueueEmpty(Q);
    if (ret)
    {
        printf("队列为空!\n");
        exit(1);
    }
    return Q.data[Q.front++];//添加队列
}

bool GetHead(SqQueue Q, elemtype& x)
{
    if (QueueEmpty(Q))
    {
        printf("队列为空!\n");
        exit(1);
    }

    x = Q.data[Q.front];
    return true;
}





int main()
{
    SqQueue Q;
    InitQueue(Q);
    EnQueue(Q, 5);
    EnQueue(Q, 8);
    EnQueue(Q, 10);
    DeQueue(Q);

    for (int i = Q.front; i < Q.rear; i++)
    {
        printf("%d\n",Q.data[i]);
    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

队列(链式存储)

带头结点

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;

typedef struct  linknode //链式节点
{
    ElemType data;
    struct linknode* next;

}LinkNode;

//链式队列
typedef struct
{
    LinkNode* front, *rear;

}LinkQueue;

void InitQueue(LinkQueue &Q)
{
    //带头节点的队列初始化
        Q.rear=Q.front=(LinkNode*)malloc(sizeof(LinkNode));
        Q.front->next = NULL;
}

bool IsEmpty(LinkQueue& Q)
{
    if (Q.rear == Q.front)
    {
        return true;
    }
    return false;

}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s= (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x;
    s->next = Q.rear->next;
    Q.rear->next = s;
    Q.rear = s;

}

bool DeQueue(LinkQueue& Q, ElemType &x)
{
    if (IsEmpty(Q))
    {
        //队列为空
        return false;
    }
    LinkNode* p = Q.front->next;
    x = p->data;
    Q.front->next = p->next;
    
    if (p == Q.rear)  //要删除的为尾队列
    {
        Q.rear = Q.front;
    }
    free(p);
    return true;
}

int main()
{
    int x;
    LinkQueue Q;
    InitQueue(Q);
    EnQueue(Q, 5);
    EnQueue(Q, 7);
    EnQueue(Q, 9);
    DeQueue(Q, x);
    LinkNode* p = Q.front->next;
    while (p != NULL)
    {
        printf("%d\n",p->data);
        p = p->next;
    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

不带头结点

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;

typedef struct  linknode //链式节点
{
    ElemType data;
    struct linknode* next;

}LinkNode;

//链式队列
typedef struct
{
    LinkNode* front, * rear;

}LinkQueue;

void InitQueue(LinkQueue& Q)
{
    //不带头节点的队列初始化
    Q.front =Q.rear= NULL;
}

bool IsEmpty(LinkQueue& Q)
{
    if (Q.rear == Q.front)
    {
        return true;
    }
    return false;

}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    if(Q.front==NULL)
    {
        s->data = x;
        s->next = NULL;
        Q.front = Q.rear = s;
        return;
    }

    s->next = NULL;
    s->data = x;
    Q.rear->next = s;
    Q.rear = s;

}

bool DeQueue(LinkQueue& Q, ElemType& x)
{
    if (IsEmpty(Q))
    {
        printf("Queue is empty!\n");
        //队列为空
        return false;
    }
    LinkNode* p = Q.front->next;
    free(Q.front);

    Q.front = p;
}

int main()
{
    int x;
    LinkQueue Q;
    InitQueue(Q);
    EnQueue(Q, 5);
    EnQueue(Q, 7);
    EnQueue(Q, 9);
    DeQueue(Q, x);
    LinkNode* p = Q.front;
    while (p != NULL)
    {
        printf("%d\n", p->data);
        p = p->next;
    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshida_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值