Java数据结构之链式队列

package JavaProject;

public class TestLinkQueue {
	public static void main(String [] args){
		LinkQueue<String> queue = new LinkQueue<String>();
		queue.pop();
		queue.push("aaaa");
		queue.push("bbbb");
		System.out.println("此时队头元素是:" + queue.front());
		queue.pop();
		System.out.println("执行pop之后队头元素是:" + queue.front());
		System.out.println("未执行clear之后元素个数是:"+queue.size);
		queue.clear();
		System.out.println("执行clear之后元素个数是:"+queue.size);
	}
}

class LinkQueue<T>{
	public class Node{
		T data;
		Node next;
		public Node(T data,Node next){
			this.data = data;
			this.next = next;
		}
	}
	private Node front;
	private Node rear;
	int size;
	public LinkQueue(){
		front = null;
		rear = null;
		size = 0;
	}
	public boolean empty(){
		return size == 0;
	}
	public int length(){
		return size;
	}
	public void push(T element){
		if(front == null){
			front = new Node(element,null);
			rear = front;
		}
		else{
			Node tempNode = new Node(element,null);
			rear.next = tempNode;
			rear = tempNode;
		}
		size++;
	}
	public T front(){
		return front.data;
	}
	public T pop(){
		if(empty() == true){
			System.out.println("队列已空");
		}
		else{
			Node del = front;
			front = del.next;
			del.next = null;
			size--;
			return del.data;
		}
		return null;
	}
	public void clear(){
		front = null;
		rear = null;
		size = 0;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值