当前位置:Gxlcms > PHP教程 > 队列有几种实现方式?

队列有几种实现方式?

时间:2021-07-01 10:21:17 帮助过:113人阅读

队列有3种实现方式,实现方式为:1、基于链表来实现队列;2、使用linkedList来实现队列;3、使用两个栈来实现一个队列。

队列有3种实现方式,实现方式为:

1、基于链表来实现队列:

首先添加一个节点类,作为队列中的节点元素

  1. public class Node {//链表中的一个节点
  2. Node next = null;
  3. int data;
  4. //构造函数,用于添加链表时候使用
  5. public Node(int d) {
  6. this.data = d;
  7. };
  8. }

再新建一个类作为我们的队列,在该类中实现队列的入队和出队以及求队列的长度和判断队列是否为空等方法

①入队操作:

首先通过函数参数传入要入队的数据,根据传入的参数,新增一个节点Node,在入队方法中判断该队列是否为空,若该队列为空(head==tail),则该入队的节点既是队头也是队尾。若队列不为空,则将尾节点tail的next指针指向该元素,然后将tail节点指向该节点。

  1. public void put(Integer data) {
  2. Node newNode = new Node(data);//构造一个新节点
  3. if(head == null && tail == null) {//队列为空
  4. head = newNode;
  5. tail = newNode;
  6. }else {
  7. tail.next = newNode;
  8. tail = newNode;
  9. }
  10. }

②出队操作:

若队列为空,则返回空。若队列不为空,则返回该队列的头结点,然后将头结点的下一个元素重新作为头节点

  1. public Integer pop() {
  2. if(this.isEmpty()) {
  3. return null;
  4. }
  5. int data = head.data;
  6. head = head.next;
  7. return data;
  8. }

③判断队列空和对列长度很简单,直接贴代码,不再多说。

  1. public int size() {
  2. int count = 0;
  3. Node tmp = head;
  4. while(tmp != null) {
  5. count++;
  6. tmp = tmp.next;
  7. }
  8. return count;
  9. }

④详细代码实现:

  1. package com.wp.datastruct;
  2. /**
  3. * 利用链表来实现队列
  4. * */
  5. public class MyQueue<E> {
  6. Node head = null;//队列头
  7. Node tail = null;//队列尾
  8. /**
  9. * 入队操作:
  10. * 若该队列尾空,则入队节点既是头结点也是尾节点
  11. * 若队列不为空,则先用tail节点的next指针指向该节点
  12. * 然后将tail节点指向该节点
  13. * */
  14. public void put(Integer data) {
  15. Node newNode = new Node(data);//构造一个新节点
  16. if(head == null && tail == null) {//队列为空
  17. head = newNode;
  18. tail = newNode;
  19. }else {
  20. tail.next = newNode;
  21. tail = newNode;
  22. }
  23. }
  24. /**
  25. * 判断队列是否为空:当头结点等于尾节点的时候该队列就为空
  26. * */
  27. public boolean isEmpty() {
  28. return head == tail;
  29. }
  30. /**
  31. * 出队操作:
  32. * 若队列为空,则返回null
  33. * 否则,返回队列的头结点,并将head节点指向下一个
  34. * */
  35. public Integer pop() {
  36. if(this.isEmpty()) {
  37. return null;
  38. }
  39. int data = head.data;
  40. head = head.next;
  41. return data;
  42. }
  43. public int size() {
  44. int count = 0;
  45. Node tmp = head;
  46. while(tmp != null) {
  47. count++;
  48. tmp = tmp.next;
  49. }
  50. return count;
  51. }
  52. }

2、使用linkedList来实现队列

该方法是使用java中的linkedList集合来实现一个队列,通过调用linkedList中的方法来实现队列的入队出队,判空等操作

首先new一个类来作为我们的队列,该类中包含两个属性,一个是size:用来统计该队列的长度,另一个是LinkedList对象,

代表我们的队列。

  1. private LinkedList<E> list = new LinkedList<>();
  2. private int size = 0;//用于统计队列的长度

①入队操作:

应为linkedList集合中已经实现好了添加删除操作,在这里我们只需要简单的调用方法就可以实现队列的相关操作了,非常简单而且容易理解。

  1. public synchronized void put(E data) {//保证线程安全,实现同步操作
  2. list.add(data);
  3. size++;
  4. }

②出队操作:

  1. public synchronized E pop() {
  2. size--;
  3. return list.removeFirst();//从头取出
  4. }

③详细代码:

  1. public class MyQueue2<E> {
  2. private LinkedList<E> list = new LinkedList<>();
  3. private int size = 0;//用于统计队列的长度
  4. public synchronized void put(E data) {//保证线程安全,实现同步操作
  5. list.add(data);
  6. size++;
  7. }
  8. public synchronized E pop() {
  9. size--;
  10. return list.removeFirst();//从头取出
  11. }
  12. public synchronized int size() {
  13. return size;
  14. }
  15. public boolean isEmpty() {
  16. return size == 0;
  17. }
  18. }

3、使用两个栈来实现一个队列。

也可以使用两个实现好的栈来实现一个队列(这个问题可能面试笔试的时候回被问到)。

实现方法是:

创建两个栈s1,s2。入队的时候就只压栈到s1中。

出队分两种情况:1.当s2不为空的使用,就弹出栈顶元素作为出队元素。

2.当s2等于空,则先将s1中的元素全部弹出到s2中,再从s2中弹出栈顶元素作为出队元素。

①入队:

  1. public synchronized void put(E data) {//使用同步处理,保证线程安全
  2. s1.push(data);
  3. }

②出队:

  1. public synchronized E pop() {
  2. if(!s2.isEmpty()) {
  3. return s2.pop();
  4. }else {
  5. s2.push(s1.pop());
  6. return s2.pop();
  7. }
  8. }

③.详细代码实现:

  1. package com.wp.datastruct;
  2. import java.util.Stack;
  3. /**
  4. * 利用两个栈来模拟队列操作
  5. * 入队操作就只是想栈s1中添加,
  6. * 出栈操作分为两部分:
  7. * 1.当s2中不为空的时候,就直接弹出s2中的栈顶数据
  8. * 2.当s2中为空的时候,就先把s1中的数据全部弹出到s2中然后将栈顶数据出栈
  9. *
  10. * */
  11. public class MyQueue3<E> {
  12. Stack<E> s1 = new Stack<>();
  13. Stack<E> s2 = new Stack<>();
  14. public synchronized void put(E data) {//使用同步处理,保证线程安全
  15. s1.push(data);
  16. }
  17. public synchronized E pop() {
  18. if(!s2.isEmpty()) {
  19. return s2.pop();
  20. }else {
  21. s2.push(s1.pop());
  22. return s2.pop();
  23. }
  24. }
  25. public synchronized boolean isEmpty() {
  26. return s1.isEmpty() && s2.empty();
  27. }
  28. }

推荐教程:《php视频教程》

以上就是队列有几种实现方式?的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行