免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1186 | 回复: 0
打印 上一主题 下一主题

java队列 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-26 14:22 |只看该作者 |倒序浏览
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。   在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。   队列空的条件:front=rear   队列满的条件: rear = MAXSIZE.
  1. //通过LinkedList实现队列
  2. package 队列和堆栈;
  3. import java.util.*;
  4. public class LinkedListQueueTest {

  5. //字段
  6. private LinkedList list;

  7. //无参数构造
  8. public LinkedListQueueTest()
  9. {
  10.   list=new LinkedList();
  11. }

  12. //队列元素的个数
  13. public int size()
  14. {
  15.   return list.size();
  16. }

  17. //进入队列
  18. public void enqueue(Object obj)
  19. {
  20.   list.addLast(obj);
  21.   
  22. }


  23. //对头出来
  24. public Object dequeue()
  25. {
  26.   return list.removeFirst();
  27. }

  28. //浏览对头元素
  29. public Object front()
  30. {
  31.   //return list.getFirst();
  32.   return list.peekFirst();
  33. }

  34. //判断队列是否为空
  35. public boolean isEmpty()
  36. {
  37.   return list.isEmpty();
  38. }


  39. /**
  40.   * @param args
  41.   */
  42. public static void main(String[] args) {
  43.   // TODO Auto-generated method stub
  44.   
  45.   LinkedListQueueTest llq=new LinkedListQueueTest();
  46.   System.out.println(llq.isEmpty());
  47.   llq.enqueue("147");
  48.   llq.enqueue("258");
  49.   llq.enqueue("369");
  50.   System.out.println(llq.size());
  51.   System.out.println("移除队列头元素:"+llq.dequeue());
  52.   System.out.println(llq.size());
  53.   llq.enqueue("abc");
  54.   llq.enqueue("def");
  55.   System.out.println(llq.size());
  56.   System.out.println("查看队列的头元素:"+llq.front());
  57.   System.out.println(llq.size());
  58.   System.out.println(llq.isEmpty());

  59. }

  60. }
  61. 通过数组实现
  62. package 队列和堆栈;

  63. import java.util.NoSuchElementException;

  64. //通过数组来实现队列
  65. public class ArrayQueue {
  66. //字段
  67. public  static  Object[] data;
  68. //队列的元素个数
  69. protected int size ;
  70. //队列头
  71. protected int head;
  72. //队列尾
  73. public static  int tail;
  74. /**
  75.   *
  76.   */
  77. //无参数构造函数
  78. public ArrayQueue() {
  79.   final int INITIAL_LENGTH=3;
  80.   data=new Object[INITIAL_LENGTH];
  81.   size=0;
  82.   head=0;
  83.   tail=-1;
  84. }

  85. //队列元素个数方法
  86. public int size()
  87. {
  88.   return size;
  89.   
  90. }

  91. public boolean isEmpty()
  92. {
  93.   return size==0;
  94. }

  95. //得到队列头元素
  96. public Object front()
  97. {
  98.   if(size==0)
  99.    throw new NoSuchElementException();
  100.   return data[head];
  101.   
  102. }

  103. //进入队列enqueue()方法
  104. public void enqueue(Object obj)
  105. {
  106.   //此时队列已经满
  107.   if(size==data.length){
  108.    Object[] oldData=data;
  109.    data=new Object[data.length*2];
  110.    //if(head==0)
  111.    System.arraycopy(oldData, head, data, 0, oldData.length-head);
  112.    if(head>0)
  113.     System.arraycopy(oldData, 0, data, head+1, tail+1);
  114.    head=0;
  115.    tail=oldData.length-1;
  116.    
  117.   }
  118.   tail=(tail+1)%data.length;
  119.   size++;
  120.   data[tail]=obj;
  121.   
  122. }

  123. //队列的元素出队
  124. public Object dequeue()
  125. {
  126.   if(size==0)
  127.    throw new NoSuchElementException();
  128.   Object ele=data[head];
  129.   //循环队列
  130.   head=(head+1)%data.length;
  131.   size--;
  132.   return ele;
  133. }

  134. @Override
  135. public String toString() {
  136.   // TODO Auto-generated method stub
  137.   return super.toString();
  138. }

  139. }
  140. 通过向量实现:
  141. //通过向量实现栈
  142. package 队列和堆栈;
  143. import java.util.*;
  144. public class VectorStackTest {
  145. //字段
  146. Vector  v;

  147. //构造函数
  148. public VectorStackTest()
  149. {
  150.   v=new Vector();
  151. }

  152. //元素的个数
  153. public int size()
  154. {
  155.   return v.size();
  156. }

  157. //是否为空
  158. public boolean isEmpty()
  159. {
  160.   return size()==0;
  161. }

  162. //进栈
  163. public Object Push(Object obj)
  164. {
  165.   v.addElement(obj);
  166.   return obj;
  167. }

  168. //出栈方法
  169. public Object Pop()
  170. {
  171.   int len=size();
  172.   Object obj=Peek();
  173.   v.removeElementAt(len-1);
  174.   return obj;
  175. }

  176. //查看栈顶元素
  177. public Object Peek()
  178. {
  179.   int len = size();
  180.   if (len == 0)
  181.       throw new EmptyStackException();
  182.   return v.elementAt(len - 1);
  183. }
  184. /**
  185.   * @param args
  186.   */
  187. public static void main(String[] args) {
  188.   // TODO Auto-generated method stub
  189.   VectorStackTest vst=new VectorStackTest();
  190.   System.out.println("大小:"+vst.size());
  191.   vst.Push("123");
  192.   vst.Push("456");
  193.   vst.Push("789");
  194.   vst.Push("abc");
  195.   System.out.println("大小:"+vst.size());
  196.   System.out.println("栈顶:"+vst.Peek());
  197.   System.out.println("出栈:"+vst.Pop());
  198.   vst.Push("def");
  199.   vst.Push("147");
  200.   System.out.println("大小:"+vst.size());
  201.   System.out.println("栈顶:"+vst.Peek());
  202.   System.out.println("出栈:"+vst.Pop());
  203.   System.out.println(vst.Peek());
  204.   vst.Push("def");
  205.   vst.Push("147");
  206.   System.out.println(vst.Pop());
  207.   System.out.println(vst.Pop());
  208.   System.out.println(vst.Peek());
  209.   System.out.println(vst.Pop());
  210.   System.out.println(vst.Pop());
  211.   vst.Push("1aadf");
  212.   vst.Push("2dafad");
  213.   vst.Push("123789");
  214.   System.out.println(vst.Pop());
  215.   System.out.println(vst.Peek());
  216.   System.out.println(vst.Pop());
  217.   System.out.println(vst.Peek());
  218.   System.out.println("------------------end------------");
  219.   VectorStackTest llst=new VectorStackTest();
  220.   llst.Push("123");
  221.   llst.Push("456");
  222.   System.out.println("栈顶:"+llst.Peek());
  223.   System.out.println("出栈:"+llst.Pop());
  224.   System.out.println(llst.Peek());
  225.   llst.Push("789");
  226.   llst.Push("abc");
  227.   System.out.println("栈顶:"+llst.Peek());
  228.   System.out.println("出栈:"+llst.Pop());
  229.   System.out.println(llst.size());
  230.   System.out.println("栈顶:"+llst.Peek());

  231.   
  232.   



  233.   

  234. }

  235. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP