数据结构队列的各种操作

 
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. typedefintElementType;
  4. typedefstructQueue
  5. {
  6. intrear,front;
  7. ElementType*elements;
  8. intMaxSize;
  9. }Queue;
  10. voidInitQueue(Queue*Q,intsz)//初始化
  11. {
  12. Q->MaxSize=sz;
  13. Q->elements=(ElementType*)malloc(sizeof(ElementType)*Q->MaxSize);
  14. Q->front=Q->rear=0;
  15. }
  16. voidfreeQueue(Queue*Q,intsz)//释放空间
  17. {
  18. free(Q->elements);
  19. }
  20. voidMakeEmpty(Queue*Q)//置空
  21. {
  22. Q->front=Q->rear=0;
  23. }
  24. intLength(Queue*Q)//返回长度
  25. {
  26. return(Q->rear-Q->front+Q->MaxSize)%(Q->MaxSize);
  27. }
  28. intIsFull(Queue*Q)//判断是否为满
  29. {
  30. if(Q->rear!=0&&(Q->front==(Q->rear)%(Q->MaxSize)))
  31. return1;
  32. else
  33. return0;
  34. }
  35. intIsEmpty(Queue*Q)//判断是否为空
  36. {
  37. if(Q->front==Q->rear)
  38. return1;
  39. else
  40. return0;
  41. }
  42. voidEnQueue(Queue*Q)//进队
  43. {ElementTypeitem;
  44. scanf("%d",&item);
  45. while((!IsFull(Q))&&(item!=-1))
  46. {
  47. Q->elements[Q->rear]=item;
  48. Q->rear=(Q->rear+1)%(Q->MaxSize);
  49. scanf("%d",&item);
  50. }
  51. }
  52. ElementTypeDeQueue(Queue*Q)
  53. {
  54. ElementTypeitem;
  55. if(!IsEmpty(Q))
  56. {
  57. item=Q->elements[Q->front];
  58. Q->front=(Q->front+1)%(Q->MaxSize);
  59. returnitem;
  60. }
  61. else
  62. {
  63. printf("对空!\n");
  64. exit(1);
  65. }
  66. }
  67. ElementTypeGetFront(Queue*Q)
  68. {
  69. if(!IsEmpty(Q))
  70. returnQ->elements[Q->front];
  71. else
  72. {
  73. printf("队空!\n");
  74. exit(1);
  75. }
  76. }
  77. voidmain()
  78. {
  79. inti;
  80. QueueQ;
  81. InitQueue(&Q,10);
  82. EnQueue(&Q);
  83. for(i=0;i<10;i++)
  84. if(!IsEmpty(&Q))
  85. printf("%-5d",DeQueue(&Q));
  86. }

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/817866

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值