队列——Java实现
1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 //队列中元素个数(栈大小) 9 int size(); 10 //判断队列是否为空 11 boolean isEmpty(); 12 //取队头元素 13 Object getQueueHead(); 14 //取队尾元素 15 Object getQueueTail(); 16 //清空队列 17 void clear(); 18 } 19 class Factory3{ 20 private Factory3(){} 21 public static IQueue getIQueueInstance(){ 22 return new LinkQueueImpl(); 23 } 24 } 25 class LinkQueueImpl implements IQueue{ 26 Node head;//队列的头 27 Node tail;//队列的尾 28 int size; 29 class Node{ 30 Node prev; 31 Node next; 32 Object data; 33 public Node(Object data) { 34 super(); 35 this.data = data; 36 } 37 } 38 //入队列(头插) 39 public void add(Object obj) { 40 Node newNode = new Node(obj); 41 //空队列 42 if(head == null){ 43 head = newNode; 44 tail = newNode; 45 size++; 46 }else{ 47 Node node = tail; 48 newNode.next = node; 49 node.prev = newNode; 50 tail = newNode; 51 size++; 52 } 53 } 54 //出队列(尾删) 55 public Object remove() { 56 if(isEmpty()){ 57 return "无要删除元素"; 58 }else if(head == tail){ 59 //只有一个元素的队列 60 size--; 61 return head.data; 62 }else{ 63 Node node = head; 64 head = node.prev; 65 node.prev = null; 66 size--; 67 return head.data; 68 } 69 } 70 //求队列长度 71 public int size() { 72 return size; 73 } 74 //求队列是否为空 75 public boolean isEmpty() { 76 return (size()==0); 77 } 78 //取队首元素 79 public Object getQueueHead() { 80 return head.data; 81 } 82 //取对尾元素 83 public Object getQueueTail() { 84 return tail.data; 85 } 86 //打印队列 87 public void print(){ 88 myPrint(head); 89 } 90 //队首到对尾打印元素 91 private void myPrint(Node head){ 92 for(Node node = head;node!=null;node = node.prev){ 93 System.out.print(node.data+" "); 94 } 95 } 96 //置空队列 97 public void clear() { 98 if(head == null){ 99 System.out.println("空队列"); 100 }else{ 101 //非空队列 102 Node node1 = tail; 103 Node node = head; 104 for(node = head;head!=tail;){ 105 head = head.prev; 106 node.prev = null; 107 node.data = null; 108 size--; 109 } 110 node1.next = null; 111 node1.data = null; 112 size--; 113 } 114 } 115 } 116 public class LinkQueue { 117 public static void main(String[] args) { 118 IQueue queue = Factory3.getIQueueInstance(); 119 //向下转型 120 LinkQueueImpl queue1 = (LinkQueueImpl)queue; 121 System.out.println("以下为空队列测试情况"); 122 System.out.println("================remove测试函数=================="); 123 System.out.println(queue.remove()); 124 System.out.println("================size测试函数=================="); 125 System.out.println(queue.size()); 126 System.out.println("================isEmpty测试函数=================="); 127 System.out.println(queue.isEmpty()); 128 System.out.println("以下为非空队列测试情况"); 129 System.out.println("================add和print测试函数=================="); 130 queue.add("hello"); 131 queue.add("world!"); 132 queue.add("hi"); 133 queue.add("陕科大!"); 134 queue.add("today"); 135 queue.add("is"); 136 queue.add("4月18号!"); 137 queue1.print(); 138 System.out.println(); 139 System.out.println("================getQueueHead测试函数=================="); 140 System.out.println(queue.getQueueHead()); 141 System.out.println("================getQueueTail测试函数=================="); 142 System.out.println(queue.getQueueTail()); 143 System.out.println("================remove测试函数=================="); 144 System.out.println(queue.remove()); 145 System.out.println(queue.remove()); 146 System.out.println("================remove后print测试函数=================="); 147 queue1.print(); 148 System.out.println(); 149 System.out.println("================size测试函数=================="); 150 System.out.println(queue.size()); 151 System.out.println("================isEmpty测试函数=================="); 152 System.out.println(queue.isEmpty()); 153 System.out.println("================clear测试函数=================="); 154 queue.clear(); 155 System.out.println(queue.size()); 156 } 157 }