队列

顺序表示

#include 
#include 
using namespace std;

typedef int DataType;
#define MAXNUM 20  /* 队列中最大元素个数 */
struct SeqQueue {   /* 顺序队列类型定义 */
    int f, r;
    DataType q[MAXNUM];
};
typedef struct SeqQueue SeqQueue, *PSeqQueue;   /* 顺序队列类型和指针类型 */

                                                /*创建一个空队列*/
PSeqQueue createEmptyQueue_seq(void) {
    PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
    if (paqu == NULL)
        printf("Out of space!! \n");
    else
        paqu->f = paqu->r = 0;
    return paqu;
}

/*判队列是否为空队列*/
int isEmptyQueue_seq(PSeqQueue paqu) {
    return paqu->f == paqu->r;
}

/* 在队列中插入一元素x */
void enQueue_seq(PSeqQueue paqu, DataType x) {
    if ((paqu->r + 1) % MAXNUM == paqu->f)
        printf("Full queue.\n");
    else {
        paqu->q[paqu->r] = x;
        paqu->r = (paqu->r + 1) % MAXNUM;
    }
}

/* 删除队列头部元素 */
void deQueue_seq(PSeqQueue paqu) {
    if (paqu->f == paqu->r)
        printf("Empty Queue.\n");
    else
        paqu->f = (paqu->f + 1) % MAXNUM;
}

/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq(PSeqQueue paqu) {
    return paqu->q[paqu->f];
}

int main()
{
    PSeqQueue st;
    st = createEmptyQueue_seq();
    cout << isEmptyQueue_seq(st) << endl;
    enQueue_seq(st, 1);
    enQueue_seq(st, 2);
    enQueue_seq(st, 3);
    cout << frontQueue_seq(st) << endl;
    deQueue_seq(st);
    cout << frontQueue_seq(st) << endl;
    cout << isEmptyQueue_seq(st) << endl;
}

链接表示

#include 
#include 
using namespace std;

typedef int DataType;
struct Node;
typedef struct Node *PNode;
struct  Node {      /* 结点结构 */
    DataType info;
    PNode link;
};
struct LinkQueue {  /* 链接队列类型定义 */
    PNode f;        /* 头指针 */
    PNode r;        /* 尾指针 */
};
typedef struct LinkQueue *PLinkQueue;

/*创建一个空队列*/
PLinkQueue createEmptyQueue_link() {
    PLinkQueue plqu = (PLinkQueue)malloc(sizeof(struct LinkQueue));
    if (plqu != NULL)
        plqu->f = plqu->r = NULL;
    else
        printf("Out of space!! \n");

    return (plqu);
}

/*判断链接表示队列是否为空队列*/
int isEmptyQueue_link(PLinkQueue plqu) {
    return (plqu->f == NULL);
}


/*进队列*/
void enQueue_link(PLinkQueue plqu, DataType x) {
    PNode p = (PNode)malloc(sizeof(struct Node));
    if (p == NULL)
        printf("Out of space!");
    else {
        p->info = x;
        p->link = NULL;
        if (plqu->f == NULL)
            plqu->f = p;
        else
            plqu->r->link = p;

        plqu->r = p;
    }
}

/*出队列*/
void deQueue_link(PLinkQueue plqu) {
    PNode   p;
    if (plqu->f == NULL)
        printf("Empty queue.\n ");
    else {
        p = plqu->f;
        plqu->f = plqu->f->link;
        free(p);
    }
}

/* 在非空队列中求队头元素 */
DataType frontQueue_link(PLinkQueue plqu) {
    return (plqu->f->info);
}

int main()
{
    PLinkQueue st;
    st = createEmptyQueue_link();
    cout << isEmptyQueue_link(st) << endl;
    enQueue_link(st, 1);
    enQueue_link(st, 2);
    enQueue_link(st, 3);
    cout << frontQueue_link(st) << endl;
    deQueue_link(st);
    cout << frontQueue_link(st) << endl;
    cout << isEmptyQueue_link(st) << endl;
}

你可能感兴趣的:(队列)