package com.stack;
public class MyQueue {
Node current;
Node head;
class Node{
Node next;
String value;
Node(String value){
this.value=value;
}
@Override
public String toString() {
if (this.next == null) {
return String.valueOf(this.value);
}
return this.next.toString() + " -> " + this.value;
}
}
//推操作后指针后移
public void push(String value){
if (current==null){
head=new Node(value);
current=head;
return;
}
Node node = new Node(value);
current.next=node;
current=node;
}
//拉操作前指针后移
public String pop(){
if (head==null){
return null;
}
String res=head.value;
if (head==current){//两个指针相遇队列只剩最后一个元素
head=null;
current=null;
return res;
}
head=head.next;
return res;
}
@Override
public String toString(){
return head.toString();
}
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push("1");
myQueue.push("2");
myQueue.push("3");
myQueue.push("4");
myQueue.pop();
System.out.println(myQueue);
}
}