zoukankan      html  css  js  c++  java
  • 数据结构学习第七天

    14:32:26 2019-08-22

    学习

    队列的数组实现

    QueueInArray.h

     1 #ifndef _QUEUE_IN_ARRAY
     2 #define _QUEUE_IN_ARRAY
     3 struct AQueue;
     4 typedef struct AQueue* Queue;
     5 
     6 int IsEmpty(Queue Q);
     7 int IsFull(Queue Q);
     8 Queue CreateAQueue(int MaxSize);
     9 void DisposeAQueue(Queue Q);
    10 void MakeEmpty(Queue Q);
    11 void Enquence(int Element, Queue Q);
    12 int Front(Queue Q);
    13 void Dequence(Queue Q);
    14 int FrontAndDequence(Queue Q);
    15 #endif // !_QUEUE_IN_ARRAY
    View Code

    QueueInArray.h

      1 #include"QueueInArray.h"
      2 #include<malloc.h>
      3 #include<stdio.h>
      4 struct AQueue
      5 {
      6     int Capacity;
      7     int Front;
      8     int rear;
      9     int Size;
     10     int* Array;
     11 };
     12 static int Succ(int Value, Queue Q)
     13 {
     14     if (Value < Q->Capacity)
     15         return Value;
     16     else if (IsFull(Q))
     17     {
     18         printf("Queue is Full
    ");
     19         return -1;
     20     }
     21     return 0;
     22 }
     23 
     24 int IsEmpty(Queue Q)
     25 {
     26     return Q->Size == 0;
     27 }
     28 
     29 int IsFull(Queue Q)
     30 {
     31     return Q->Size == Q->Capacity;
     32 }
     33 
     34 Queue CreateAQueue(int MaxSize)
     35 {
     36     Queue Q = (Queue)malloc(sizeof(struct AQueue));
     37     Q->Capacity = MaxSize;
     38     Q->Array = (int*)malloc(sizeof(int) * MaxSize);
     39     MakeEmpty(Q);
     40     return Q;
     41 }
     42 
     43 void DisposeAQueue(Queue Q)
     44 {
     45     free(Q->Array);
     46     free(Q);
     47 }
     48 
     49 void MakeEmpty(Queue Q)
     50 {
     51     Q->Size = 0;
     52     Q->Front = 1;
     53     Q->rear = 0;
     54 }
     55 
     56 void Enquence(int Element, Queue Q)
     57 {
     58     if (IsFull(Q))
     59     {
     60         printf("Queue is Full
    ");
     61         return;
     62     }
     63     else
     64     {
     65         Q->rear = Succ(++Q->rear,Q);
     66         Q->Array[Q->rear] = Element;
     67         Q->Size++;                     //这边将 Size++写在最后 因为在Succ的检测中会使用IsFull来进行判断
     68     }
     69 }
     70 
     71 int Front(Queue Q)
     72 {
     73     return Q->Array[Q->Front];
     74 }
     75 
     76 void Dequence(Queue Q)
     77 {
     78     if (IsEmpty(Q))
     79     {
     80         printf("Queue is Empty
    ");
     81         return;
     82     }
     83     else
     84     {
     85         Q->Front=Succ(++Q->Front,Q);
     86         Q->Size--;
     87     }
     88 }
     89 
     90 int FrontAndDequence(Queue Q)
     91 {
     92     if (IsEmpty(Q))
     93     {
     94         printf("Queue is Empty
    ");
     95         return -1;
     96     }
     97     else
     98     {
     99         int Element = Q->Array[Q->Front];
    100         Dequence(Q);
    101         return Element;
    102     }
    103 }
    View Code

    main.c

     1 #include<stdio.h>
     2 #include"QueueInArray.h"
     3 
     4 int main()
     5 {
     6     Queue Q = NULL;
     7     Q = CreateAQueue(4);
     8     printf("%10d %10d
    ", IsEmpty(Q), IsFull(Q));
     9     Enquence(20, Q);
    10     printf("%10d %10d
    ", Front(Q), IsEmpty(Q));
    11     Enquence(25, Q);
    12     Enquence(30, Q);
    13     Enquence(45, Q);
    14     printf("%10d %10d
    ",Front(Q), IsEmpty(Q));
    15     Dequence(Q);
    16     Dequence(Q);
    17     printf("%10d
    ", FrontAndDequence(Q));
    18     printf("%10d
    ", FrontAndDequence(Q));
    19     DisposeAQueue(Q);
    20     return 0;
    21 }
    View Code

    测试:

  • 相关阅读:
    引用传递函数值
    关于引用(python中的伪指针)的理解
    学生管理系统_排序后通过name删除列表里的字典
    学生管理系统(函数版)
    全局变量和局部变量的理解
    lambda隐藏函数的嵌套
    lambda函数常见用法
    函数的多个返回值
    函数的函数名重名
    函数的嵌套
  • 原文地址:https://www.cnblogs.com/57one/p/11395124.html
Copyright © 2011-2022 走看看