zoukankan      html  css  js  c++  java
  • PTA 7-1 银行业务队列简单模拟

      用链表实现队列操作,代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <malloc.h>
      5 
      6 using namespace std;
      7 
      8 //函数状态码定义
      9 #define TRUE        1
     10 #define FALSE       0
     11 #define OK          1
     12 #define ERROR       0
     13 #define INFEASIBLE -1
     14 #define OVERFLOW   -2
     15 
     16 typedef int Status;
     17 typedef int QElemType;
     18 
     19 typedef struct QNode {
     20     QElemType data;
     21     struct QNode *next;
     22 }QNode, *QueuePtr;
     23 
     24 typedef struct {
     25     QueuePtr front;
     26     QueuePtr rear;
     27 }LinkQueue;
     28 
     29 Status InitQueue(LinkQueue &Q) {
     30     Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
     31     if (!Q.front) exit(OVERFLOW);
     32     Q.front->next = NULL;
     33     return OK;
     34 }
     35 
     36 Status DestroyQueue(LinkQueue &Q) {
     37     while (Q.front) {
     38         cout << Q.front->data << endl;
     39         Q.rear = Q.front->next;
     40         free(Q.front);
     41         Q.front = Q.rear;
     42     }
     43     return OK;
     44 }
     45 
     46 Status EnQueue(LinkQueue &Q, QElemType e) {
     47     Q.rear->next = (QueuePtr)malloc(sizeof(QNode));
     48     if (!Q.rear->next) exit(OVERFLOW);
     49     Q.rear->data = e;
     50     Q.rear = Q.rear->next;
     51     Q.rear->next = NULL;
     52     return OK;
     53 }
     54 
     55 Status DeQueue(LinkQueue &Q, QElemType &e) {
     56     if (Q.front == Q.rear)
     57         return ERROR;
     58     e = Q.front->data;
     59     QueuePtr p = Q.front;
     60     Q.front = Q.front->next;
     61     free(p);
     62     return OK;
     63 }
     64 
     65 bool EmptyQueue(LinkQueue &Q) {
     66     return Q.front == Q.rear;
     67 }
     68 
     69 int main()
     70 {
     71     LinkQueue A, B;
     72     InitQueue(A);
     73     InitQueue(B);
     74     int n, m;
     75     cin >> n;
     76     for (int i = 0; i < n; ++i) {
     77         cin >> m;
     78         if (m & 1)
     79             EnQueue(A, m);
     80         else
     81             EnQueue(B, m);
     82     }
     83     bool ok = false;
     84     while (!EmptyQueue(A) && !EmptyQueue(B)) {
     85         DeQueue(A, m);
     86         if (ok)
     87             cout << ' ' << m;
     88         else
     89             cout << m;
     90 
     91         if (!EmptyQueue(A)) {
     92             DeQueue(A, m);
     93                 cout << ' ' << m;
     94         }
     95         DeQueue(B, m);
     96         cout << ' ' << m;
     97         ok = true;
     98     }
     99     while (!EmptyQueue(A)) {
    100         DeQueue(A, m);
    101         if (ok)
    102             cout << ' ' << m;
    103         else
    104             cout << m;
    105         ok = true;
    106     }
    107     while (!EmptyQueue(B)) {
    108         DeQueue(B, m);
    109         if (ok)
    110             cout << ' ' << m;
    111         else
    112             cout << m;
    113         ok = true;
    114     }
    115     return 0;
    116 }
    版权声明:该博客版权归本人所有,若有意转载,请与本人联系
  • 相关阅读:
    通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制
    解密腾讯课堂视频缓存文件
    Pycharm启动后加载anaconda一直updating indices造成Pycharm闪退甚至电脑崩溃
    Pycharm基本设置和插件安装
    Pycharm配置anaconda环境
    Anaconda管理Python环境
    Markdown介绍及工具推荐
    Android应用性能测试
    常用的adb命令
    QTP入门——玩玩小飞机
  • 原文地址:https://www.cnblogs.com/fan-jiaming/p/9743912.html
Copyright © 2011-2022 走看看