队列结构

来源:互联网 发布:阿里云服务器安装镜像 编辑:程序博客网 时间:2024/06/16 21:14



  队列结构是一种常见的数据结构,从数据的逻辑结构来看是线性结构,从存储结构来说分顺序和链式存储。且队列的两个基本操作是入队列和出队列,在队头进行出队列操作,在队尾进行入队列操作.

 

 队列中操作的对象:


Java代码  收藏代码
  1. public class DATA4 {  
  2.     String name;  
  3.     int age;  
  4. }  

 队列的基本操作:

 

伦理片 http://www.dotdy.com/

Java代码  收藏代码
  1. public class SQType {  
  2.      
  3.     static final int QUEUELEN=15;  
  4.     DATA4[] data=new DATA4[QUEUELEN];//队列数组  
  5.     int head;//队列头  
  6.     int tail;//队列尾  
  7.       
  8.     /** 
  9.      * 初始化队列 
  10.      */  
  11.     SQType SQTypeInit(){  
  12.         SQType q;  
  13.         if((q=new SQType())!=null){  
  14.             q.head=0;//设置队头  
  15.             q.tail=0;//设置队尾  
  16.             return q;  
  17.         }else{  
  18.             return null;  
  19.         }  
  20.     }  
  21.       
  22.     /** 
  23.      * 判断空队列 
  24.      */  
  25.     int SQTypeEmpty(SQType q){   
  26.         int temp=0;  
  27.         if(q.head==q.tail)  
  28.             temp=1;  
  29.             return temp;  
  30.     }  
  31.       
  32.     /** 
  33.      * 判断满队列 
  34.      */  
  35.     int SQTypeIsFull(SQType q){  
  36.         int temp=0;  
  37.         if(q.tail==QUEUELEN)  
  38.             temp=1;  
  39.             return temp;  
  40.     }  
  41.       
  42.     /** 
  43.      * 清空队列 
  44.      */  
  45.     void SQTypeClear(SQType q){  
  46.         q.head=0//设置对头  
  47.         q.tail=0;//设置队尾  
  48.     }  
  49.       
  50.     /** 
  51.      * 释放内存 
  52.      */  
  53.     void SQTypeFree(SQType q){  
  54.         if(q!=null){  
  55.             q=null;  
  56.         }  
  57.     }  
  58.       
  59.     /** 
  60.      * 入队列 
  61.      */  
  62.     int InSQType(SQType q,DATA4 data){  
  63.         if(q.head==QUEUELEN){  
  64.             System.out.println("队列已经满了,操作失败");  
  65.             return 0;  
  66.         }else{  
  67.             q.data[q.tail++]=data;  
  68.             return 1;  
  69.         }  
  70.     }  
  71.       
  72.     /** 
  73.      * 出队列 
  74.      */  
  75.     DATA4 OutSQType(SQType q){  
  76.         if(q.head==q.tail){  
  77.             System.out.println("队列已经为空");  
  78.             System.exit(0);  
  79.         }else{  
  80.             return q.data[q.head++];  
  81.         }  
  82.         return null;  
  83.     }  
  84.       
  85.     /** 
  86.      * 读取结点 
  87.      */  
  88.     DATA4 PeekSQType(SQType q){  
  89.         if(SQTypeEmpty(q)==1){  
  90.             System.out.println("空队列");  
  91.             return null;  
  92.         }else{  
  93.             return q.data[q.head];  
  94.         }  
  95.     }  
  96.       
  97.     /** 
  98.      * 计算队列长度 
  99.      */  
  100.     int SQTypeLen(SQType q){  
  101.         int temp;  
  102.         temp=q.tail-q.head;  
  103.         return temp;  
  104.     }  
  105. }  

 影音先锋电影 http://www.iskdy.com/ 


 看看就明白了,也就这么几句.

 从这个代码学到了:

 1.就是队列的一些基本操作

 2.++a,a++。用的确实巧妙,如果是我自己,应该不会那么写的

 3.数据情况、释放内存

 

 越来越淡静了...在想想这段代码,确实写的好. 呵呵、感谢分享.



0 0