• 链队列


     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;
    }
  • 相关阅读:
    基础才是重中之重~B/S系统中的事件订阅
    将不确定变为确实~请自己搞清楚异常页面的设置方法(网上那些资料说的基本都有问题!)
    基础才是重中之重~延迟初始化
    批量替换sqlserver数据库TEXT字段类型的数据
    12554 A Special "Happy Birthday" Song!!!
    Linux socket 网络编程常用函数总结
    新浪微博Python SDK笔记——发微博(一)
    在Ubuntu上下载、编译和安装Android 4.2 最新内核源代码(Linux Kernel)
    20、SQL Server 数据修改之Update
    19、SQL Server 数据修改之Insert into
  • 原文地址:https://www.cnblogs.com/sgawscd/p/10198933.html
Copyright © 2020-2023  润新知