东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1070|回复: 0
打印 上一主题 下一主题

[C/C++] 用纯c简单封装一个队列

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2021-4-28 16:23:34 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式



用纯c简单封装一个队列


  1. #include "j_queue.h"

  2. //用纯c简单封装一个队列
  3. void main()
  4. {
  5.         //我只需要把 j_queue.cpp 和 j_queue.h 文件给同事即可 需要将队列封装一下
  6.         //通过头文件封装的方式,将队列的使用与操作分离,代码复用与统一
  7.         Q Q1;//创建
  8.         printf("内存空间全部就分配:%d\n", sizeof(Q1));

  9.         init(&Q1);

  10.         enQueue(&Q1, 'A');
  11.         printfQ(&Q1);


  12.         enQueue(&Q1, 'B');
  13.         printfQ(&Q1);

  14.         enQueue(&Q1, 'C');
  15.         printfQ(&Q1);
  16.         enQueue(&Q1, 'D');
  17.         printfQ(&Q1);
  18.         enQueue(&Q1, 'E');
  19.         printfQ(&Q1);

  20.         DeQueue(&Q1);
  21.         printfQ(&Q1);
  22.         DeQueue(&Q1);
  23.         printfQ(&Q1);
  24.         DeQueue(&Q1);
  25.         printfQ(&Q1);
  26.         DeQueue(&Q1);
  27.         printfQ(&Q1);
  28.         DeQueue(&Q1);
  29.         printfQ(&Q1);
  30.         printf("\n");


  31.         DeQueue(&Q1);
  32.         printfQ(&Q1);
  33.         DeQueue(&Q1);
  34.         printfQ(&Q1);
  35.         DeQueue(&Q1);
  36.         printfQ(&Q1);



  37.         system("pause");
  38.         //getchar();
  39.         return ;
  40. }
复制代码


  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define N 100   //定义队列最大多少个
  4. #define datatype char  //定义队列的数据类型

  5. struct queue
  6. {
  7.         datatype data[N];//保存数据的数组
  8.         int front;  //数据的开头
  9.         int rear;  //数据的结尾
  10. };
  11. typedef struct queue Q; //给已经有的类型简化一下


  12. void init(Q *myqueue); //初始化结构体
  13. int isempty(Q *myqueue); //判断是否为空,1为空,0不为空

  14. void enQueue(Q *myqueue,datatype num);  //入队
  15. datatype DeQueue(Q *myqueue);//出队

  16. void printfQ(Q *myqueue); //打印队列所有的元素

  17. datatype gethead(Q *myqueue);//获取开头的一个节点
复制代码


  1. #include "j_queue.h"


  2. void init(Q *myqueue)
  3. {
  4.         myqueue->front = myqueue->rear = 0;
  5. }

  6. int isempty(Q *myqueue)
  7. {
  8.         if (myqueue->front==myqueue->rear)
  9.         {
  10.                 return 1;
  11.         }
  12.         else
  13.         {
  14.                 return 0;
  15.         }
  16. }


  17. void enQueue(Q *myqueue, datatype num)
  18. {
  19.         if (myqueue->rear == N)
  20.         {
  21.                 printf("队列空间是满的");
  22.                 return ;
  23.         }
  24.         else
  25.         {
  26.                 myqueue->data[myqueue->rear] = num;//赋值
  27.                 myqueue->rear += 1;//增加一个
  28.         }
  29. }

  30. datatype DeQueue(Q *myqueue)
  31. {
  32.         if (myqueue->front==myqueue->rear)
  33.         {
  34.                 printf("没有数据 无法出队列");
  35.                 return -1;
  36.         }
  37.         else
  38.         {
  39.                 myqueue->front += 1;
  40.                 return myqueue->data[myqueue->front-1];
  41.         }
  42. }


  43. void printfQ(Q *myqueue) //打印队列所有的元素
  44. {
  45.         printf("\n");
  46.         if (myqueue->front==myqueue->rear)
  47.         {
  48.                 printf("队列为空\n");
  49.         }
  50.         else
  51.         {
  52.                 for (int i = myqueue->front; i < myqueue->rear; i++)
  53.                 {
  54.                         printf("%6c",myqueue->data[i]);
  55.                 }
  56.         }
  57. }

  58. datatype gethead(Q *myqueue)
  59. {
  60.         if (myqueue->front == myqueue->rear)
  61.         {
  62.                 printf("为空");
  63.                 return -1;
  64.         }
  65.         else
  66.         {
  67.                 return myqueue->data[myqueue->front];
  68.         }
  69. }

复制代码




j_queue.png (9.23 KB, 下载次数: 77)

j_queue.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-19 17:42 , Processed in 0.186901 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表