队列

目录

 

队列的基本操作有初始化队列,判队列是否为空,入队,出队

栈可分为两种存储结构:顺序队和链队。

顺序队

/* 顺序队结构 */
typedef struct 
{
ElemType data[MAXSIZE];
int front;
int rear;
} SqQueue;

顺序队四个要素:

(1)队空条件:qu.rear == qu.front;

(2)队满条件: (qu.rear + 1) % MAXSIZE == qu.front;

(3)进队条件: qu.rear = (qu.rear + 1) % MAXSIZE; qu.data[qu.rear] = data;

(4)出队条件: qu.front = (qu.front + 1) % MAXSIZE; data = qu.data[qu.front];

顺序队基本操作

 

复制代码
#include "stdafx.h"
#include <stdlib.h>

#define MAXSIZE 10

typedef int ElemType;

/* 顺序栈结构 */
typedef struct 
{
    ElemType data[MAXSIZE];
    int front;
    int rear;
} SqQueue;

void InitStack(SqQueue &qu)
{
    qu.front = qu.rear = 0;
}

bool IsEmpty(SqQueue &qu)
{
    return (qu.front == qu.rear);
}

int InQueue(SqQueue &qu, ElemType data)
{
    if ((qu.rear + 1) % MAXSIZE == qu.front)
    {
        return -1;
    }
    
    qu.data[qu.rear] = data;
    qu.rear = (qu.rear+1) % MAXSIZE;
    
    return 0;
}

int OutQueue(SqQueue &qu, ElemType &data)
{
    if (qu.front == qu.rear)
    {
        return -1;
    }

    qu.front = (qu.front + 1) % MAXSIZE;
    data = qu.data[qu.front];    
    return 0;
}

void PrintQueue(SqQueue &qu)
{
    int i = 0;
    if (qu.front == qu.rear)
        return;

    i = qu.front;
    while (i != qu.rear)
    {
        printf("%d\t", qu.data[i]);
        i = (i+1)%MAXSIZE;
    }
    printf("\r\n");
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{

    ElemType array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int i = 0;
    int data = 0;
    SqQueue queue = { 0 };

    InitStack(queue);
    for (i = 0; i < sizeof(array)/sizeof(ElemType); i++)
    {
        InQueue(queue, array[i]);
    }

    PrintQueue(queue);
    OutQueue(queue, data);
    OutQueue(queue, data);
    PrintQueue(queue);
    InQueue(queue, 11);
    InQueue(queue, 12);
    PrintQueue(queue);
    return 0;
}
复制代码

复制代码
#include "stdafx.h"
#include <stdlib.h>

#define MAXSIZE 10

typedef int ElemType;

/* 顺序栈结构 */
typedef struct 
{
    ElemType data[MAXSIZE];
    int front;
    int rear;
} SqQueue;

void InitStack(SqQueue &qu)
{
    qu.front = qu.rear = 0;
}

bool IsEmpty(SqQueue &qu)
{
    return (qu.front == qu.rear);
}

int InQueue(SqQueue &qu, ElemType data)
{
    if ((qu.rear + 1) % MAXSIZE == qu.front)
    {
        return -1;
    }
    
    qu.data[qu.rear] = data;
    qu.rear = (qu.rear+1) % MAXSIZE;
    
    return 0;
}

int OutQueue(SqQueue &qu, ElemType &data)
{
    if (qu.front == qu.rear)
    {
        return -1;
    }

    qu.front = (qu.front + 1) % MAXSIZE;
    data = qu.data[qu.front];    
    return 0;
}

void PrintQueue(SqQueue &qu)
{
    int i = 0;
    if (qu.front == qu.rear)
        return;

    i = qu.front;
    while (i != qu.rear)
    {
        printf("%d\t", qu.data[i]);
        i = (i+1)%MAXSIZE;
    }
    printf("\r\n");
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{

    ElemType array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int i = 0;
    int data = 0;
    SqQueue queue = { 0 };

    InitStack(queue);
    for (i = 0; i < sizeof(array)/sizeof(ElemType); i++)
    {
        InQueue(queue, array[i]);
    }

    PrintQueue(queue);
    OutQueue(queue, data);
    OutQueue(queue, data);
    PrintQueue(queue);
    InQueue(queue, 11);
    InQueue(queue, 12);
    PrintQueue(queue);
    return 0;
}
复制代码

posted on 2020-01-16 10:38  星云留水  阅读(132)  评论(0编辑  收藏  举报

导航