码迷,mamicode.com
首页 > 其他好文 > 详细

链队列

时间:2018-12-30 12:08:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:.cpp   cpp   flow   class   while   def   span   stat   null   

 1 //Queue.h
 2 
 3 #ifndef QUEUE_H
 4 #define QUEUE_H
 5 
 6 #define OK 1
 7 #define ERROR 0
 8 typedef int QElemType;
 9 typedef int Status;
10 
11 typedef struct QNode {
12     QElemType data;
13     struct QNode *next;
14 }QNode,*QueuePtr;
15 typedef struct {
16     QueuePtr front;  //队头指针
17     QueuePtr rear;   //队尾指针
18 }LinkQueue;
19 
20 Status InitQueue(LinkQueue &Q);
21 Status DestroyQueue(LinkQueue &Q);
22 Status ClearQueue(LinkQueue &Q);
23 Status QueueEmpty(LinkQueue Q);
24 int QueueLength(LinkQueue Q);
25 Status GetHead(LinkQueue Q, QElemType &e);
26 Status EnQueue(LinkQueue &Q, QElemType e);//插入队尾元素
27 Status DeQueue(LinkQueue &Q, QElemType &e);//删除队头元素
28 Status QueueTraverse(LinkQueue Q);
29 #endif
//Queue.cpp

#include"Queue.h"
#include<iostream>
#include<cstdlib>
using namespace std;

Status InitQueue(LinkQueue &Q)
{
    //构造空队列
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    if (!Q.front)
        exit(OVERFLOW);
    Q.front->next = NULL;
    return OK;
}
Status DestroyQueue(LinkQueue &Q)
{
    while (Q.front)
    {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}
Status ClearQueue(LinkQueue &Q)
{
//    Q.rear = Q.front;
    DestroyQueue(Q);
    InitQueue(Q);
    return OK;
}
Status QueueEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return OK;
    return ERROR;
}
int QueueLength(LinkQueue Q)
{
    QueuePtr temp = Q.front;
    int i = 0;
    while (temp != Q.rear)
    {
        temp = temp->next;
        i++;
    }
    return i;
}
Status GetHead(LinkQueue Q, QElemType &e)
{
    if (QueueEmpty(Q))
        return ERROR;
    e = Q.front->next->data;
    return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e)
{
    QueuePtr P = (QueuePtr)malloc(sizeof(QNode));
    if (!P)
        exit(OVERFLOW);
    P->data = e;
    P->next = NULL;
    Q.rear->next = P;
    Q.rear = P;
    return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e)
{
    if (QueueEmpty(Q))
        return ERROR;
    QueuePtr P = Q.front->next;
    e = P->data;
    Q.front->next = P->next;
    if (Q.rear == P)
        Q.rear = Q.front;
    free(P);
    return OK;
}
Status QueueTraverse(LinkQueue Q)
{
    if (QueueEmpty(Q))
    {
        cout << "Empty!" << endl;
        return ERROR;
    }
    QueuePtr temp = Q.front;
    while (temp != Q.rear)
    {
        temp = temp->next;
        cout << temp->data << " ";
    }
    cout << endl;
    return OK;
}
//Main.cpp

#include"Queue.h"
#include<iostream>
using namespace std;
int main()
{
    LinkQueue LQ;
    InitQueue(LQ);
    QElemType temp;
    QElemType temp2;
    cout << QueueEmpty(LQ) << endl;
    EnQueue(LQ, 2);
    EnQueue(LQ, 7);
    EnQueue(LQ, 1);
    EnQueue(LQ, 5);
    EnQueue(LQ, 2);
    EnQueue(LQ, 0);
    QueueTraverse(LQ);
    DeQueue(LQ, temp);
    cout << "temp = " << temp << endl;
    QueueTraverse(LQ);
    cout << "length:" << QueueLength(LQ)<<endl;
    GetHead(LQ, temp);
    cout << "head = " << temp << endl;
//    DestroyQueue(LQ);
    ClearQueue(LQ);
    cout << "after Destroy LQ: " << endl;
    QueueTraverse(LQ);
    cout << "length:" << QueueLength(LQ) << endl;
    GetHead(LQ, temp2);
    cout << "head = " << temp2 << endl;
    system("pause");
    return 0;
}

 

链队列

标签:.cpp   cpp   flow   class   while   def   span   stat   null   

原文地址:https://www.cnblogs.com/sgawscd/p/10198933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!