• 简单数据结构之队列模拟


      1 /************************************************************************************** 
      2 * Function     : 模拟队列
      3 * Create Date  : 2014/04/23
      4 * Author       : NTSK13 
      5 * Email        : beijiwei@qq.com 
      6 * Copyright    : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 
      7 *                任何单位和个人不经本人允许不得用于商业用途 
      8 * Version      : V0.1                    
      9 ***************************************************************************************
     10 题目:简单数据结构之队列模拟
     11      
     12 **************************************************************************************/  
     13 #include<stdio.h>  
     14             
     15 #define MY_FUNC  1
     16 #if MY_FUNC  
     17 
     18 #define M 10 //队列最大长度
     19 
     20 typedef struct queue
     21 {
     22     int data[M];
     23     int len;//队列当前长度
     24 }Queue;
     25 
     26 int sample[M];
     27 int result[M];
     28 
     29 void init_queue(Queue *a);//队列初始化
     30 int in_queue(Queue *a,int value);//进队
     31 int out_queue(Queue *a);//出队
     32 int get_head_element(Queue *a);//获取队首的元素
     33 
     34 // The first method:
     35 int main()  
     36 {  
     37     int i=0;
     38     Queue sq;
     39     init_queue(&sq);
     40 
     41     freopen("input.txt","r",stdin);
     42 
     43     for(i=0;i<M;i++) 
     44         scanf("%d",&sample[i]);  // get input data
     45     out_queue(&sq);
     46     get_head_element(&sq);
     47            
     48     for(i=0;i<M;i++)
     49         in_queue(&sq,sample[i]);
     50     in_queue(&sq,100);
     51 
     52     for(i=0;i<M;i++)
     53         result[i]=out_queue(&sq);
     54 
     55     for(i=0;i<M;i++)
     56     {
     57         printf("%d	",result[i]);
     58         fflush(stdout);//修复Eclipse printf()不能显示的小bug
     59     }
     60 
     61     printf("
    ");
     62     fflush(stdout);
     63 
     64     return (0);
     65 }  
     66 
     67 void init_queue(Queue *a)//队列初始化
     68 {
     69     (*a).len=0;
     70 }
     71 //该种情况,进队效率太低
     72 int in_queue(Queue *a,int value)//进队
     73 {
     74     int i=0;
     75     if( (*a).len==M)
     76     {
     77         printf("Queue is full ,can not  in !!!
    ");
     78         fflush(stdout);
     79         return (-1);
     80     }else{
     81         if((*a).len==0)
     82         {
     83             (*a).data[0]=value;
     84             (*a).len++;
     85             return (1);
     86         }else
     87         {
     88             for(i=(*a).len;i>0;i--)
     89             {
     90                 (*a).data[i]=(*a).data[i-1];
     91             }
     92             (*a).data[0]=value;
     93             (*a).len++;
     94             return (1);
     95         }
     96     }
     97 }
     98 int out_queue(Queue *a)
     99 {
    100     int tmp=0;
    101     if((*a).len==0)
    102     {
    103         printf("Queue is empty ,can not out !!!
    ");
    104         fflush(stdout);
    105         return (-1);
    106     }else{
    107         tmp=(*a).len-1;
    108         (*a).len--;
    109         return ( (*a).data[tmp]);
    110     }
    111 }
    112 int get_head_element(Queue *a)
    113 {
    114     if( (*a).len==0)
    115     {
    116         printf("Queue is empty full ,can not get !!!
    ");
    117         fflush(stdout);
    118         return (-1);
    119     }else{
    120         return ( (*a).data[ (*a).len -1]);
    121     }
    122 }
    123     
    124 /********************************************my function end**************************************************/  
    125 #else  
    126 
    127 
    128 #define M 10 //队列最大长度
    129 
    130 typedef struct queue
    131 {
    132     int data[2*M-1];//队列最长为M
    133     int head;
    134     int tail;
    135     int len;//队列当前长度
    136 }Queue;
    137 
    138 int sample[M];
    139 int result[M];
    140 
    141 void init_queue(Queue *a);//队列初始化
    142 int in_queue(Queue *a,int value);//进队
    143 int out_queue(Queue *a);//出队
    144 int get_head_element(Queue *a);//获取队首的元素
    145 
    146 // The second method:    
    147 int main()  
    148 {         
    149     int i=0;
    150     Queue sq;
    151     init_queue(&sq);
    152 
    153     freopen("input.txt","r",stdin);
    154 
    155     for(i=0;i<M;i++)
    156         scanf("%d",&sample[i]);  // get input data
    157     out_queue(&sq);
    158     get_head_element(&sq);
    159 
    160     for(i=0;i<M;i++)
    161         in_queue(&sq,sample[i]);
    162     in_queue(&sq,100);
    163 
    164     for(i=0;i<M;i++)
    165         result[i]=out_queue(&sq);
    166 
    167     for(i=0;i<M;i++)
    168     {
    169         printf("%d	",result[i]);
    170         fflush(stdout);//修复Eclipse printf()不能显示的小bug
    171     }
    172 
    173     printf("
    ");
    174     fflush(stdout);
    175 
    176     return (0);
    177 }
    178 
    179 
    180 void init_queue(Queue *a)
    181 {
    182     (*a).len=0;
    183 }
    184 
    185 //这种方法 避免了 for 循环
    186 int in_queue(Queue *a,int value)//进队
    187 {
    188     if( (*a).len==M)
    189     {
    190         printf("Queue is full ,can not  in !!!
    ");
    191         fflush(stdout);
    192         return (-1);
    193     }else{
    194         if((*a).len==0)
    195         {
    196             (*a).data[M]=value;
    197             (*a).len++;
    198             (*a).head=M;
    199             (*a).tail=M;
    200             return (1);
    201         }else
    202         {
    203             if( (*a).tail ==0 )
    204                 (*a).tail=2*M-2;
    205             else
    206                 (*a).tail--;
    207 
    208             (*a).data[ (*a).tail]=value;
    209             (*a).len++;
    210             return (1);
    211         }
    212     }
    213 }
    214 int out_queue(Queue *a)
    215 {
    216     int tmp=0;
    217     if((*a).len==0)
    218     {
    219         printf("Queue is empty ,can not out !!!
    ");
    220         fflush(stdout);
    221         return (-1);
    222     }else{
    223         tmp=(*a).head;
    224         if((*a).head==0)
    225             (*a).head=2*M-2;
    226         else
    227             (*a).head--;
    228         (*a).len--;
    229         return ( (*a).data[tmp]);
    230     }
    231 }
    232 int get_head_element(Queue *a)
    233 {
    234     if( (*a).len==0)
    235     {
    236         printf("Queue is empty full ,can not get !!!
    ");
    237         fflush(stdout);
    238         return (-1);
    239     }else{
    240         return ( (*a).data[ (*a).head ]);
    241     }
    242 }
    243 
    244 #endif  
  • 相关阅读:
    jquery 拼图小游戏
    重要参考SQL
    SQL Server save transaction
    SelectList类的构造函数
    一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程重要
    springMVC,spring和Hibernate整合(重要)
    delphi环境变量
    C# Chart 点击获取当前点击坐标和Series
    如何修改 app.config 的配置信息
    C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)
  • 原文地址:https://www.cnblogs.com/ntsk13/p/3701427.html
Copyright © 2020-2023  润新知